On 11/13/2018 8:07 AM, Tetsuo Handa wrote:
> Hello.
>
> I want to represent up to a few hundreds gigabytes for file size.
>
> On 32bits platform, I noticed that
>
> my $value = ...;
> printf("%u\n", $value);
>
> prints 4294967295 if $value >= 4294967295 whereas
>
> my $value = ...;
> printf("%s\n", $value);
>
That can fail if $value is so big that it requires more than 15 decimal
digits to express it accurately.
For example:
C:\_32>perl -le "printf '%s', 901234567890123456789;"
9.01234567890123e+020
As you can see, it's printing the value as 901234567890123000000
> and
>
> use Math::BigInt;
> my $value = ...;
> printf("%s\n", Math::BigInt->new($value)->bstr());
>
> print correct value even when $value >= 4294967295.
>
> Is it guaranteed (e.g. described as language specification) that
> printf("%s\n", $value) will print correct value for any environment?
>
If you're dealing with integer values that overflow perl's integer type
(IV), then you're generally better off using a module that accommodates
larger integers.
Math::BigInt is fine for this, though it can be rather slow if you're doing
lots of large calculations.
Other options include Math::Int64 (64-bit integer support),
Math::Int128(128-bit integer support), Math::GMP(multiple precision) and
Math::GMPz (multiple precision).
Note that even with Math::BigInt it's important that you assign the value
as a string:
C:\_32>perl -MMath::BigInt -le "printf '%s',
Math::BigInt->new('901234567890123456789');"
901234567890123456789
If you assign it as number (unquoted):
C:\_32>perl -MMath::BigInt -le "printf '%s',
Math::BigInt->new(901234567890123456789);"
901234567890123000000
Similarly, assign as a string when using the other alternative modules
that I mentioned.
Note also that you can just print() the value:
C:\_32>perl -MMath::BigInt -le "print
Math::BigInt->new('901234567890123456789');"
901234567890123456789
There's no need to invoke printf(), and no need to call the bstr() method.
Cheers,
Rob
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/