[Cuis-dev] More on The Cuis Book

Martin McClure martin at hand2mouse.com
Mon Mar 6 11:55:18 PST 2023


Hi Nacho,

If what you want is to produce a rounded /print string/, rather than a 
rounded/number/, one can use a formatted printing message, such as 
#printShowingDecimalPlaces:.

Regards,
-Martin

On 3/6/23 04:03, Ignacio Sniechowski wrote:
> Thanks for your response, Martin!
> I thought that #roundTo: only affected representation, but it seems 
> that there are some caveats when applied to floatings.
> I've experimented with 0.1, 0.01, and 0.001, and this 
> misrepresentation affects other numbers in the interval. So the lesson 
> would be: be careful with #roundUp: & similar methods when using 
> floating as the imagined or expected representation could fail.
>
> Thanks again
> Nacho
>
>
> *Ignacio Sniechowski**
> *
>
> *
> *
>
> *
> *
> *
> *
> *
> *
> *
> *
>
> *
> *
> *
> *
> *
> *
> *
>
> **
> **
> *
>
>
>
>
>
> On Sun, Mar 5, 2023 at 10:34 PM Martin McClure <martin at hand2mouse.com> 
> wrote:
>
>     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/20230306/7e8e0454/attachment.htm>


More information about the Cuis-dev mailing list