I'm having an issue with numeric overloading using Perl compiled with 64-bit integers. When the numerification of an object returns an integer > 10^15, Perl is coercing it to a floating-point number despite the fact that 64-bit integers can go over 10^19.

The following code illustrates:

#!/usr/bin/perl use strict; use warnings; package aaa; { use overload ( '""' => sub { return(1000000000000000) }, '0+' => sub { return(1000000000000000) }, 'fallback' => 1 ); sub new { return (bless({}, shift)); } } package bbb; { use overload ( '""' => sub { return(999999999999999) }, '0+' => sub { return(999999999999999) }, 'fallback' => 1 ); sub new { return (bless({}, shift)); } } package main; { print("Object overloaded as 16-digit integer\n"); my $a = aaa->new(); print("STRING: $a\n"); print('NUMBER: ', 0 + $a, "\n\n"); print("Object overloaded as 15-digit integer\n"); my $b = bbb->new(); print("STRING: $b\n"); print('NUMBER: ', 0 + $b, "\n\n"); print("Addition with 16-digit integer\n"); print('NUMBER: ', 0 + 1000000000000000, "\n"); }
It produces:
Object overloaded as 16-digit integer
STRING: 1000000000000000
NUMBER: 1e+15

Object overloaded as 15-digit integer
STRING: 999999999999999
NUMBER: 999999999999999

Addition with 16-digit integer
NUMBER: 1000000000000000
It's the '1e+15' above that I object to.

Update:Using int(0 + $a) does restore the result back to an integer, but that doesn't address my desire to negate the floating-point conversion in the first place.

Is there a way to modify this behavior? If not, I would think this should be reported as a bug, no?

Remember: There's always one more bug.

In reply to Numeric overloading with 64-bit Perl by jdhedden

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.