<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div class="moz-cite-prefix">On 3/5/23 16:51, Ignacio Sniechowski
via Cuis-dev wrote:<br>
</div>
<blockquote type="cite"
cite="mid:CAMCzT-ZLLbCeZfke8fa5BYjmU2fugzQp2pzE6Ln29uqMoeyTTQ@mail.gmail.com">
<div class="gmail_default"
style="font-family:monospace;font-size:large"><span
style="color:rgb(0,0,0);font-size:9pt;font-family:CMTT9">Float
pi to: 5 by: 1/3 do: [:i | Transcript show: (i roundTo: 0.01)
; space]<span class="gmail-Apple-converted-space"> </span></span><span
style="color:rgb(0,0,0);font-size:9pt;font-family:CMSY9">⇒<span
class="gmail-Apple-converted-space"> </span></span><span
style="color:rgb(0,0,0);font-size:9pt;font-family:CMTT9">3.14
3.47 3.81 4.14 4.47 4.81</span> </div>
<div>
<div dir="ltr" class="gmail_signature"
data-smartmail="gmail_signature">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div dir="ltr">
<div><b><font size="2"><span
class="gmail_default"
style="font-family:monospace;font-size:large">"</span></font></b><b><font
size="2"><span
class="gmail_default"
style="font-family:monospace;font-size:large"></span></font></b>3.14
3.47 3.81 4.14 4.47
4.8100000000000005<span
class="gmail_default"
style="font-family:monospace;font-size:large">"
the last number is not
rounded...</span></div>
<div><span class="gmail_default"
style="font-family:monospace;font-size:large">What
is especially intriguing is the
last case...that
4.8100000000000005 is not
rounded.</span></div>
<div><span class="gmail_default"
style="font-family:monospace;font-size:large"></span></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
<br>
Hi Nacho,<br>
<br>
Ah, welcome the lovely and confusing world of decimal expressions of
binary floating-point numbers. <br>
<br>
What is not obvious here is that the last case <i>is</i> rounded. <br>
<br>
"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. <br>
<br>
0.01 asFraction > (1/100) ==> true<br>
<br>
So we end up with a float that is 481 * 0.01, which is a little more
than 481 / 100, <i>and</i> 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.<br>
<br>
Regards,<br>
-Martin<br>
</body>
</html>