develooper Front page | perl.perl5.porters | Postings from August 2021

Re: Pre-RFC decimal data type

Thread Previous | Thread Next
From:
Nicholas Clark
Date:
August 5, 2021 09:36
Subject:
Re: Pre-RFC decimal data type
Message ID:
20210805093613.GK11066@etla.org
On Thu, Aug 05, 2021 at 10:18:39AM +0100, paul.marquess via perl5-porters wrote:
> From: Nicholas Clark <nick@ccl4.org> 
> > 
> > On Thu, Aug 05, 2021 at 08:17:22AM +0000, Ovid via perl5-porters wrote:
> > > Since we're talking about wishes, would a decimal type be interesting?
> For example, in floating point, you can't represent .3. So the following
> evaluates to false:
> > > 
> > > if ( 0 == .1 + .2 - .3 ) { ... }
> > 
> > Yes, it would be interesting, but I have no good idea how to implement
> this in a performant and portable way.
> 
> Use the Rational number implementation from Raku as inspiration? See
> https://docs.raku.org/type/Rat

I don't know how much that relies on the bytecode specialiser to get decent
performance. Also, I suspect that it's not as easy to convert "idiomatic"*
NQP into sane C code. Compared with (say) a design for C, C++ or a similar
compiled language build around structures and explicit memory management.

Also, it's a full-on rational numbers system - in Rakudo, I believe that
0.2 is stored as 1/5 (not 2/10), and all the implementation is built for
arbitrary rationals (not just base 10), *and* can deal with "BigRats" - ie
big integers for numerator or denominator.

Possibly more useful would be a decimal arithmetic library written in C,
with a suitable licence.

Also, as there (IIRC) there's now an IEEE spec for decimal types, which might
actually be more useful is decimal floating point, particularly if some CPUs
can actually do it in hardware.

(So floating point rather than either arbitrary precision decimal
arithmetic, or fixed point decimal arithmetic - it wasn't clear what Ovid
was thinking, and I don't know any COBOL so don't know what it does. But
all three options would make 0.1 + 0.2 - 0.3 be zero)

Nicholas Clark

* Rakudo is written in NQP, not Raku. And most of that NQP is written for
  speed, with knowledge of how MoarVM would compile it.
  "This week"
  And then gets changed when the optimiser changes. But maybe not until some
  "weeks" later.
  So it's not something beautiful and easy to learn from. It's not the swan
  gliding smoothly over the water. It's more the legs paddling furiously
  below the surface.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About