Hello Monks,

I get different results for formatted printing of big numbers between Slackware 13.37 x86_64 (real PC):
  - perl 5, version 12, subversion 3 (v5.12.3) built for x86_64-linux-thread-multi
and all three of:
Slackware 13.37 32bit (running as a VMWare Player guest)
  - perl 5, version 12, subversion 3 (v5.12.3) built for i486-linux-thread-multi
Windows XP Home Edition Service Pack 3 (build 2600) 32bit (running as a VMWare Player guest)
  - (ActivePerl) perl 5, version 12, subversion 4 (v5.12.4) built for MSWin32-x86-multi-thread
  - (Strawberry Perl) perl 5, version 12, subversion 3 (v5.12.3) built for MSWin32-x86-multi-thread

I suppose it's related to "use bignum;" and 32bit.

The results on the 32bit OSs print correctly for big numbers when using a simple "print" statement. But when I try formatted printing using printf, or using print on a string produced by sprintf, then the values are wrong for big numbers (> 2**32 - 1). The problem occurs with all non-floating point formats I tried: binary (%b), octal (%o), hex (%h) and int (%u).
I also printed out in scientific (%e) and floating point (%f) - they are correct.

Code and results are below. Anybody any ideas?

Cheers, Peter

#!/usr/bin/perl use warnings; use strict; use bignum; my $i = 10; &format_and_print; $i = 4300000000; &format_and_print; sub format_and_print { print "i: $i\n"; my $ibytes = sprintf("%b", $i); print "print ibytes : $ibytes\n"; printf("printf ibytes: %s\n", $ibytes); my $ioctal = sprintf("%o", $i); print "print ioctal : $ioctal\n"; printf("printf ioctal: %s\n", $ioctal); my $ihex = sprintf("%x", $i); print "print ihex : $ihex\n"; printf("printf ihex : %s\n", $ihex); my $isci = sprintf("%e", $i); print "print isci : $isci\n"; printf("printf isci : %s\n", $isci); my $ifloat = sprintf("%f", $i); print "print ifloat : $ifloat\n"; printf("printf ifloat: %s\n", $ifloat); my $iint = sprintf("%u", $i); print "print iint : $iint\n"; printf("printf iint : %s\n\n", $iint); } exit;
All 3 of Win XP 32bit ActivePerl, Win XP 32bit Strawberry Perl and Slackware 13.37 32 bit give identical incorrect results (for big numbers > 2**32 - 1):
i: 10 print ibytes : 1010 printf ibytes: 1010 print ioctal : 12 printf ioctal: 12 print ihex : a printf ihex : a print isci : 1.000000e+01 printf isci : 1.000000e+01 print ifloat : 10.000000 printf ifloat: 10.000000 print iint : 10 printf iint : 10 i: 4300000000 print ibytes : 11111111111111111111111111111111 printf ibytes: 11111111111111111111111111111111 print ioctal : 37777777777 printf ioctal: 37777777777 print ihex : ffffffff printf ihex : ffffffff print isci : 4.300000e+09 printf isci : 4.300000e+09 print ifloat : 4300000000.000000 printf ifloat: 4300000000.000000 print iint : 4294967295 printf iint : 4294967295
Slackware 13.37 x86_64 correct results:
i: 10 print ibytes : 1010 printf ibytes: 1010 print ioctal : 12 printf ioctal: 12 print ihex : a printf ihex : a print isci : 1.000000e+01 printf isci : 1.000000e+01 print ifloat : 10.000000 printf ifloat: 10.000000 print iint : 10 printf iint : 10 i: 4300000000 print ibytes : 100000000010011001100101100000000 printf ibytes: 100000000010011001100101100000000 print ioctal : 40023145400 printf ioctal: 40023145400 print ihex : 1004ccb00 printf ihex : 1004ccb00 print isci : 4.300000e+09 printf isci : 4.300000e+09 print ifloat : 4300000000.000000 printf ifloat: 4300000000.000000 print iint : 4300000000 printf iint : 4300000000

In reply to bignum with (s)printf - 64bit OS OK, 32bit incorrect. What to do? by geep999

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.