Forums Register Login

Question about floats in Java/in general

+Pie Number of slices to send: Send
Let's say I have the following 3 situations:

a)

b)

c)

Why is the output wrong by all 3 of them?
in a) the result should be 1, because 1e20/1e-20 is the same as 1/1 and its 1 but java outputs Infinity
in b) the expected result is 1.00000001e8 but java outputs 1.0ef. The same as the float c
in c) the expected result is 1.0000001e38, but it should be 1.1e38
+Pie Number of slices to send: Send
 

Why is the output wrong by all 3 of them?
in a) the result should be 1, because 1e20/1e-20 is the same as 1/1 and its 1 but java outputs Infinity
in b) the expected result is 1.00000001e8 but java outputs 1.0ef. The same as the float c
in c) the expected result is 1.0000001e38, but it should be 1.1e38


a) 1e20/1e-20 = 1e40, not 1
   1e20 is not the same as 1, nor is 1e-20 the same as 1.
But the real problem is that floating point numbers have only about a maximum of 6 significant figures.
See  http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
Do not confuse range, accuracy, and significant figures.

Try the same code using double instead of float and see what, if anything, changes. Do you understand why?
+Pie Number of slices to send: Send
Fred, the new version of the floating-point document you posted is here. Your link now points to a page which links to the original and also to what I posted, which the author thinks is better now.
+Pie Number of slices to send: Send
Don't use floats unless some other code requires them.
Every time you till, you lose 30% of your organic matter. But this tiny ad is durable:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 350 times.
Similar Threads
ArithmeticException
Dan's Operator C Exam
wrappers
Clrify results from SCJP mock exam site
float- arithmatic
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 02:10:55.