<div dir="ltr"><div>truncateTo: 0.01s or 1/100 is effectively less surprising... but is it good enough?</div><div><br></div><div>66.66 truncatesTo: 0.01s as 66.65s.</div><div>It might look surprising, bt it's not because 66.66 < (6666/100).</div><div>Does it always work?</div><div><br></div><div>99.99 < 99.99s.<br>-> true</div><div><br></div><div>so we might expect that it truncates to 99.98.s</div><div>99.99 truncateTo: 0.01s<br>-> 99.99s</div><div><br></div><div>Nope...</div><div>(0 to: 10000) count: [:i | (i/100) asFloat < (i/100) and: [((i/100) asFloat truncateTo: 0.01s) = (i/100)]].</div><div>-> 3720</div><div><br></div><div>For the other way (truncate to lower when it should not), I has to go to 5th digit, try this:</div><div><br></div><div>(1 to: 100000) select: [:i | (i/100000) asFloat >= (i/100000) and: [((i/100000) asFloat truncateTo: 0.00001s) < (i/100000)]].</div><div><br></div><div>The last one is very interesting:</div><div><br></div><div>1.0 truncateTo: 0.00001s</div><div>-> 0.99999s5</div><div><br></div><div>We have 1.0 = 1, so we're dealing with exact decimal fraction operands, and we could expect an exact result...</div><div>...but the intermediate 0.00001s asFloat used in (truncateTo:) does not.</div><div><br></div><div>One must use (aFloat asFraction truncateTo: 0.00001s) for exactness.<br></div><div><br></div><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Le jeu. 1 août 2019 à 18:20, Chris Cunningham <<a href="mailto:cunningham.cb@gmail.com">cunningham.cb@gmail.com</a>> a écrit :<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div class="gmail_attr" dir="ltr">On Wed, Jul 31, 2019 at 6:06 AM Levente Uzonyi <<a href="mailto:leves@caesar.elte.hu" target="_blank">leves@caesar.elte.hu</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;padding-left:1ex;border-left:1px solid rgb(204,204,204)">On Wed, 31 Jul 2019, David T. Lewis wrote:<br>
<br>
> On Wed, Jul 31, 2019 at 11:53:38AM +0000, Rein, Patrick wrote:<br>
>> Hi everyone,<br>
>> <br>
>> a student recently encountered the following behavior:<br>
>> <br>
>> fraction := 5 / 9.<br>
>> (fraction* 100) truncateTo: 0.01. ???55.550000000000004"<br>
>> fraction:= 1/18.<br>
>> (fraction * 100) roundTo: 0.01. ???5.5600000000000005"<br>
>> <br>
>> This is not what I would expect from #truncateTo: or #roundTo: even when using Floats (especially roundTo:).<br>
>> <br>
>> Is this what we want or an open issue? I have not found any test covering the protocol.<br>
>><br>
><br>
> The results are unexpected, but not wrong.<br>
><br>
> Why unexpected? When I look at the expression, I intuitively expect<br>
> it to perform decimal arithmetic, and it does not do that.<br>
><br>
> Why not wrong? 0.01 is a float, even though it may have been intended<br>
> as an exact decimal number by the writer. Mixing float values in any<br>
> arithmetic expression produces an inexact float result, as it should.<br>
><br>
> To get a result that is both correct and intuitively right, the expression<br>
> might better be written like this:<br>
><br>
>  5 / 9 * 100 truncateTo: (1/100) ==> (1111/20)<br>
<br>
I think ScaledDecimal is a better fit, as it gives a decimal result:<br>
<br>
5 / 9 * 100 truncateTo: 0.01s "==> 55.55s2"<br></blockquote><div><br></div><div>To be precise, it still gives you the same result as the fraction result, but it look like you would expect (i.e., it prints as a decimal, properly truncated).</div><div><br></div><div>But, yes, probably a better fit.</div><div><br></div><div>-cbc</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;padding-left:1ex;border-left:1px solid rgb(204,204,204)">
<br>
<br>
Levente<br>
<br>
><br>
> Dave<br>
<br>
</blockquote></div></div>
<br>
</blockquote></div>