[Cuis-dev] More on The Cuis Book

Martin McClure martin at hand2mouse.com
Sun Mar 5 17:34:32 PST 2023


On 3/5/23 16:51, Ignacio Sniechowski via Cuis-dev wrote:
> Float pi to: 5 by: 1/3 do: [:i | Transcript show: (i roundTo: 0.01) ; 
> space]⇒3.14 3.47 3.81 4.14 4.47 4.81
> *"***3.14 3.47 3.81 4.14 4.47 4.8100000000000005" the last number is 
> not rounded...
> What is especially intriguing is the last case...that 
> 4.8100000000000005 is not rounded.

Hi Nacho,

Ah, welcome the lovely and confusing world of decimal expressions of 
binary floating-point numbers.

What is not obvious here is that the last case /is/ rounded.

"roundTo: 0.01" requests that the result be an exact multiple of 0.01. 
But 0.01 is not exactly representable as a floating-point number, so the 
closest Float is used, and that float is a bit greater than 1/100.

0.01 asFraction > (1/100) ==> true

So we end up with a float that is 481 * 0.01, which is a little more 
than 481 / 100, /and/ there exists a different float which is closer to 
481 / 100. Every float has a unique default print string. The string 
'4.81' is used for that other float, so we have to use the string 
'4.8100000000000005' for the float that is the correct result of the 
expression.

Regards,
-Martin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20230305/1185d092/attachment.htm>


More information about the Cuis-dev mailing list