Comparing two doubles

Hi,

I'm working on a root finder function (something like Bissection or Newton) and I'm facing one doubt

Use doubles to perform the algorithm or use BigDecimal?

doubles give me faster computing but at some point I must compare the results with zero.For example I'm getting

-1.0

0.25

-0.4375

-0.109375

0.06640625

-0.0224609375

0.021728515625

-4.2724609375E-4

0.0106353759765625

0.005100250244140625

0.0023355484008789062

9.539127349853516E-4

...

(one line for each iteration)

and for each iteration I want to compare the result with 0 (zero). I want also a precision of, say, 8 decimal digits. How can I accomplish that?

Should I convert the doubles to BigDecimal and set a scale and then compare?

thanks in advance

[844 byte] By [manuel.leiria] at [2007-11-26 12:17:03]
# 1

Instead of testing for equality, test for a value within a comparison tollerance where you compute the comparison tollerance based on the 8 significant places you need.

For example if ((x < comparisonTollerance) && (x > -comparisonTollerance))

{

}

sabre150 at 2007-7-7 14:54:01 > top of Java-index,Archived Forums,Socket Programming...