c# - Parallel.For not giving consistent results -


when run code below result different when comes 5th , 6th digit:

public void testparallel()     {         object locker = new object();         double grandtotal = 0;         parallel.for( 1, 1000000,              () => 0.0, // initialize local value.              ( i, state, localtotal ) => // body delegate. notice             {                 return localtotal + math.sqrt( ); // returns new local total.             },              localtotal => // add local value             {                 lock( locker ) grandtotal += localtotal; // master value.             }             );         console.writeline( string.format( "grand total = {0}", grandtotal ) );     } 

i have no clue why result not grand total = 666666166,458842. when running this:

double grandtotal = 0; ( int = 1; < 1000000; i++ ) {     grandtotal += math.sqrt( ); // returns new local total. } console.writeline( string.format( "grand total = {0}", grandtotal ) ); 

with floating point calculations, 1.0 + 0.9 doesn't equal 0.9 + 1.0


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -