Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there an abs method for Doubles? 1

Status
Not open for further replies.

Petemush

Technical User
Jun 21, 2002
255
GB
Quite simply, the subject line.

Basically I want to do this check in an IF statement

|a - b|
| --- | < eps
| c |

and I wanted to know if there was a method for absolute. If not I'm thinking the best way to do it is put a-b/c in a variable, check the positivity/negativity, change it if necessary and then do the check:

if(definedVariable < eps);

Sooooooooo, does anyone know of a method which will save me a few lines of code?

Cheers,

Pete
 
What you're looking for is this:

Math.abs((a-b)/c)

If c is zero then you won't get an execption. Instead your comparison will always return false. This is because the result is Nan (Not a Number) and comparisons always return false.

Because of this you might get some &quot;silent errors&quot; in your program.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top