[squeakdev] Decimals as fractions
Nicolas Cellier
nicolas.cellier.aka.nice at gmail.com
Sun Apr 7 14:16:06 UTC 2019
Hi,
Yes sure, that's a possibility.
But that means changing well established Smalltalk syntax, and thus modify
a huge library of source code...
We currently have ScaledDecimal with 3.41s or 3.42s2 syntax.
The problem with ScaledDecimal is that their printing is ambiguous: two
different ScaledDecimal may print the same.
{1.0s2 / 3.0s2. 0.33s2} collect: #printString.
{1.0s2 / 3.0s2. 0.33s2} collect: #reciprocal.
They just truncate in Squeak (and maybe round in Pharo, not even with
banker rounding).
This is because they are just Fraction in disguise, and that's their second
problem: repeated computations could lead to monstruous fractions.
That's why I'm interested in experiments: is their usage sustainable, or
does it lead to explosion?
It's very easy to experiment: introduce a new NumberParser subclass and
connect it to Smalltalk Parser (via Scanner>>#xDigit).
Then, carefully recompile some part of the system, and discover the vital
source code where Float literals are required.
We could also introduce FixedPoint, a number that would round to a fixed
number of fractional digits. And maybe DecimalFixedPoint and
BinaryFixedPoint for the two principal usefull flavours.
The problem with FixedPoint is that we must decide in advance how many
digits?
A FixedPoint vs FloatingPoint is trading a bit more precision vs lot less
range, so it's maybe not that intersting for a general purpose library...
Or we could have a DecimalFloat, a Floating point in base 10.
https://en.wikipedia.org/wiki/Decimal_floating_point
Maybe that's the practical universal Number that we are after?
Of course, like the other kinds above, it would be emulated...
Le dim. 7 avr. 2019 à 05:10, K K Subbu <kksubbu.ml at gmail.com> a écrit :
> Hi,
>
> Squeak compiler compiles numbers like "3.4" into SmallFloat types. But
> Squeak can also handle number pairs like Point and Fractions. Has anyone
> considered decimals into fractions, instead? Like how we treat fractions
> in real life? $0.02 is really 2/100th of a dollar. I wouldn't want it to
> float ;).
>
> Squeak could compile fix points like "3.4" into fractions like "34/10"
> and compile into float only if an explicit exponent is given like in
> "3.4e0". This would help us maintain accuracy as long as possible in
> fraction arithmetic we use in daily life. e.g.
>
> 3.41  3.40 0.010000000000000231
> vs
> ((341/100)  (340/100)) asFloat 0.01
>
> Regards .. Subbu
>
 next part 
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/squeakdev/attachments/20190407/93fb59af/attachment.html>
More information about the Squeakdev
mailing list
