Solved

Handling extremely large numbers with PHP

Posted on 2010-11-20
2
246 Views
Last Modified: 2012-05-10
I am creating a calculator app that needs to be able to handle large numbers accurately. Since integers larger than 2147483647 can't be handled by PHP, and floats have an accuracy issue:

echo floor((.7+.1)*10) //Outputs 7

Open in new window


I need another solution. Is there any way to ensure accuracy with floats or another way to do math with php? Although I'd rather not use JS, as this means that someone can copy my processer, can JS handle large numbers?
0
Comment
Question by:cloud-9
2 Comments
 
LVL 7

Accepted Solution

by:
armchang earned 500 total points
ID: 34180475
Hi,

For some reason, there are two standard libraries in PHP handling the arbitrary length/precision numbers: BC Math and GMP. I personally prefer GMP, as it's fresher and has richer API.

Based on GMP I've implemented Decimal2 class for storing and processing currency amounts (like USD 100.25). A lot of mod calculations there and tried with very large numbers w/o any problems.

Go to GMP
Go to BC Math

0
 
LVL 3

Author Closing Comment

by:cloud-9
ID: 34183347
Thank-you for your quick response. This will work.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question