swarm-modeling
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Swarm Futures re-cap


From: gepr
Subject: Re: Swarm Futures re-cap
Date: Tue, 15 Oct 2002 11:27:06 -0700

I think what you're dealing with is the conversion operation of the
print function.  Since %f assumes a double and %g and %e assume
long doubles, you're going to run into these problems because it
scarfs whatever trash happens to surround the thing you're printing.

Check out the attached program and its results.  Basically, the
problem is not floating point arithmetic... it's the proper use
of constants.

Gary Polhill writes:
 > Now that I've just found a bug in our model that I've traced back to
 > floating point arithmetic [e.g. (9 * 0.4) - (6 * 0.6) = 4.44089E-16,
 > whilst 0.4 + 0.4 + 0.4 + 0.4 + 0.4 + 0.4 + 0.4 + 0.4 + 0.4 - 0.6 - 0.6
 > - 0.6 - 0.6 - 0.6 - 0.6 = -6.66134E-16 -- not even consistent in which
 > side of 0 it is... plus, 0.4 + 0.4 + 0.4 - 0.6 + 0.4 - 0.6 + 0.4 + 0.4
 > + 0.4 + 0.4 + 0.4 - 0.6 - 0.6 - 0.6 - 0.6 = -2.22045E-16, so even the
 > order matters!], I wonder if one of the standard libraries Swarm
 > should provide is some kind of proper treatment of floating point
 > arithmetic, or perhaps even representing numbers as Integer, Rational,
 > Surd and Real classes somehow. All I know is I'm never trusting
 > floating points again (these were double "precision", too!).
 > 
 > I'd be interested to hear how other people deal with floating
 > points. Some kind of tolerance windows could be used, but, for example
 > (0.4 * 50331648.0) - (0.6 * 33554432.0) = 3.72529E-9, so it's not even
 > immediately obvious what order of magnitude such tolerances should
 > have! You would also then need to somehow be sure that the smallest
 > number you could generate given your parameters was always greater
 > than this tolerance. I am not at all convinced that such certainty can
 > be obtained, but I do feel sure that others have come across and dealt
 > with this issue before.
 > 
 > Gary 

-- 
glen e. p. ropella              =><=                           Hail Eris!
H: 831.335.4950                              http://www.ropella.net/~gepr
M: 831.247.7901                               http://www.tempusdictum.com

int
main()
{
   double val1 = (9 * 0.4) - (6 * 0.6);
   double val2 = (9L * 0.4L) - (6L * 0.6L);
   long double val3 = (9 * 0.4) - (6 * 0.6);
   long double val4 = (9L * 0.4L) - (6L * 0.6L);



   printf("\nsizeof(float) = %d  sizeof(double) = %d  sizeof(long double) = 
%d\n",
          sizeof(float), sizeof(double), sizeof( long double));


   printf("\n(%%g, %%f, %%e, %%#x)\n");
   printf("\nval1 [(9 * 0.4) - (6 * 0.6)] = \n\t(%g, %f, %e, 0x%#012x)\n", 
          val1, val1, val1, val1);
   printf("\nval2 [(9L * 0.4L) - (6L * 0.6L)] = \n\t(%g, %f, %e, 0x%#012x)\n", 
          val2, val2, val2, val2);

   printf("\nval3 [(9 * 0.4) - (6 * 0.6)] = \n\t(%g, %f, %e, 0x%#012x)\n", 
          val3, val3, val3, val3);
   printf("\nval4 [(9L * 0.4L) - (6L * 0.6L)] = \n\t(%g, %f, %e, 0x%#012x)\n", 
          val4, val4, val4, val4);


   return 0;
}

reply via email to

[Prev in Thread] Current Thread [Next in Thread]