LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Ensuring large number precision and avoiding float. (http://www.linuxquestions.org/questions/programming-9/ensuring-large-number-precision-and-avoiding-float-4175426988/)

systemlordanubis 09-13-2012 01:33 AM

Ensuring large number precision and avoiding float.
 
Hi All,

Recently in part of my code I've noticed a strange occurance when working with large numbers (greater than the 32-bit range).

What I'd like to know is if anyone knows of a way to ensure that a large integer say "12476476550" for example remains as an int and doesn't get converted to a float.

What's happening is part-way during the code (and strangest of all) when performing a subtraction the number is converting to a float and a small 'decimal' is added despite both numbers in the subtraction being actual whole numbers.

The only thing I can put this down to is a change in the actual data type perl is thinking the variable is and when converting to a float it's loosing precision.


This is an example of the issue:

Input Number: 15892213505
Subtracting: 12884901888
Returns a result like: 3007311616.99999


I've already tried Math::BigInt and use bigint; but both have serious performance impacts which means these aren't able to be used.


Any suggestions welcome.
Thanks
Anubis

pan64 09-13-2012 01:40 AM

have you tried this ?
also see here: http://perldoc.perl.org/Math/BigInt.html#MATH-LIBRARY
and here: http://perldoc.perl.org/Math/BigInt.html#PERFORMANCE

systemlordanubis 09-13-2012 11:41 PM

Hi pan64,

Thanks for the reply. I've already tried those ones and although it does fix the problem it does make the execution time too long for application.

Is there some way to re-build Perl such that -Duse64bitint is enabled on a 32-bit system? Is it even possible to use that flag on a 32-bit system?

Thanks
Anubis

pan64 09-14-2012 03:33 AM

There is a Math::Int64 module too, you can try that also (if not yet made)

systemlordanubis 09-16-2012 11:38 PM

Hi pan64,

Unfortunately my only solution has been to compile another version of perl to run side-by-side with the one built into debian so that I can enable the -Duse64bitint flag. Now I can use the magical 'q' pack flag to get what I need.

Thanks
Anubis.

systemlordanubis 11-20-2012 06:48 PM

Hi All,

In the end, I've upgraded to Debian 7.0 (testing) which has perl built-in with the -Duse64bitint flag set. This has solved this issue.

Thanks.
Anubis.


All times are GMT -5. The time now is 04:01 PM.