The Artima Developer Community
Sponsored Link

Weblogs Forum
What is the meaning of life?

57 replies on 4 pages. Most recent reply: Nov 14, 2012 10:21 AM by Heinz Kabutz

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 57 replies on 4 pages [ « | 1 2 3 4 | » ]
robert young

Posts: 361
Nickname: funbunny
Registered: Sep, 2003

Re: What is the meaning of life? Posted: Dec 27, 2011 5:28 PM
Reply to this message Reply
Advertisement
Run it more than once, you'll get (if my life imitates your art) different low order digits.

S. Fanchiotti

Posts: 10
Nickname: impatient
Registered: Nov, 2003

Re: What is the meaning of life? Posted: Dec 27, 2011 11:58 PM
Reply to this message Reply
He forgot to divide by 10 and take the integer part...

Not a fun guy to work with :-)

Heinz Kabutz

Posts: 46
Nickname: drbean
Registered: May, 2003

Re: What is the meaning of life? Posted: Dec 28, 2011 6:28 AM
Reply to this message Reply
Sometimes dividing the result by 10 would also not work.

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: What is the meaning of life? Posted: Jan 3, 2012 5:16 PM
Reply to this message Reply
> It happens to be zero, so Robert you are correct.
>
> However, this returns 2:
>
> public class DoubleD {
> public static void main(String[] args) {
> double d = 0.9999999999999999991234d;
> int i = 1;
> i += d;
> System.out.println(i);
> }
> }
>
> So the tricky question is - why is the probability zero in
> the current Random.nextDouble() implementation?

Without looking anything up or compiling the code, I'll wager a guess that the literal value has a greater precision than what can be represented in a double and therefore there is rounding in the compiler.

I'll also guess that Math.random() creates 'true' doubles that fall within the range allowed by the specification.

Valeriu Nedelcu

Posts: 2
Nickname: valined
Registered: Jan, 2012

Re: What is the meaning of life? Posted: Jan 10, 2012 4:17 AM
Reply to this message Reply
At first I thought (as most of you) that the answer is "42". The unexpected result can be explained only if one analyzes what happens when the JVM performs floating-point addition operations incurred by the expression
meaning += Math.random() + 1;


When doing the addition on the right-hand side, JVM first converts the 1 integer value to 1.0d FP-value (as described in JLS, ยง5.6.2). Nothing surprising here.

Although Math.random() theoretically returns always a FP-value strictly less than 1.0d, several values are so close to 1.0d that are actually "mapped" to 1.0d:

0.99999999999999999d
0.99999999999999998d
0.99999999999999997d
0.999999 99999999996d
0.99999999999999995d

All the above values have the same internal representation as 1.0d (raw bits):
11111111110000000000000000000000000000000000000000000000000000

So adding those values to 1.0 is giving 2.0 as a result, therefore increasing the final number over the expected value of 420000000.
That would explain an extra 5 added to the end result (assuming that the "special" values are picked only once). But for some executions (most?) of the program we can obtain as much as 420000009.

It turns out that although 0.99999999999999994d is rounded to the nearest value 0.99999999999999990d, adding it with 1.0 does not yield 1.99999999999999990d, but 2.0d. (In other words, there is no internal FP-value that could represent 1.99999999999999990d)

TL;DR:
There 11 values from 1.99999999999999989d to 1.99999999999999999d have no exact internal representation and are represented as 2.0d.

Valeriu Nedelcu

Posts: 2
Nickname: valined
Registered: Jan, 2012

Re: What is the meaning of life? Posted: Jan 10, 2012 7:16 AM
Reply to this message Reply
> Although Math.random() theoretically returns always a
> FP-value strictly less than 1.0d, several values are so
> close to 1.0d that are actually "mapped" to 1.0d:
>
> 0.99999999999999999d
> 0.99999999999999998d
> 0.99999999999999997d
> 0.99999999999999996d
> 0.99999999999999995d
>

> All the above values have the same internal representation
> as 1.0d (raw bits):
> 11111111110000000000000000000000000000000000000000000
> 000000000


I have given some more thoughts to this problem and now I realize that although my analysis is correct, it does not point to the real cause.

First of all, I don't think Math.random() would ever return the above "special" values (that would mean it would return in fact 1.0d, in contradiction with the API specs)

What happens is that sometimes the expression meaning + rand + 1 (where rand = Math.random()) has the same internal value as meaning + 2.0d due to the inherent loss of precision when performing FP addition between a large number and a very small one.

Heinz Kabutz

Posts: 46
Nickname: drbean
Registered: May, 2003

Re: What is the meaning of life? Posted: Jan 10, 2012 12:48 PM
Reply to this message Reply
Correct. (int)(Math.random() + 1) can never equal 2, because there are not enough bits available in the random calculation.

robert young

Posts: 361
Nickname: funbunny
Registered: Sep, 2003

Re: What is the meaning of life? Posted: Jan 10, 2012 2:30 PM
Reply to this message Reply
But the problem is clearly in the += implementation. As I mentioned above, doing the explicit cast removes the problem. So, it isn't rounding, per se, but the influence of the operator. Where or what that is, I know not.

Heinz Kabutz

Posts: 46
Nickname: drbean
Registered: May, 2003

Re: What is the meaning of life? Posted: Jan 10, 2012 2:54 PM
Reply to this message Reply
Robert, the += is simply translated as = (int)(val + ...)

Thus the += is not to blame, it just hides the problem of converting from double to int.

If you said "meaning += (int)(Math.random() + 1)"

It would also work, but mainly because of the way that Java calculates the random values.

However, "meaning = (int)(meaning + Math.random() + 1)" would have the same result as the +=

Regards

Heinz

Michael Makuch

Posts: 4
Nickname: koodawg
Registered: Nov, 2011

Re: What is the meaning of life? Posted: Jan 14, 2012 5:41 PM
Reply to this message Reply

public class mol {
public static void main(String argv[]) {
int i = 1;
double d = 0.99999999999999999;
int r = (int)(d + i);
System.out.println(r);
}
}

$ javac mol.java
$ java mol
2


Same thing occurs in C & C++;


#include <iostream>
int main() {
int i = 1;
double d = 0.99999999999999999999;
int r = i + d;
std::cout << r << std::endl;
return 0;
}
$ g++ -g -o mol mol.cpp
$ ./mol
2

Michael Makuch

Posts: 4
Nickname: koodawg
Registered: Nov, 2011

Re: What is the meaning of life? Posted: Jan 15, 2012 1:18 PM
Reply to this message Reply

Further experimenting;

public class testj {
public static void main(String argv[]) {
System.out.format("%20s %10s %20s %5s %5s\n", "i", "subtract", "nines", "sum0", "sum1");
long i = 1;
for(i = 1 ; i < 1000000000000000000L ; i *= 10) {
double subtractfrom1 = (double)1 / (double) i;
double nines = 1 - subtractfrom1;
int sum0 = 1;
int sum1 = 1;
sum0 += nines;
sum1 = sum1 + (int)nines;
System.out.format("%20s %10s %20s %5s %5s\n", i, subtractfrom1, nines, sum0, sum1);
}

}
}

$ javac testj.java
$ java testj
i subtract nines sum0 sum1
1 1.0 0.0 1 1
10 0.1 0.9 1 1
100 0.01 0.99 1 1
1000 0.0010 0.999 1 1
10000 1.0E-4 0.9999 1 1
100000 1.0E-5 0.99999 1 1
1000000 1.0E-6 0.999999 1 1
10000000 1.0E-7 0.9999999 1 1
100000000 1.0E-8 0.99999999 1 1
1000000000 1.0E-9 0.999999999 1 1
10000000000 1.0E-10 0.9999999999 1 1
100000000000 1.0E-11 0.99999999999 1 1
1000000000000 1.0E-12 0.999999999999 1 1
10000000000000 1.0E-13 0.9999999999999 1 1
100000000000000 1.0E-14 0.99999999999999 1 1
1000000000000000 1.0E-15 0.999999999999999 1 1
10000000000000000 1.0E-16 0.9999999999999999 2 1
100000000000000000 1.0E-17 1.0 2 2
$



** sorry, neither pre nor code nor java in square bracks will preserve spacing **

It appears there is a difference between += and +(int).

As another poster suggested it appears that when the floating point rep is maxed out it is "identical" to 1. Is this what's actually going on?

Heinz Kabutz

Posts: 46
Nickname: drbean
Registered: May, 2003

Re: What is the meaning of life? Posted: Jan 15, 2012 1:25 PM
Reply to this message Reply
Sure, the equivalent of "sum += nines" is "sum = (int)(sum + nines)"

robert young

Posts: 361
Nickname: funbunny
Registered: Sep, 2003

Re: What is the meaning of life? Posted: Jan 17, 2012 10:22 AM
Reply to this message Reply
I still disagree.

The reply I posted to your site demonstrated that:
a) += is coercion and fails
b) += (int) is casting and doesn't fail

nothing that's been said explains that. or did I not see this discussed?

Michael Makuch

Posts: 4
Nickname: koodawg
Registered: Nov, 2011

Re: What is the meaning of life? Posted: Jan 17, 2012 2:25 PM
Reply to this message Reply
> Sure, the equivalent of "sum += nines" is "sum = (int)(sum
> + nines)"

Ah yes, it is:

i subtract nines sum0 sum1
1000000000000000 1.0E-15 0.999999999999999 1 1
10000000000000000 1.0E-16 0.9999999999999999 2 2
100000000000000000 1.0E-17 1.0 2 2

Michael Makuch

Posts: 4
Nickname: koodawg
Registered: Nov, 2011

Re: What is the meaning of life? Posted: Jan 24, 2012 7:38 AM
Reply to this message Reply
Well Heinz, you posted this riddle, many people have chimed in, whats the final word as to why this is happening???

Flat View: This topic has 57 replies on 4 pages [ « | 1  2  3  4 | » ]
Topic: Service Locator Pattern Revisited, Part 2 Previous Topic   Next Topic Topic: Style is Substance


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us