Search code examples
perl

What domain does the Perl floating point have?


Can somebody help me with this question, can't find any information about floating points domain in Perl programming language?


Solution

  • For a build of perl for the x86 or x64, it would be exceedingly rare to find anything that deviates from the following:

    • They can represent numbers as large as ≈ ±1.7976931348623157 × 10308.
    • They can represent numbers as small as ≈ ±4.9406564584124654 × 10−324.
    • They have 53 bits (≈ 16 digits) of precision.[1].
    • They can represent all integers up to and including ±253 (±9,007,199,254,740,992) without loss.

    Perl normally uses a C double, though I believe that can be changed when Perl is built.

    On an x86 or x64, a double is a IEEE double-precision floating-point number[2], which offers 53 bits of precision (almost 16 digits).

    Info provided by perl:

    $ perl -V:nvtype
    nvtype='double';
    
    $ perl -V:nvsize
    nvsize='8';
    
    $ perl -V:nv_preserves_uv_bits
    nv_preserves_uv_bits='53';
    
    $ perl -V:nv_overflows_integers_at
    nv_overflows_integers_at='256.0*256.0*256.0*256.0*256.0*256.0*2.0*2.0*2.0*2.0*2.0';
    
    $ perl -Mv5.10 -MConfig -e'say eval $Config{nv_overflows_integers_at}'
    9007199254740992
    

    These variables are available to programs through the %Config hash provided by the Config module.

    nv_overflows_integers_at is one less than the smallest integer that can't be represented by floating point numbers. Larger integers can be represented (some with and some without loss of precision), but every integer up to this number can be represented without loss of precision (on both the positive and negative side).


    1. The smallest of numbers (the "subnormals") only have 52 bits of precision.
    2. The C compiler could make a double something else, but that wouldn't make sense.