LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 09-13-2012, 01:33 AM   #1
systemlordanubis
Member
 
Registered: Jun 2010
Distribution: Debian, Ubuntu, Win
Posts: 126

Rep: Reputation: 16
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
 
Old 09-13-2012, 01:40 AM   #2
pan64
Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 5,169

Rep: Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364
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

Last edited by pan64; 09-13-2012 at 01:43 AM.
 
Old 09-13-2012, 11:41 PM   #3
systemlordanubis
Member
 
Registered: Jun 2010
Distribution: Debian, Ubuntu, Win
Posts: 126

Original Poster
Rep: Reputation: 16
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
 
Old 09-14-2012, 03:33 AM   #4
pan64
Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 5,169

Rep: Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364
There is a Math::Int64 module too, you can try that also (if not yet made)
 
Old 09-16-2012, 11:38 PM   #5
systemlordanubis
Member
 
Registered: Jun 2010
Distribution: Debian, Ubuntu, Win
Posts: 126

Original Poster
Rep: Reputation: 16
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.
 
Old 11-20-2012, 06:48 PM   #6
systemlordanubis
Member
 
Registered: Jun 2010
Distribution: Debian, Ubuntu, Win
Posts: 126

Original Poster
Rep: Reputation: 16
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.
 
  


Reply

Tags
perl


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Increasing the precision of float on python Adwat Linux - Newbie 1 05-17-2012 10:39 AM
how to compare float number in shell program olivia dunham Linux - Newbie 1 08-23-2011 04:39 AM
#define a float : which precision? tanoatlq Programming 5 09-27-2008 08:41 PM
assigning a large float number in E notation edM Programming 3 05-09-2005 01:51 PM
C++ format float number output keefaz Programming 3 06-06-2004 03:16 PM


All times are GMT -5. The time now is 06:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration