high precision Math with 'double' (underflow problem)

W

Wolfgang

I'm trying to detect a very small difference in a math operation, like
this:
double c;
double a;
double b= 3e8;
double result = Math.sqrt(b*b + 100.0) - b;
System.out.println(b + " " + result);

This dispalys a result of

result: 1.7881393432617188E-7

If I increase b to say b=3e9 and greater, result goes to 0;

result: 0.0

I think it's just an underflow, showing zero for very small numbers.
In Java, what can I do to increase the precision?

PS: I know BigInteger, but that obviously won't help me here.

Thanks for your help,

Wolfgang
Santa Barbara, CA
 
M

Matt Parker

Wolfgang said:
I'm trying to detect a very small difference in a math operation, like
this:
double c;
double a;
double b= 3e8;
double result = Math.sqrt(b*b + 100.0) - b;
System.out.println(b + " " + result);

This dispalys a result of

result: 1.7881393432617188E-7

If I increase b to say b=3e9 and greater, result goes to 0;

result: 0.0

I think it's just an underflow, showing zero for very small numbers.
In Java, what can I do to increase the precision?

PS: I know BigInteger, but that obviously won't help me here.

Thanks for your help,

Wolfgang
Santa Barbara, CA

BigDecimal then???

Matt
 
A

Andrew Hobbs

Wolfgang said:
I'm trying to detect a very small difference in a math operation, like
this:
double c;
double a;
double b= 3e8;
double result = Math.sqrt(b*b + 100.0) - b;
System.out.println(b + " " + result);

This dispalys a result of

result: 1.7881393432617188E-7

If I increase b to say b=3e9 and greater, result goes to 0;

result: 0.0

I think it's just an underflow, showing zero for very small numbers.
In Java, what can I do to increase the precision?

I would have thought that your method was extremely precise. After all no
matter how many times you repeat the calculation you will get the same
answer. I think you mean that you want to increase your accuracy.

How about trying to simplify the expression. In this case

r = sqrt(b*b + 100) - b

ie r * ( r + 2b) = 100

since r is much smaller than b then essentially r = 100 / b.

As b gets larger your estimate of r gets more accurate.

If b = 3e9 then r is about 1 / 3e7 then the accuracy in your estimate of b
is 1/ 1e16

If you used that with BigDecimals you can calculate your answer as
accurately as you like.

Otherwise you could code a square root function using the simple
arithmetical methods in Big Decimal.

Andrew


--
********************************************************
Andrew Hobbs PhD

MetaSense Pty Ltd - www.metasense.com.au
Australia

61 8 9246 2026
metasens AntiSpam @iinet dot net dot au


*********************************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top