Hello, I found a bug in Squeak (3.0, #7067)... The following code returns true: (#(0.3 0.5 0.2) inject:
0.0 into: [:sum :proportion | sum + proportion]) = 1.0 But this following code returns false ! (#(0.3 0.6
0.1) inject: 0.0 into: [:sum :proportion | sum + proportion]) = 1.0 Fréd
-- Frédéric Pluquet Université Libre de Bruxelles (ULB) Assistant |
Actually we can skip some of the complexity
(0.3 + 0.6 + 0.1) = 1.0 -> false This is not a squeak problem, it's a problem with how floating point numbers are stored in binary floating point representation. http://en.wikipedia.org/wiki/Floating_point_number The 0.3 + 0.6 + 0.1 returns a number close to, but not quite 1.0 (0.3 + 0.6 + 0.1) hex '3FEFFFFFFFFFFFFF' and (0.3 + 0.5 + 0.2) hex '3FF0000000000000' and (1.0) hex '3FF0000000000000 so when you compare 3FEFFFFFFFFFFFFF to 3FF0000000000000 that returns (truefully) false. It's dangerous to compare floating point numbers, so consider instead. (0.3 + 0.6 + 0.1) closeTo: (0.3 + 0.5 + 0.2) -> true but you would need to look at the algorithm in closeTo: to decide if it's statistically meaningful to what you want. On Nov 15, 2007, at 11:06 PM, Frederic Pluquet wrote: > Hello, > > I found a bug in Squeak (3.0, #7067)... The following code returns > true: > > (#(0.3 0.5 0.2) inject: 0.0 > into: [:sum :proportion | sum + proportion]) = 1.0 > > > But this following code returns false ! > (#(0.3 0.6 0.1) inject: 0.0 > into: [:sum :proportion | sum + proportion]) = 1.0 > > > An idea ? > > Fréd > -- > Frédéric Pluquet > Université Libre de Bruxelles (ULB) > Assistant > -- = = = ======================================================================== John M. McIntosh <[hidden email]> Corporate Smalltalk Consulting Ltd. http://www.smalltalkconsulting.com = = = ======================================================================== |
Thank you for this perfect and complete answer !
Fréd
2007/11/16, John M McIntosh <[hidden email]>: Actually we can skip some of the complexity -- Frédéric Pluquet Université Libre de Bruxelles (ULB) Assistant |
Free forum by Nabble | Edit this page |