Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (575) Games in Android Showcase (154) games submitted by our members Games in WIP (623) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 difference in local and instance variable results...  (Read 2345 times) 0 Members and 1 Guest are viewing this topic.
Riven
« League of Dukes »

« JGO Overlord »

Medals: 954
Projects: 4
Exp: 16 years

 « Posted 2008-03-02 15:17:24 »

I have this code:

Code listing: A
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15 `            double sum = 0.0;            @Override            public void run()            {               int depth = 250;               for (int a = 0; a < depth; a++)                  for (int b = a + 1; b < depth; b++)                     for (int c = b + 1; c < depth; c++)                        for (int d = c + 1; d < depth; d++)                           sum += Math.sqrt(a + b + c + d);               System.out.println("sum=" + sum);            }`

Output
 1  2  3  4 `sum=3.5056001420551696E9sum=3.5056001420551696E9sum=3.5056001420551696E9sum=3.5056001420551696E9`

Code listing: B
 1  2  3  4  5  6  7  8  9  10  11  12  13  14 `            @Override            public void run()            {               int depth = 250;               double sum = 0.0;               for (int a = 0; a < depth; a++)                  for (int b = a + 1; b < depth; b++)                     for (int c = b + 1; c < depth; c++)                        for (int d = c + 1; d < depth; d++)                           sum += Math.sqrt(a + b + c + d);               System.out.println("sum=" + sum);            }`

Output
 1  2  3  4 `sum=3.5056001420551696E9sum=2.789092624904878E9sum=2.8371074309628153E9sum=2.806383173404868E9`

Note: these are 4 threads concurrently running on a quad-core machine (all in their own Runnable instance, ofcourse).
 1  2  3  4  5  6  7  8  9 `      for (int i = 0; i < 4; i++)      {         Runnable task = new Runnable()         {             // impl         };         new Thread(task).start();      }`

As you can see, the only difference is that the 'sum' variable has another scope (local vs. instance)

I realize memory-access works different for them, and the bytecode is very different, but... I thought either both should be deterministic, or both should be unpredictable due to floatingpoint being slightly non-deterministic in x86 / x64...

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Abuse

JGO Knight

Medals: 17

falling into the abyss of reality

 « Reply #1 - Posted 2008-03-02 17:12:56 »

Seems to work fine for me (Core 2 Quad, Q6600),

All 4 results for both tests give "sum=3.5056001420551696E9" everytime.

(Using JRE 1.6.0_10-ea)

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Riven
« League of Dukes »

« JGO Overlord »

Medals: 954
Projects: 4
Exp: 16 years

 « Reply #2 - Posted 2008-03-02 17:20:40 »

That worries me... this is a brand new PC... (btw, also a Q6600)

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Mr_Light

Senior Devvie

Medals: 1

shiny.

 « Reply #3 - Posted 2008-03-03 02:21:55 »

Quote
same here, consistent

I also naively would have guessed the one with instance variable would be inconsistent, not the local one if there would be any difference.(T5500)

Hm... I ran a Memtest, and everything was fine... I ran the 32-bit VM (jre160_04) on a 64-bit OS (Vista) ... I might do some other tests later today.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Riven
« League of Dukes »

« JGO Overlord »

Medals: 954
Projects: 4
Exp: 16 years

 « Reply #4 - Posted 2008-03-03 07:17:17 »

Ha! The 64bit version of JRE160_04 produced 'stable' results!

So... what happened?

I also tried:
 1  2  3  4  5  6 `        double sum = 0.0f;               for (int a = 0; a < depth; a++)                  for (int b = a + 1; b < depth; b++)                     for (int c = b + 1; c < depth; c++)                        for (int d = c + 1; d < depth; d++)                           sum += (a+b+c+d)/123456789.0;//Math.sqrt(a + b + c + d);`

 1  2  3  4  5  6  7  8  9  10 `sum=85.20330685445509 <---------------------------- WTFsum=640.9012427900386sum=621.0207516557817sum=640.9012427900386sum=640.9012427900386sum=640.9012427900386sum=629.505959999343sum=640.9012427900386sum=640.9012427900386sum=640.9012427900386`

WAY worse than rounding errors!!

This is: JRE160_04 32bit, 64bit Vista, Q6600

JRE160_04 64bit works fine! (all: sum=640.9012427900386)

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Mr_Light

Senior Devvie

Medals: 1

shiny.

 « Reply #5 - Posted 2008-03-03 07:49:15 »

don't scare me like that riven!

Hm... I ran a Memtest, and everything was fine... I ran the 32-bit VM (jre160_04) on a 64-bit OS (Vista) ... I might do some other tests later today.

« Last Edit: Today at 11:02:08 pm by Riven »

I am sleepy, since I'm having trouble sleeping. But that just can't be something I've said: unless: I installed vista tested stuff AND magicly got back to xp whilst sleep walking seemed pretty impossible.

The idea that you would have hit edit instead of quote saved my insanity.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Riven
« League of Dukes »

« JGO Overlord »

Medals: 954
Projects: 4
Exp: 16 years

 « Reply #6 - Posted 2008-03-03 07:56:24 »

don't scare me like that riven!

I am sleepy, since I'm having trouble sleeping. But that just can't be something I've said: unless: I installed vista tested stuff AND magicly got back to xp whilst sleep walking is pretty impossible.

The idea that you would have hit edit instead of quote saved my insanity.

I think my parallel universe just got out of sync! Might explain the other anomalies...

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
ewjordan

Junior Devvie

 « Reply #7 - Posted 2008-03-03 20:33:07 »

I can't check this stuff right now on my machine, but I'm curious - are the inconsistent results at least consistent from run to run, or do they vary as well?
Abuse

JGO Knight

Medals: 17

falling into the abyss of reality

 « Reply #8 - Posted 2008-03-03 22:40:09 »

hmm, my test was with 32bit XP.

I havae 32bit Vista.... but don't want to downgrade just to test this

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Riven
« League of Dukes »

« JGO Overlord »

Medals: 954
Projects: 4
Exp: 16 years

 « Reply #9 - Posted 2008-03-04 01:09:55 »

I can't check this stuff right now on my machine, but I'm curious - are the inconsistent results at least consistent from run to run, or do they vary as well?
Highly random! But tends to stabilize the longer it runs.

hmm, my test was with 32bit XP.

I havae 32bit Vista.... but don't want to downgrade just to test this
Apart from stating the obvious by saying that downgrading is the future, I can only wait for people here with x64 Vista running a x86 JRE and give it a whirl...

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 BurntPizza (28 views) 2015-04-23 03:42:11 theagentd (32 views) 2015-04-22 16:23:07 Riven (45 views) 2015-04-16 10:48:47 Duke0200 (54 views) 2015-04-16 01:59:01 Fairy Tailz (39 views) 2015-04-14 20:13:12 Riven (42 views) 2015-04-12 21:36:37 bus hotdog (58 views) 2015-04-10 02:39:32 CopyableCougar4 (63 views) 2015-04-10 00:51:04 BurntPizza (62 views) 2015-04-06 22:06:58 ags1 (62 views) 2015-04-02 10:58:48
 theagentd 23x BurntPizza 16x wessles 15x alwex 11x 65K 11x kingroka123 11x Rayvolution 8x kevglass 8x Roquen 7x chrislo27 7x Hanksha 7x Riven 7x Olo 7x ra4king 7x Ecumene 7x KevinWorkman 7x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org