Main Topics
Browse All TopicsHi,
The result of the following expression is a bug?
(double)((float)1.53) = 1.5299999713897705
If works as designed, please tell me where i find the explanation.
Thx,
sey.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Whatever you do, you are always looking at - and verifying with String representations
of data that can not be expressed exactly.
So, before making up conclusions, realise what deviations are introduced
by formats, field capabilities, expression conversions, and finally the way a number is converted
into what you want to look at.
My professor told me long ago: "you can't really do math with a computer:
you are just jumping from bit pattern to bit pattern in a limited collection".
;JOOP!
None of the comments answered the question, which is about roundoff and why it happens, not on how to use BigDecimal when there is absolutely no reason to do so.
double precision takes 64 bits. It holds approximately 15 digits accuracy. It is encoded in binary, so numbers that are powers of 2 are exact:
.5
.25
.125
Numbers that are "not nice" fractions in decimal that we are used to have roundoff error in decimal. If you only remember 6 digits:
1/3 = .333333
which has an error, because there should be an infinite repeating string of 33333333....
In the same way, numbers that humans think of as nice, like 1/10 are repeating fractions in binary, which is how float and double are stored.
The original code given has a double precision constant:
double x = 1.53;
This is not actually exactly 1.53, as that number cannot be exactly represented in binary, but leave that for a moment. If you printed it, it would appear correct:
System.out.println(x);
Then, you cast to float, which I will split into a separate line:
float y = (float) x;
This truncates the answer to 32 bits, which only has 7-8 digits accuracy. Since the number is not exactly 1.53, now it is less accurate, having been chopped short. Then you copy it back as a double:
double x = y;
printing it out yields the wrong value.
There are a few lessons to be learned:
1. Don't use float unless you really know what you are doing.
2. Don't use float for financial calculations, there aren't enough digits precision: 123,456.78 (the 8 might be a 7 or a 9)
3. Don't cast intermediate results to float
Try this:
double x = 0;
for (int i = 0; i < 100; i++, x += 0.1) {
System.out.println(x);
}
After 30 or 40 iterations, suddenly there is enough cumulative roundoff that you start seeing a lot of digits.
You can stop the display by rounding to the nearest value you believe. Since we're adding .01, we know there shouldn't be any digits smaller than that:
double x = 0;
for (int i = 0; i < 100; i++, x += 0.1) {
System.out.println(Math.ro
}
Lesson:
4. Even double precision value are slowly poisoned by roundoff. double is more suitable to scientific use than business.
Integers in double are exact, so if you want to represent dollars in a double, multiply by 100, compute integer numbers of pennies, and only divide by 100 when printing.
>>
None of the comments answered the question, which is about roundoff and why it happens, not on how to use BigDecimal when there is absolutely no reason to do so.
>>
Then you haven't read the comments and links properly. The only way to get full control over rounding/precision issues is to use BigDecimal
Business Accounts
Answer for Membership
by: CEHJPosted on 2005-08-15 at 07:29:07ID: 14674688
Floating point values are approximations. If you want exactitude, use BigDecimal