mins
mins

Reputation: 7484

Rounding of very large floats in Java

Java:

float big = (float) 1e12;
float ulp = Math.ulp(big);

float result = (big + 2/3*ulp) - big;

result is 0.0, while I was expecting ulp (65536.0). Can somebody explain why?

Upvotes: 0

Views: 117

Answers (3)

Rahul Tripathi
Rahul Tripathi

Reputation: 172378

You may try this:

float result = (big + 2f/3*ulp) - big;

ie, you need to typecast the division values else integer/integer will result to zero.

Upvotes: 3

Jry9972
Jry9972

Reputation: 473

Try typecasting either 2 or 3 to float and try, as 2/3=0, thats why result=0.0

Upvotes: 1

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 135992

This is because 2/3 = 0 (integer division), try 2.0 / 3

Upvotes: 3

Related Questions