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

Re: Pre-RFC decimal data type

Thread Previous | Thread Next
From:
haj
Date:
August 5, 2021 16:53
Subject:
Re: Pre-RFC decimal data type
Message ID:
877dgzkf3p.fsf@hajtower
Ovid via perl5-porters <perl5-porters@perl.org> writes:

> 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 ) { ... }
>
> Fun fact: many COBOL-to-Java death marches have fallen down and not gotten back up because Java can't match COBOL's
> precision due to lacking COBOL's native decimal type (and simulating a Decimal type via classes meant that Java couldn't
> get the performance needed).

In current Perl, you can get rather close like this:

   perl -Mbignum=a,31 -E 'say (0 == 0.1 + 0.2 - 0.3)'  # prints 1
   perl -Mbignum=a,31 -E 'say (1 == 1/3 + 1/3 + 1/3)'  # prints ''

If decimals were in core instead of objects of a core library, they
could avoid the bignum pitfall (https://perldoc.perl.org/bignum#Caveats)
and also the overhead for objects and overloading.  Any further
performance improvement could be applied to bignum as well.

It might also encourage DB drivers to support the "decimal" data type
where the DB engine supports it.

For comparison:

COBOL's speed on mainframes comes from using an internal representation
(https://www.ibm.com/docs/en/i/7.4?topic=type-packed-decimal-format)
which is directly supported by hardware instructions operating on those
packed decimals.  The hardware instructions handle integers up to 31
digits, but placing the decimal point is a task for programming
languages.  Conversion from this format to and from strings is also
hardware supported and dead fast, the "zoned decimal format" happens to
be identical to the EBCDIC string.

C# uses a 96-bit integer which happens to have about the same numeric
range as 31 digits, plus flags for the sign and decimal point.

To add to the Java misery: If you retrieve a Java BigDecimal, e.g. from
a database, and want to add one, you need to create a new object:
value = value.add(new BigDecimal(1)).  Compare that to Perl's $value++.
-- 
Cheers,
haj


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