Solved

Handling extremely large numbers with PHP

Posted on 2010-11-20
2
238 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now