Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  difference in local and instance variable results...  (Read 2006 times)
0 Members and 1 Guest are viewing this topic.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« 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.5056001420551696E9
sum=3.5056001420551696E9
sum=3.5056001420551696E9
sum=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.5056001420551696E9
sum=2.789092624904878E9
sum=2.8371074309628153E9
sum=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
Offline Abuse

JGO Knight


Medals: 12


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
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« 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
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mr_Light

Senior Member




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.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


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

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

So... what happened?  Undecided


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 <---------------------------- WTF
sum=640.9012427900386
sum=621.0207516557817
sum=640.9012427900386
sum=640.9012427900386
sum=640.9012427900386
sum=629.505959999343
sum=640.9012427900386
sum=640.9012427900386
sum=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
Offline Mr_Light

Senior Member




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.  Tongue

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.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« 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.  Tongue

 Shocked

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
Offline ewjordan

Junior Member





« 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?
Offline Abuse

JGO Knight


Medals: 12


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 Undecided

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

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« 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. Shocked


hmm, my test was with 32bit XP.

I havae 32bit Vista.... but don't want to downgrade just to test this Undecided
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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (16 views)
2014-08-28 18:26:30

CopyableCougar4 (25 views)
2014-08-22 19:31:30

atombrot (38 views)
2014-08-19 09:29:53

Tekkerue (34 views)
2014-08-16 06:45:27

Tekkerue (32 views)
2014-08-16 06:22:17

Tekkerue (20 views)
2014-08-16 06:20:21

Tekkerue (30 views)
2014-08-16 06:12:11

Rayexar (66 views)
2014-08-11 02:49:23

BurntPizza (43 views)
2014-08-09 21:09:32

BurntPizza (34 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!