I need to convert a number from 36-th base into an integer. The original number length is about 10 characters which seem to be bigger then PHP's limits for integers.
In my case the original number looks like this: 9F02tq977. When converted I get the following result 8.46332972237E+12.
How can I store huge numbers in PHP?
Since BC Math doesn't work with arbitrary base numbers, you could try using GMP functions if you have them available.
http://www.php.net/manual/en/ref.gmp.php
Otherwise you'll probably have to rewrite your algorithm and write your own arbitrary precision arithemtic implementation of the validation algorithm.
Use a math library like BC Math for big numbers :)
http://be2.php.net/manual/en/book.bc.php
Related
I am making Fibonacci series for long numbers in PHP. For example my n = 100 and post 92 sequence it starts getting values like 1.2200160415122E+19.
Please help me to understand how to handle such big numbers in PHP.
On first sight I'd say this has nothing to do with the php language. It is a general issue with floating point notation that you simply do not have a precision as with fixed point notation. For tasks like Fibonacci I'd say you need a precision of 1, thus a floating point notation is unsuitable for the task. No way around that.
However there are a number of classes and extensions for php that allow arithmetic with large integers. I suggest you take a look into those:
BC Math
GMP
I need to store a huge number in a PHP variable (it's for a programming contest if you're wondering why). However, if the number is too big, it gets displayed as 6.2995416979471E+77. Is there a way to store that huge number in PHP?
integers have a limited size of 32 or 64bit, depending on your architecture.
floats can store values of any size, but are imprecise.
If you want to work with giant, precise numbers, use strings and the BC Math extension.
$bigNumber = 1234567890; // etc
echo number_format($bigNumber, 0);
Have you tried PHP's BCMath arbitrary precision functions? They use strings to represent numbers.
Need something like intVal() but for bigger numbers, for example: 100001416147426.
Is there a function like this in php? Can't find it.
You should use BC Math, it is designed to numbers of any size with PHP.
The BC Math extensions offers several mathematic functions like:
bcadd Add two arbitrary precision numbers
bccomp — Compare two arbitrary precision numbers
bcsqrt Get the square root of an arbitrary precision number
...
On the PHP documentation site there is a small code example by Charles to round a long number!
consider
$x = (double) "100001416147426";
var_dump($x);
output:
float(1.0000141614743E+14)
coding standard in the past (since C) has been to follow the number with an L/l
$x = 100001416147426L;
This cue's the parser to allocate 64 bits in order to read the number out of the script to compile.
But unless you are running the php x64 this will be useless. Other wise you will have to build it out of a big_number component. Once in a x64 environment intval will automatically expand to a long when exceeding a 32-bit int.
I have very large numbers, and decided to represent them with base64 strings in php. I was wondering if anyone knows of a library (or built in system) to work with base64 as numbers (aka add, subtract, multiply, divide, etc)?
Generally it does not make much sense to speak about operations on numbers in one specific base or another. Rather, you decode your numbers to a more useful or generalized representation (e.g. a string of decimal digits) and then perform work, re-encoding your result for storage as necessary.
The Math_BigInteger library facilitates this. You will of course still have to first decode your base64 data to a base usable by the class, i.e., binary, decimal, or hexidecimal.
This is an instance of the XY problem. The problem is that you want to add arbitrary-precision numbers. For that, you should use an arbitrary precision math package, such as BC. There's no point representing them in base64 unless there's an implementation of arithmetic for those, which as far as you know there isn't.
What is fastest and most easy way of making basic arithmetic operations on strings representing real numbers in PHP? I have a string representing MySQL's DECIMAL value on which I want to operate and return the result to a database after that. I would like to avoid errors introduced by floating-point real number representation.
Is there some standard library like Python's decimal? Are there any FOSS libraries you can recommend?
Regards
You could use the BC math functions:
http://www.php.net/manual/en/ref.bc.php
Or the GMP functions, although they seem to be integer only.
http://www.php.net/manual/en/ref.gmp.php
You can use the bc_math extension, which works exactly how you ask (it's used for arbitrary precision numbers):
$num = '123.456';
$twoNum = bcadd($num, $num);