Absolute function? - Script Writing - CHDK Forum

Absolute function?

  • 1 Replies
  • 1904 Views
Absolute function?
« on: 30 / January / 2009, 22:48:06 »
Advertisements
I need to compare the absolute values of two variables but there doesn't seem to be any function or operator to do that.

Normally I'd do something like:
if abs(a) > abs(b) then ...

The simplest way seems to be:
if a*a > b*b

Can anyone suggest a better way to do this?  Is there a speed penalty for those extra multiplications, or shouldn't I worry about it?  Should I use parenthesis, e.g. (a*a)?

*

Offline reyalp

  • ******
  • 13880
Re: Absolute function?
« Reply #1 on: 30 / January / 2009, 23:15:32 »
Don't worry about the speed penalty, it'll be nothing compared to the 10ms per statement that ubasic currently imposes.

If the result of the multiplication exceeds about 2 billion, you will run into problems.

If you use lua, you can use math.abs()
Don't forget what the H stands for.

 

Related Topics