Java error on simple multiplication [duplicate] - java

This question already has answers here:
Retain precision with double in Java
(24 answers)
Closed 8 years ago.
I'm currently in the process of learning Java. I have just run the following line in Eclipse in Win7 using jre1.8.0_25:
System.out.println(4.5 * 7.9);
The console output is: 35.550000000000004
I'm just wondering why the output is wrong.

When looking for accuracy, doubles are not the best choice, as they are actually not as perfect as they may seem due to some limitations in their design.
See http://introcs.cs.princeton.edu/java/91float/

It's not only a Java issue (feature?), it's a common issue for all languages with floating point arithmetics. It's based on IEEE 754 standard, in case you're interested.

It's because of loss of precision due to operations made by the computer in binary. You can see an explanation of it here.

Related

How to use a large number in Java like 300 decimal digits or more [duplicate]

This question already has answers here:
Large Numbers in Java
(6 answers)
Closed 1 year ago.
I declared a long in Java code and it only take 19 decimal digits. I need a data type that takes over 300 decimal digits.
There is no primitive type that supports a number that long.
What you are searching for is most likely a BigInteger object. Calculations are done through the methods shown in the API.
As another user said, Javascript and Java are two different languages, please use the correct tag.

Inconsistent value in floating point operations JAVA [duplicate]

This question already has answers here:
Calculation problem in Java
(2 answers)
Why do I see a double variable initialized to some value like 21.4 as 21.399999618530273?
(14 answers)
How can I handle precision error with float in Java?
(9 answers)
Closed 9 years ago.
I don't know whether this question is asked before. I couldn't find it in searches.
System.out.println(1.00 - 9*.10);
the above statement prints out 0.09999999999999998 instead of simply 0.01
I know this is a well known problem in JAVA but i want to know the reason why it is so. Can someone guide me to the implementation details of float and explain the reason.
It's not a problem with java, it's a fundamental problem of the way floating point and double precision numbers are stored and processed in pretty much every language and on every processor.
Because of the way it stores the number it cannot (except in very rare cases) store the number precisely.
A full description of why can be found here: http://en.wikipedia.org/wiki/Single-precision_floating-point_format
You can use things such as BigDecimal to store any precision of number without this issue, however it will run much slower so you have a trade-off of precision vs performance.

Why is 65.12 - 2 not equal to 63.12 in java? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Floating point arithmetic not producing exact results in Java
I was recently working on a project when I came across a strange bug.
When 2 was subtracted from 65.12 the value was greater (not equal to) 63.12.
Here's the simplified code:
System.out.println(65.12-2);
And the output in the console:
63.120000000000005
I'm not sure why this is the case and if anyone knows a simple fix/workaround that would be great!
Thanks.
It has to do with the way floating-point values are handled by computers. The recommended text for fully understanding the topic is: What Every Computer Scientist Should Know About Floating-Point Arithmetic

# in java string instead of backslashes [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Raw Strings in Java?
In C# there is such a thing as # ("at sign") that can be put before string if forbidden symbols occurs. For example:
#"a\b\c"
In java I have to put backslashes
"a\\b\\c"
Is there any way in Java to make this easier?
Another way may be use equvivalent code for the symbols you want to escape.
Not really. I have made the transition not long ago and at first was constantly looking for "what is C#'s equivalent in Java for xyz?"
This is sometimes helpful but mostly frustrating. C# is a much more advanced language than Java and it will take a long time for Java to catch up.
You get used to it over time :-)

Android. 4.1 - 4 = 0.09999999999999964. Why? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Android number format is wrong somehow, instead of 3.5 I get 3.499999999, why?
Does any one know why Android platform gives such strange result?
4.1 - 4 = 0.09999999999999964
It's only 4 that gives such strange rounding.
Actually I need to get mantissa from 4.1 so I need 0.1 as result but not 0.09999999999999964.
Any ideas?
This is an issue of rounding. You can take a look at the BigDecimal class which should do what you need.

Categories

Resources