Rounding off issue in java [duplicate] - java

This question already has answers here:
Is floating point math broken?
(31 answers)
How to resolve a Java Rounding Double issue [duplicate]
(13 answers)
Closed 3 years ago.
I am using math.round() to round off.
this is the code:
double value=0.145
Math.round( value* 100.0) / 100.0;
My expectation is it should return 0.15 but it is returning 0.14.
Also, if the value is 0.055, then it is returning 0.05 instead of 0.06
I have already gone through the link round up to 2 decimal places in java?
Please tell me specific that why this is happening?
Thanks in Advance.

You may use Math.ceil(0.145) to get your desired result 0.15.

If you would print value * 100.0, you would find that this is not actually 14.5 as you would expect, but it's close to 14.499999999999998 (on my jdk1.8.0_181). As you can see, it would give you 0.14 as final result.
Why it happened, this is how floating point arithmetic is supposed to work. When you have a value that cannot be properly represented by binary system, you will face this problem. I am giving links to answers that gives good information why it happens.
https://stackoverflow.com/a/1089026/841221
https://stackoverflow.com/a/8604224/841221
EDIT : Update 1
As matt suggested, it's actually 0.145 which cannot be represented by binary properly. As per IEEE754 Double Precision 64-Bit representation, it's actually is 1.44999999999999990007992778374E-1 which is not exactly 0.145.

Related

How do I round a double to the thirds place? [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 2 years ago.
double1 = .05100000000003;
double2 = .05295;
How do I round a decimal to three places? Respectively, I'd like the output to do this 0.051 and 0.053.
I tried Math.round(double1); but I got back 0.0.
Any ideas would be greatly appreciated thanks.
Try the following code in Java. The number of zeros indicates how many decimal places you would like to round to. (In this case there are 3 zeros).
(double)Math.round(value * 1000d) / 1000d
EDIT: same answer is provided in this previous StackOverflow Post

Java - Math.asin() only returns 0 [duplicate]

This question already has an answer here:
Division between integers in Java
(1 answer)
Closed 3 years ago.
I've been struggling with Math.asin() for a while.
System.out.println(Math.toDegrees(Math.asin(1 / 2)));
This very simple line of code pretty much sums up my desperation: The result should be 30°, however, I only get 0.0 as a result. Its not just these numbers, no matter which numbers I use, the result is 0.0.
My question is, is this a known bug of Java, or am I missing something?
Bonus Information:
I need asin() to calculate the angle between the centerpoints of two objects in my game.
You’re doing integer division, please add a decimal point in the end of each number like this:
System.out.println(Math.toDegrees(Math.asin(1.0/2.0)))

Rounding off issue clarification in java [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 3 years ago.
I am using Math.round(). This is the sample code:
double value = 0.14499999970197677;
value = Math.round( value* 100.0) / 100.0;
My expectation is it should return 0.15 but it is returning 0.14
Also, if the value is 0.13499999970197677, then it is returning 0.13, why not 0.14
I have already gone through the link round up to 2 decimal places in java?
Please tell me clearly that why this is happening with both the numbers?
In Java, Math.round rounds up/down to closest long value (in mathematics terms, closest whole number).
14.49 will be rounded to 14 (because it is treated as 14.4). It won't take the .09 into account.

Double data type not returning desired output in java [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 8 years ago.
In Java, for the following code
Double d = 2.0-1.1;
System.out.println(d);
The result is
0.8999999999999999
If the program is dealing with sensitive information such as precentile/money or cents how do I solve this problem?
I also tried the following piece of code:
new BigDecimal(d)
which outputs
0.899999999999999911182158029987476766109466552734375
What should I do to get 0.90 for the above case?
Since double cannot accurately represent the result of 2 - 1.11, the precision has already been lost by the time the constructor is used.Therefore you need to chain BigDecimal using the String based constructor
BigDecimal result = new BigDecimal("2").subtract(new BigDecimal("1.1"));
The Standard Reference is What Every Computer Scientist Should Know About Floating-Point Arithmetic
A more digestable read: Floating Point Numbers

Floating point number representation, Java example [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 9 years ago.
Could you please explain, why I got next result:
when I run this:
System.out.println((0.2 - 0.1));
I got: 0.1
when I run this:
System.out.println((0.3 - 0.2));
I got: 0.09999999999999998
I know that number "0.1" doesn't have finite representation in binary, but it doesn't explain the results above. Most likely this is not about particular language but about how digits are stored in computer.
Java uses IEEE floating point to represent double values. It is not a precise representation, and some calculations result in tiny errors that manifest themselves in this way.
I agree with Bohemian above (float and double is not precise) so you will get oddities like this
but there is a solution for your problem:
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(1);
nf.format(0.3f - 0.2f);
This will produce 0.1.

Categories