Hi john you can propose for inclusion in pharo. - Add a nice comment (with executablecomments would be a plus). - Possibly some tests - Send a PR.
On 1 Sep 2020, at 09:32, John Aspinall <john.aspinall@gmail.com> wrote:
Dolphin shows the same behaviour. I added the following method to ScaledDecimal to help:
roundedByScale
| scaleFactor |
scaleFactor := 10 ** scale.
^(ScaledDecimal newFromNumber: (fraction * scaleFactor) rounded scale: scale) / scaleFactor
Translating slightly for Pharo:
roundedByScale
| scaleFactor |
scaleFactor := 10 ** scale.
^(ScaledDecimal newFromNumber: (self * scaleFactor) rounded scale: scale) / scaleFactor
You can then do:
(82 - (2 * 35.9) - (0 / 2) * (113/121) asScaledDecimal: 1) roundedByScale = 9.5s1 "true"
HTH.
John
On 1 Sep 2020, at 05:17, Esteban Maringolo <emaringolo@gmail.com> wrote:
As a follow up to this, adding a roundTo: before converting into a ScaledDecimal does not work.
(((91 - (2 * 35.9) - (0 / 2) * (113/121)) roundTo: 0.1) asScaledDecimal: 1) = 17.9s1
So how do you compare two ScaledDecimals that _should_ be the same?
Regards!
Esteban A. Maringolo
On Tue, Sep 1, 2020 at 1:07 AM Esteban Maringolo <emaringolo@gmail.com> wrote:
Hi,
I was doing some basic calculations based on a formula, and I wanted to convert the result to a scaled decimal in order to avoid having these "loose" decimals in 10th position or something similar.
So I did the following: 82 - (2 * 35.9) - (0 / 2) * (113/121) asScaledDecimal: 1 -> 9.5s1
But When I do this in a test: (82 - (2 * 35.9) - (0 / 2) * (113/121) asScaledDecimal: 1) = 9.5s1
It fails because the comparison returns false.
I guess this is the proper behavior, but I'd expected that the conversion from a Float to a scaled decimal would have eliminated the extra precision from the formula.
I can do a roundTo: 0.1 before converting, but I'd like to know what would be the proper way of dealing with this.
Thanks!
Esteban A. Maringolo
-------------------------------------------- Stéphane Ducasse http://stephane.ducasse.free.fr / http://www.pharo.org 03 59 35 87 52 Assistant: Aurore Dalle FAX 03 59 57 78 50 TEL 03 59 35 86 16 S. Ducasse - Inria 40, avenue Halley, Parc Scientifique de la Haute Borne, Bât.A, Park Plaza Villeneuve d'Ascq 59650 France