in reply to -Duse64bitint and (Cygwin) perl 5.8.7

It can get tricky when you have the situation where your "int"s can hold more significant bits than your "double"s, but, of course, your doubles can deal with larger and smaller exponents than your ints. So, when you have two Perl numeric values to do an operation with, should you perform the operation as ints or as doubles?

Before 64-bit ints, the decision was simple; you do all math as doubles because the doubles can do everything the ints can do.

To counter your example, consider:

$x = 2 ** 45 + 1; $y = 2 ** 45 + 1.5; if($x + .5 == $y) {print "fine\n"} else {print "crap\n"} if($x == $y) {print "crap\n"} else {print "fine\n"} # Then $x = 1e55 + 1e44; $y = 1e55 + 2e44; # ...

Is Perl smart enough to tell that it needs to do your example with 64-bit ints but that it needs to do my examples with doubles? Obviously not. Can it be made that smart? Perhaps, but it might not be easy.

You could perlbug this to give the maintainers something to think about. But I hope you have a better appreciation for the difficultly here.

Note that you can help Perl know what you want here with a simple use integer;.

- tye        

  • Comment on Re: -Duse64bitint and (Cygwin) perl 5.8.7 (it's not how many bits, but how you use them)
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: -Duse64bitint and (Cygwin) perl 5.8.7 (it's not how many bits, but how you use them)
by syphilis (Archbishop) on Nov 30, 2006 at 08:38 UTC
    use integer

    That's the piece I was overlooking. I was expecting that, as with purely 32-bit arithmetic, the number would be an IV unless it was either a fraction, or was too big to fit into an IV. But that's not the case - an integer becomes an NV as soon as 53 bits are exceeded (even though the IV can accommodate 64 bits). Of course, with 'use integer;', the integer retains it's IV status, even when it exceeds 64 bits.

    Things start to make some sense ... though still appear a little buggy. (But maybe that's just because I need to readjust my vision :-)

    Cheers,
    Rob
Re^2: -Duse64bitint and (Cygwin) perl 5.8.7 (it's not how many bits, but how you use them)
by mersenne (Initiate) on Nov 30, 2006 at 16:37 UTC
    Is "use bignum;" an option? --->
    use bignum;
    use warnings;
    
    $x = 2**54 + 123; # 18014398509482107
    $y = 2**54 + 125; # 18014398509482109
    
    if($x + 2 == $y) {print "fine\n"}
    else {print "crap\n"}
    
    if($x == $y) {print "crap\n"}
    else {print "fine\n"}
    
    --->
    fine
    fine