c# - Parallel.For not giving consistent results -
this question has answer here:
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
Post a Comment