<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">2015-05-02 22:32 GMT+02:00 Levente Uzonyi <span dir="ltr">&lt;<a href="mailto:leves@elte.hu" target="_blank">leves@elte.hu</a>&gt;</span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class=""><div class="h5">On Sat, 2 May 2015, Nicolas Cellier wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
<br>
2015-05-02 18:27 GMT+02:00 &lt;<a href="mailto:commits@source.squeak.org" target="_blank">commits@source.squeak.org</a>&gt;:<br>
      A new version of Kernel was added to project The Inbox:<br>
      <a href="http://source.squeak.org/inbox/Kernel-mtf.924.mcz" target="_blank">http://source.squeak.org/inbox/Kernel-mtf.924.mcz</a><br>
<br>
      ==================== Summary ====================<br>
<br>
      Name: Kernel-mtf.924<br>
      Author: mtf<br>
      Time: 2 May 2015, 12:26:05.675 pm<br>
      UUID: 7f688860-5202-4c0d-8ece-8723fe7317d6<br>
      Ancestors: Kernel-nice.923<br>
<br>
      Copied Number &gt;&gt; #round: from Pharo. One of 3 methods needed to make the Artefact pdf library work on squeak:<br>
      <a href="https://sites.google.com/site/artefactpdf/" target="_blank">https://sites.google.com/site/artefactpdf/</a><br>
<br>
      =============== Diff against Kernel-nice.923 ===============<br>
<br>
      Item was added:<br>
      + ----- Method: Float&gt;&gt;round: (in category &#39;truncation and round off&#39;) -----<br>
      + round: numberOfWishedDecimal<br>
      +         &quot;only leave a fixed amount of decimal&quot;<br>
      +         &quot;10.12345 round: 2 =&gt; 10.12&quot;<br>
      +<br>
      +         | v |<br>
      +         v := 10 raisedTo: numberOfWishedDecimal.<br>
      +         ^ ((self * v) rounded / v) asFloat<br>
      + !<br>
<br>
<br>
self assert: (Float fmax round: 2) = Float fmax.<br>
<br>
It&#39;s probably not very important for artefact, but if this method has a wider usage than artefact, then it should better be robust to overflow...<br>
 <br>
      Item was added:<br>
      + ----- Method: Fraction&gt;&gt;round: (in category &#39;truncation and round off&#39;) -----<br>
      + round: numberOfWishedDecimal<br>
      +       ^self asFloat round: numberOfWishedDecimal!<br>
<br>
<br>
Transforming exact arithmetic into inexact Float sounds like heresy to me ;)<br>
That&#39;s a questionable choice.<br>
Why this cult to Float would be necessary here?<br>
 <br>
      Item was added:<br>
      + ----- Method: Integer&gt;&gt;round: (in category &#39;truncation and round off&#39;) -----<br>
      + round: numberOfWishedDecimal<br>
      +       ^self!<br>
<br>
      Item was added:<br>
      + ----- Method: Number&gt;&gt;round: (in category &#39;truncation and round off&#39;) -----<br>
      + round: numberOfWishedDecimal<br>
      +       self subclassResponsibility!<br>
<br>
<br>
If it&#39;s subclass responsibility, then it must be implemented in ScaledDecimal.<br>
This is not necessary in Pharo, because ScaledDecimal has been moved under Fraction, but it is in Squeak.<br>
I&#39;d rather see Float implementation moved up (without the asFloat) and Float invoking super asFloat...<br>
<br>
I didn&#39;t check artefact, but I suspect that the usage is just to printShowingMaxDecimals: numberOfWishedDecimal, which would make the whole method<br>
moot.<br>
</blockquote>
<br></div></div>
The method was probably written by someone who&#39;s not aware of #roundTo:.`<br></blockquote><div><br></div><div>No, this is because &#39;x roundTo: (10 raisedTo: n) reciprocal&#39; is a bit worse than &#39;(x * (10 raisedTo: n) asFloat) rounded / (10 raisedTo: n) asFloat&#39;<br></div><div>Indeed, in the first one you perform one more inexact operation (1/100 ~= 0.01).<br><br></div><div>But anyway, these are vain efforts, because #round: in Pharo still cumulates several inexact operations and thus will sometimes fail to deliver the correct answer.<br><br></div><div>For example:<br></div><div>    self assert: 1.0005 &lt; (5/10000+1) ==&gt; ((1.0005 round: 3) = 1).<br></div><div>alas, it answers 1.001 and differs from printShowingMaxDecimalPlaces: 3...<br></div><div>Definitely not top quality.<br></div><div><br>I think Pharo did not integrate printShowingMaxDecimalPlaces: so that probably explains the mistake.<br></div><div>Or maybe they were influenced by Python round, but should have checked how it is implemented:<br></div><div>For example, see <a href="https://github.com/python/cpython/blob/c7688b44387d116522ff53c0927169db45969f0e/Python/pymath.c">https://github.com/python/cpython/blob/c7688b44387d116522ff53c0927169db45969f0e/Python/pymath.c</a><br></div><br>double round(double x) { double absx, y; absx = fabs(x); y = floor(absx); if (absx - y &gt;= 0.5) y += 1.0; return copysign(y, x); }<br><br>That should translate easily in Smalltalk if REALLY necessary...<br><br></div><div class="gmail_quote"><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
You&#39;re right about the printing thing in Artefact. I replaced #round: with #printOn:maxDecimalPlaces: in my image.<br>
<br></blockquote><div><br></div><div>Easy to guess, the only other possible use I can think of, is naive monetary apps; that makes two reasons to ban the message ;)<br></div><div><br><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Levente<br>
<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
Nicolas<br>
<br>
</blockquote>
<br><br>
<br></blockquote></div><br></div></div>