Open rsFalse opened 9 months ago
>perl -wle "print join ' ', map sqrt, 199999982358225, 199999982358226;"
14142135 14142135
Perl's print() function rounds the given values to 15 significant decimal digits - and at that precision the 2 square roots are the same, But the 2 values are different, as perl well knows:
>perl -wle "printf '%.17g %.17g', sqrt 199999982358225, sqrt 199999982358226;"
14142135 14142135.000000035
This unfortunate behaviour of the print() function has been around for a long time, and will probably remain for a few more years yet.
The warnings you see arise just because you've loaded both bigint and bignum:
>perl -Mbigint -Mbignum -wle "print 'ok'";
Subroutine main::inf redefined at -e line 0.
Subroutine main::NaN redefined at -e line 0.
ok
>perl -Mbignum -Mbigint -wle "print 'ok'";
Subroutine main::inf redefined at -e line 0.
Subroutine main::NaN redefined at -e line 0.
ok
I don't know why the warnings don't appear under the warnings pragma. Perhaps it's because the warnings pragma recognizes that these redefinitions are not really an issue. You could take this up with the bignum/bigint maintainer on CPAN if you want to pursue it.
D:\pscrpt>perl -Mbigint -Mbignum -le "no bignum; use bigint; print join ' ', sqrt 199999982358225, sqrt 199999982358226;"
14142135 14142135.00000003535534061865477701916612
D:\pscrpt>perl -Mbigint -le "print join ' ', sqrt 199999982358225, sqrt 199999982358226;"
14142135 14142135
I expected both of those one-liners to output 14142135 14142135
.
I don't know why the first one didn't - perhaps that's a bug in my thinking or perhaps a bug in bignum/bigint.
Something else to take up with the bignum/bigint maintainer if you' want to pursue it.
However, I don't see anything here for the perl porters to consider, I think this issue should be closed unless, of course, I've missed something important
@Sisyphus wrote:
The warnings you see arise just because you've loaded both bigint and bignum:
>perl -Mbigint -Mbignum -wle "print 'ok'"; Subroutine main::inf redefined at -e line 0. Subroutine main::NaN redefined at -e line 0. ok >perl -Mbignum -Mbigint -wle "print 'ok'"; Subroutine main::inf redefined at -e line 0. Subroutine main::NaN redefined at -e line 0.
Yes, and both bigint
and bignum
export inf
and NaN
by
default. The following issues no warnings:
perl -wle "use bigint(); use bignum();"
The following case puzzles me, though
$ perl -le "use bignum; no bignum; use bigint; print join ' ', sqrt 199999982358225, sqrt 199999982358226;"
14142135 14142135.00000003535534061865477701916612
but even bignum
version 0.03 from 2002 (long before I got
involved) gives this output, so at least it is nothing new.
I'll look into this.
Somehow warnings disappear when I use use warnings instead of -w flag.
See https://perldoc.perl.org/warnings#What's-wrong-with-w-and-$%5EW - the -w flag is discouraged as it enables warnings in scopes that are not expecting them, leading to spurious warnings.
Description
Somehow
sqrt()
truncates number14142135.0000000353553...
to integer value14142135
.Steps to Reproduce
OUTPUT:
Somehow warnings disappear when I use
use warnings
instead of-w
flag.I found several more similar size numbers, all of them are bigger than 1e14, each of them is square which has two non-square neighbors (+1 and -1):
The smallest which I found:
102609606529600+-1
, which issqr( 10129640 )
, butsqr( 10129640 - 1 )+-1
have decimal point.Expected behavior
Number 199999982358226, which is approx. 2e14, has no integer root.
Perl configuration