Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (404)
games submitted by our members
Games in WIP (289)
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 1539 times)
0 Members and 1 Guest are viewing this topic.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 437
Projects: 4


Hand over your head.


« Posted 2008-03-02 16: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
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline Abuse

JGO Coder


Medals: 2


falling into the abyss of reality


« Reply #1 - Posted 2008-03-02 18: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!
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 437
Projects: 4


Hand over your head.


« Reply #2 - Posted 2008-03-02 18: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
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline Mr_Light

Senior Member




shiny.


« Reply #3 - Posted 2008-03-03 03: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: 437
Projects: 4


Hand over your head.


« Reply #4 - Posted 2008-03-03 08: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
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline Mr_Light

Senior Member




shiny.


« Reply #5 - Posted 2008-03-03 08: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: 437
Projects: 4


Hand over your head.


« Reply #6 - Posted 2008-03-03 08: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
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline ewjordan

Junior Member





« Reply #7 - Posted 2008-03-03 21: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 Coder


Medals: 2


falling into the abyss of reality


« Reply #8 - Posted 2008-03-03 23: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!
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 437
Projects: 4


Hand over your head.


« Reply #9 - Posted 2008-03-04 02: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
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (53 views)
2013-05-17 21:29:12

alaslipknot (62 views)
2013-05-16 21:24:48

gouessej (93 views)
2013-05-16 00:53:38

gouessej (90 views)
2013-05-16 00:17:58

theagentd (100 views)
2013-05-15 15:01:13

theagentd (92 views)
2013-05-15 15:00:54

StreetDoggy (138 views)
2013-05-14 15:56:26

kutucuk (160 views)
2013-05-12 17:10:36

kutucuk (159 views)
2013-05-12 15:36:09

UnluckyDevil (169 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.109 seconds with 20 queries.