Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# type range question

Bora Sabrioglu
Ranch Hand
Posts: 100
Hi all,

if you have

long var = 1000000 * 1000000;
System.out.println(var);

the result is -727379968 aus... but Long.MAX_VALUE is 9223372036854775807 ... so 1000000000000 would fit in there quite comfortably....

another strange thing is:

if you have

double var = 0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1-1.0;
System.out.println(var);

then the result is: -1.1102230246251565E-16 and not 0.0 as expected...

Can someone explain this?

Thanks!

Joanne Neal
Rancher
Posts: 3742
16
You are actually multiplying two ints together (which will produce an int result) and then putting that (truncated) value into a long.
You need to make one of the values a long.
Try
long var = 1000000 * 1000000L;
System.out.println(var);

Bora Sabrioglu
Ranch Hand
Posts: 100
oh i see, yes... thanks alot...

if you have

double var = 0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1+0.1-1.0;
System.out.println(var);

then the result is: -1.1102230246251565E-16 and not 0.0 as expected...

Joanne Neal
Rancher
Posts: 3742
16
That's a common question

Winston Gutkowski
Bartender
Posts: 10573
65
Joanne Neal wrote:That's a common question

And this is one of the best pages I know for explaining why it happens.
It's also the reason why you shouldn't use doubles or floats for calculations involving money.

Winston

Campbell Ritchie
Marshal
Posts: 55717
163
Bora Sabrioglu wrote: . . . -1.1102230246251565E-16 and not 0.0 as expected...
I ahve seen that before and did not expect to see 0.0. If you try log(1.1102230246251565E-16) ÷ log(2), you get -53.000. Remember that a double has 53 bits’ precision, so that suggests you are 1 bit out in the last digit, what is here called a ulp.
By the way: is that an ulp or a ulp?

Bora Sabrioglu
Ranch Hand
Posts: 100
thats right... if I delete one of the 0.1, then the result is -0.10000000000000009 ... thanks alot guys.

Campbell Ritchie
Marshal
Posts: 55717
163
You’re welcome

 It is sorta covered in the JavaRanch Style Guide.