Just bumped into this problem.

Some calculations seem to make a number become a "hidden" real number instead of an integer. The number prints as an integer, but after a substraction with another integer, the result is a real.

#!/usr/bin/perl

use strict; use warnings;

my ($x1, $x2) = (256080, 258160);
my $diff      = $x2 - $x1;

print "x1=$x1, x2=$x2, diff=$diff\n"; # OK

$x2 = 1000*240 + 1000*18 + 1000*( 4/25 ); 
$diff = $x2 - $x1;
print "x1=$x1, x2=$x2, diff=$diff\n"; # Also OK

$x2 = 1000 * ( 4*60 + 18 + 4/25 );
$diff = $x2 - $x1;
print "x1=$x1, x2=$x2, diff=$diff\n"; # $x2 is integer, but $diff is real!

The output is:

x1=256080, x2=258160, diff=2080
x1=256080, x2=258160, diff=2080
x1=256080, x2=258160, diff=2080.00000000003

Or the short one-liner version: perl -e '$x1=256080; $x2 = 1000 * ( 4*60 + 18 + 4/25 ); $diff=$x2-$x1; print "x1=$x1, x2=$x2, diff=$diff, Perl=$^V\n";'

I tried this on Perl versions v5.14.2, v5.18.2 and v5.20.2 with the same result. It is disconcerting that $x2 prints as "258160", but becomes something else when used in a substraction.


In reply to Integers sometimes turn into Reals after substraction by rduke15

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.