I received a comment from Peter John Acklam (current bigint maintainer).

-------- Forwarded Message --------
Date: Wed, 14 Nov 2018 20:08:27 +0100

Hello

This has actually nothing to do with Math::BigInt. The issue here is that
the conversion "u", converts a number to an unsigned integer before the
integer is printed as a string. On 32 bit systems, an unsigned integer has
values between 0 and 2^32-1, inclusive. On 64 bit systems, the values are
between 0 and 2^64-1. Using input values outside of this range is not a
good idea. It seems that if the input is larger than the maximum value, it
is truncated to the maximum value, but I wouldn't rely on this. On my 64
bit system, the input value is truncated:

$ perl -we 'printf "%u\n", "1e30"'
18446744073709551615

On the other hand, when the input value is smaller than zero, the input is
not truncated to zero on my system:

$ perl -we 'printf "%u\n", "-1e30"'
9223372036854775808

As for the "s" conversion, it never converts the input to an integer (or
float). Strings are passed right through, unmodified. So that's why I get

$ perl -we 'printf "%s\n", "1e30"'
1e30

If you want "1e30" to be displayed as "100...", Math::BigInt is handy for
converting "1e30" to "100...":

$ perl -MMath::BigInt -we 'printf "%s\n", Math::BigInt->new("1e30")->bstr()'
1000000000000000000000000000000

Regarding the question "Is it guaranteed (e.g. described as language
specification) that printf("%s\n", $value) will print correct value
for any environment?"
Yes, the "s" conversion passes strings through unmodified.

If you have more questions or want more clarification, don't hesitate to
ask again.

Best regards,
Peter

On 2018/11/13 23:57, Mike Flannigan wrote:
> 
> I don't have an answer for you, but I find this
> interesting.  I note the same issue in 64bit
> up near
> 18446744073709551615
> 
> I'm guessing the guy who wrote
> Math::BigInt
> may have the answer.
> 
> 
> Mike
> 
> 
> 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);
>>
>> 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?
>>
> 

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to