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 | » ]
Carlos Morales

Posts: 1
Nickname: cmorales
Registered: Aug, 2005

Re: What is the meaning of life? Posted: Dec 24, 2011 9:34 PM
Reply to this message Reply
Advertisement
I thought I had figured out the solution (reference to some famous work was a hint), however, after running the code on my Mac OS X, I found that such answer changes every time I run the program. Is this normal? :(

Merry Xmas!

Fred Finkelstein

Posts: 48
Nickname: marsilya
Registered: Jun, 2008

Re: What is the meaning of life? Posted: Dec 25, 2011 6:21 AM
Reply to this message Reply
>> I thought I had figured out the solution (reference
>> to some famous work was a hint), however, after
>> running the code on my Mac OS X, I found that such
>> answer changes every time I run the program.
>> Is this normal? :(

No, you should buy a new computer!
Just a joke :)
It has to do with
Math.random()
- every time the program starts a new seed is chosen and the numbers which are produced are different (with very high probability) from previous run. There lies also the solution to this weird program: If the value which is produced is very near to 1 - say 0,99999999... then something happens..

Fred Finkelstein

Posts: 48
Nickname: marsilya
Registered: Jun, 2008

Re: What is the meaning of life? Posted: Dec 25, 2011 6:28 AM
Reply to this message Reply
(correction to my previous post)

.. then something happens when certain conditions are met.

Geoffrey De Smet

Posts: 2
Nickname: ge0ffrey
Registered: Jul, 2011

Re: What is the meaning of life? Posted: Dec 26, 2011 5:05 AM
Reply to this message Reply
It's basically longer version of:
How much do you get if you add 9 cents and 1 cent?


public static void main(String[] args) {
System.out.println("0.09 + 0.01 = " + (0.09 + 0.01));
}

Geoffrey De Smet

Posts: 2
Nickname: ge0ffrey
Registered: Jul, 2011

Re: What is the meaning of life? Posted: Dec 26, 2011 5:11 AM
Reply to this message Reply
It's after christmas, so I guess we can post spoilers.

Below a list of all "evil double combinations" below a small treshold:
0.05 + 0.01 = 0.060000000000000005 not 0.06
0.06 + 0.01 = 0.06999999999999999 not 0.07
0.07 + 0.02 = 0.09000000000000001 not 0.09
0.07 + 0.04 = 0.11000000000000001 not 0.11
0.07 + 0.05 = 0.12000000000000001 not 0.12
0.08 + 0.07 = 0.15000000000000002 not 0.15
0.09 + 0.01 = 0.09999999999999999 not 0.1
0.09 + 0.08 = 0.16999999999999998 not 0.17
0.1 + 0.02 = 0.12000000000000001 not 0.12
0.1 + 0.05 = 0.15000000000000002 not 0.15
0.11 + 0.06 = 0.16999999999999998 not 0.17
0.11 + 0.1 = 0.21000000000000002 not 0.21
0.12 + 0.02 = 0.13999999999999999 not 0.14
0.12 + 0.05 = 0.16999999999999998 not 0.17
0.12 + 0.11 = 0.22999999999999998 not 0.23
0.13 + 0.08 = 0.21000000000000002 not 0.21
0.14 + 0.01 = 0.15000000000000002 not 0.15
0.14 + 0.04 = 0.18000000000000002 not 0.18
0.14 + 0.07 = 0.21000000000000002 not 0.21
0.14 + 0.08 = 0.22000000000000003 not 0.22
0.14 + 0.1 = 0.24000000000000002 not 0.24
0.15 + 0.02 = 0.16999999999999998 not 0.17
0.15 + 0.08 = 0.22999999999999998 not 0.23
0.15 + 0.14 = 0.29000000000000004 not 0.29
0.16 + 0.05 = 0.21000000000000002 not 0.21
0.16 + 0.13 = 0.29000000000000004 not 0.29
0.16 + 0.14 = 0.30000000000000004 not 0.3
0.17 + 0.01 = 0.18000000000000002 not 0.18
0.17 + 0.04 = 0.21000000000000002 not 0.21
0.17 + 0.05 = 0.22000000000000003 not 0.22
0.17 + 0.07 = 0.24000000000000002 not 0.24
0.17 + 0.12 = 0.29000000000000004 not 0.29
0.17 + 0.13 = 0.30000000000000004 not 0.3
0.17 + 0.14 = 0.31000000000000005 not 0.31
0.18 + 0.02 = 0.19999999999999998 not 0.2
0.18 + 0.05 = 0.22999999999999998 not 0.23
0.18 + 0.15 = 0.32999999999999996 not 0.33
0.18 + 0.16 = 0.33999999999999997 not 0.34
0.19 + 0.1 = 0.29000000000000004 not 0.29
0.19 + 0.15 = 0.33999999999999997 not 0.34
0.2 + 0.01 = 0.21000000000000002 not 0.21
0.2 + 0.04 = 0.24000000000000002 not 0.24
0.2 + 0.09 = 0.29000000000000004 not 0.29
0.2 + 0.1 = 0.30000000000000004 not 0.3
0.21 + 0.02 = 0.22999999999999998 not 0.23
0.21 + 0.12 = 0.32999999999999996 not 0.33
0.21 + 0.13 = 0.33999999999999997 not 0.34
0.21 + 0.2 = 0.41000000000000003 not 0.41
0.22 + 0.07 = 0.29000000000000004 not 0.29
0.22 + 0.12 = 0.33999999999999997 not 0.34
0.22 + 0.19 = 0.41000000000000003 not 0.41
0.22 + 0.2 = 0.42000000000000004 not 0.42
0.23 + 0.01 = 0.24000000000000002 not 0.24
0.23 + 0.06 = 0.29000000000000004 not 0.29
0.23 + 0.07 = 0.30000000000000004 not 0.3
0.23 + 0.18 = 0.41000000000000003 not 0.41
0.23 + 0.19 = 0.42000000000000004 not 0.42
0.23 + 0.2 = 0.43000000000000005 not 0.43
0.24 + 0.04 = 0.27999999999999997 not 0.28
0.24 + 0.09 = 0.32999999999999996 not 0.33
0.24 + 0.1 = 0.33999999999999997 not 0.34
0.24 + 0.17 = 0.41000000000000003 not 0.41
0.24 + 0.21 = 0.44999999999999996 not 0.45
0.24 + 0.22 = 0.45999999999999996 not 0.46
0.26 + 0.03 = 0.29000000000000004 not 0.29
0.26 + 0.15 = 0.41000000000000003 not 0.41
0.26 + 0.16 = 0.42000000000000004 not 0.42
0.26 + 0.17 = 0.43000000000000005 not 0.43
0.27 + 0.02 = 0.29000000000000004 not 0.29
0.27 + 0.03 = 0.30000000000000004 not 0.3
0.27 + 0.08 = 0.35000000000000003 not 0.35
0.27 + 0.14 = 0.41000000000000003 not 0.41
0.27 + 0.15 = 0.42000000000000004 not 0.42
0.27 + 0.16 = 0.43000000000000005 not 0.43
0.27 + 0.17 = 0.44000000000000006 not 0.44
0.27 + 0.2 = 0.47000000000000003 not 0.47
0.28 + 0.01 = 0.29000000000000004 not 0.29
0.28 + 0.02 = 0.30000000000000004 not 0.3
0.28 + 0.03 = 0.31000000000000005 not 0.31
0.28 + 0.07 = 0.35000000000000003 not 0.35
0.28 + 0.08 = 0.36000000000000004 not 0.36
0.28 + 0.13 = 0.41000000000000003 not 0.41
0.28 + 0.14 = 0.42000000000000004 not 0.42
0.28 + 0.15 = 0.43000000000000005 not 0.43
0.28 + 0.16 = 0.44000000000000006 not 0.44
0.28 + 0.17 = 0.45000000000000007 not 0.45
0.28 + 0.19 = 0.47000000000000003 not 0.47
0.28 + 0.2 = 0.48000000000000004 not 0.48
0.29 + 0.03 = 0.31999999999999995 not 0.32
0.29 + 0.04 = 0.32999999999999996 not 0.33
0.29 + 0.05 = 0.33999999999999997 not 0.34
0.29 + 0.11 = 0.39999999999999997 not 0.4
0.29 + 0.15 = 0.43999999999999995 not 0.44
0.29 + 0.16 = 0.44999999999999996 not 0.45
0.29 + 0.17 = 0.45999999999999996 not 0.46
0.29 + 0.28 = 0.5700000000000001 not 0.57
0.3 + 0.03 = 0.32999999999999996 not 0.33
0.3 + 0.04 = 0.33999999999999997 not 0.34
0.3 + 0.15 = 0.44999999999999996 not 0.45
0.3 + 0.16 = 0.45999999999999996 not 0.46

It's a good idea to use such numbers in unit tests, especially if your data gets marshalled to XML, JSON, database, file, ...

Roberto Martino

Posts: 3
Nickname: chairam
Registered: Feb, 2007

Re: What is the meaning of life? Posted: Dec 26, 2011 7:17 AM
Reply to this message Reply
It works but it should not compile (compiler bug). I expected :"Type mismatch: cannot convert from double to int". On the other side it's interesting to notice how many times the random number generator produce a number on the edge of the range.

Fred Finkelstein

Posts: 48
Nickname: marsilya
Registered: Jun, 2008

Re: What is the meaning of life? Posted: Dec 26, 2011 8:26 AM
Reply to this message Reply
> It works but it should not compile (compiler bug). I
> expected :"Type mismatch: cannot convert from double to
> int". On the other side it's interesting to notice how
> many times the random number generator produce a number on
> the edge of the range.

It's not only the edge. Try this:

System.out.println(25000000 + 0.99999999);
System.out.println(250000000 + 0.99999999);

The first statement produces the correct result:

2.500000099999999E7

But the second statement produces:

2.50000001E8

Because you have just a limited number of places for significant figures (http://en.wikipedia.org/wiki/Significant_figures) in the floating point number if the limit is reached rounding occurs. That's why the overall result is too big.

Morgan Conrad

Posts: 307
Nickname: miata71
Registered: Mar, 2006

Re: What is the meaning of life? Posted: Dec 26, 2011 9:42 PM
Reply to this message Reply
IEEE 754 doubles have 52 bits for the value, and integers have 32, so seems that some doubles MUST get rounded up.

I'm shocked, shocked that this way overly cute, way overly clever program that would not survive even one minute of a code review, may actually have a subtle bug in it.

Heinz Kabutz

Posts: 46
Nickname: drbean
Registered: May, 2003

Re: What is the meaning of life? Posted: Dec 27, 2011 12:25 AM
Reply to this message Reply
So here is a follow on question:

Given the current implementation of Random.nextDouble(), what is the probability of (int)(Math.random() + 1) returning 2?

robert young

Posts: 361
Nickname: funbunny
Registered: Sep, 2003

Re: What is the meaning of life? Posted: Dec 27, 2011 10:09 AM
Reply to this message Reply
It *should* be 0, since the defined range of Math.random() is open at 0 and 1.

Heinz Kabutz

Posts: 46
Nickname: drbean
Registered: May, 2003

Re: What is the meaning of life? Posted: Dec 27, 2011 10:17 AM
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?

Morgan Conrad

Posts: 307
Nickname: miata71
Registered: Mar, 2006

Re: What is the meaning of life? Posted: Dec 27, 2011 11:41 AM
Reply to this message Reply
First, ignoring the code to remove 0s, I get 420000009 for the meaning of life in the original code.

As a test of my Shannonese hypothesis (http://cm.bell-labs.com/cm/ms/what/shannonday/shannon1948.pdf) that the nine unexpected round-ups are related to the number of bits in a double vs. in an int, I changed the type of meaning to a long. My theory predicts that the number of "round-ups", which was 9, would decrease (or perhaps even go to zero, I haven't thought all that hard about it :-)).

With meaning as a long, I get 420000004. Only 4 unexpected roundups.

Heinz Kabutz

Posts: 46
Nickname: drbean
Registered: May, 2003

Re: What is the meaning of life? Posted: Dec 27, 2011 1:38 PM
Reply to this message Reply
Yes Morgan, but if you run it a few times with a long, you might get 9 again. Or you might get 4 with an int. It's random after all :-)

Morgan Conrad

Posts: 307
Nickname: miata71
Registered: Mar, 2006

Re: What is the meaning of life? Posted: Dec 27, 2011 2:42 PM
Reply to this message Reply
Heinz, the meaning of life may have some noise but its never random. :-)

Heinz Kabutz

Posts: 46
Nickname: drbean
Registered: May, 2003

Re: What is the meaning of life? Posted: Dec 27, 2011 2:52 PM
Reply to this message Reply
Pseudo random - it appears random but it's completely deterministic if you know the algorithm ;-)

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