New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Math::BigInt->new() wrongly converts large floats #13569
Comments
From perlmathbigint@volkerschatz.comHello, a conversion of large integral floating-point numbers into BigInts can lose $ perl -MMath::BigInt -e 'print Math::BigInt->new(504403158265495552.0)->bstr(), The cause seems to be that the _split function in Math::BigInt treats its My Perl version is 5.18.2, the Math::BigInt version 1.9991. Best regards, Volker |
From dana@acm.orgOn Sat Feb 01 06:35:27 2014, perlmathbigint@volkerschatz.com wrote:
This ought to go on the Math::BigInt RT list. Also see RT 61887 (still open) which shows inconsistent behavior with floats that have non-zero fractional parts, which means more changes are necessary in this code. This may fall under the "don't do this" clause in the description. Use int($n) to work around. Quoting the value also works, though is a bit awkward if you have a double to begin with, forcing you to do Math::BigInt->new(sprintf("%f",$n)). It would be my belief that the conversion through %f would be a bad idea in general, as it would lose precision for strings and large integers. If we had something like Scalar::Number we could convert to a full precision string if and only if the input was an NV. A hack for testing (don't do this), is to insert right before the _split in new: Dana |
The RT System itself - Status changed from 'new' to 'open' |
From perlmathbigint@volkerschatz.comHi Dana, thank you for your reply.
Sorry for that... I was not sure where to send the report, and in the past for
This works only when int() can convert to a 64-bit fixed point type, and the
Yes, I agree that it would likely mess up non-floats.
Thanks for mentioning Scalar::Number and Scalar::Util, which I had not been Anyway, I think the following would work too: @@ -3048,6 +3050,8 @@ + $x= sprintf("%f", $x) unless "$x" == $x; # ensure enough digits from float The condition checks the integrity of a roundtrip conversion via the automatic I have taken a look at some other bugs, and #63038 is fixed by using substr() @@ -625,8 +625,10 @@ Regarding #61887: The code (lines 613 to 623) shows that the intention was to defeats this when the integer part is 0. To keep the old behaviour, one Considering your remark about posting on the Math::BigInt bug tracker, I am Regards, Volker |
Migrated from rt.perl.org#121136 (status was 'open')
Searchable as RT121136$
The text was updated successfully, but these errors were encountered: