Hi John, I was doing a similar rounding in the test assertion, I think your method is better and broadly usable (and maybe convenient for an #equals: implementation).
Thanks! Esteban A. Maringolo On Tue, Sep 1, 2020 at 4:33 AM John Aspinall <john.aspin...@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 <emaring...@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 <emaring...@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 > > > >