What is the difference between bcpow and pow? - php

Can someone explain to me if I should use bcpow() instead of pow() and why?
I understand that not all installations of php have bcmath enabled. So if I write an open source project, and want to have as few dependencies/requirements as possible, I would rather use pow() in my code.
But what are the downsides to using pow() over bcpow()?

bcpow() is a function of the BCMath Arbitrary Precision Mathematics library.
Quoting the introduction of it's manual :
For arbitrary precision mathematics
PHP offers the Binary Calculator which
supports numbers of any size and
precision, represented as strings.
On the other hand, pow() is limited to floats, which have a limited size (quoting) :
The size of a float is
platform-dependent, although a maximum
of ~1.8e308 with a precision of
roughly 14 decimal digits is a common
value (the 64 bit IEEE format)
Generally, you'll work with pow() and other float-based functions (which are probably faster, and are always enabled) ; but, if you need to handle very big number, you'll have to work with bcpow().

According to the manual the bc* functions are
[...] arbitrary precision mathematics PHP offers the Binary Calculator which supports numbers of any size and precision, represented as strings.
pow() is limited to the maximum supported numeric representation on the system it runs on.

bcpow is used for arbitrary precision values. As a third parameter you can specify a number of digits after coma.

Related

Convert string to float without losing precision

var_dump((float)'79.10') returns me 79.09999999999999. I've tried a million ways to try and round this value up to the original 79.10 as a float (number_format, round), and I can't find a way to do it.
Is there any way I can get a float value of 79.10 from the original string?
No, because 0.1 (and, by extension, 79.1) is not actually representable as a float (assuming IEEE-754 single or double precision encoding). 0.1 in that encoding has an infinitely recurring fractional part:
1001 1001 1001 1001 ...
You'll either have to leave it as a string or accept the fact that the encoding scheme does not have infinite precision and work around it.
An example of the latter is to only output the numbers to a certain precision, such as two decimal digits, and to make sure that (in-)equality comparisons use either absolute or relative deltas to compare numbers.
When you're adding numbers, it takes quite a few operations for the imprecision effects to become visible at the hundredths level. It's quicker when multiplying but still takes a while.
While paxdiablo is right and working with floats does not have infinite precision, I've discovered that it is indeed possible to represent 79.10 as a float by first adjusting PHP's precision setting:
ini_set('precision', 15);
After that, var_dump((float)'79.10') correctly returns a float of 79.1. The different results that everyone is seeing on their own machines seems to be a result of everyone having different precision values set by default.
This is impossible as a float because it does not offer enough precision (see here for more information)
Now, in most languages you could cast it to a double... Unfortunately, in PHP, float and double use exactly the same underlying datatype so in order to get the value you want, you would have to recompile PHP.
Your best option would be to use something like the PHP BCMath module for arbitrary precision.

Does php support 64-bit ieee float

I have done a deep reserach on this topic. But nothing is clear about this question. Can anyone help me out with this.
http://www.php.net/manual/en/language.types.float.php
Nothing is properly described this link prperly.
Yes it does.
From the PHP Manual..
The size of an integer is platform-dependent, although a maximum value
of about two billion is the usual value (that's 32 bits signed).
64-bit platforms usually have a maximum value of about 9E18. PHP does
not support unsigned integers. Integer size can be determined using
the constant PHP_INT_SIZE, and maximum value using the constant
PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5.
Also..
If the float is beyond the boundaries of integer (usually +/- 2.15e+9
= 2^31 on 32-bit platforms and +/- 9.22e+18 = 2^63 on 64-bit platforms), the result is undefined, since the float doesn't have
enough precision to give an exact integer result. No warning, not even
a notice will be issued when this happens!
Exerpts from the inner links of PHP Manual to a 3rd Party site...
PHP is dynamically typed and will often convert implicitly between
strings and floating-point numbers (which are platform-dependant, but
typically IEEE 64 bit values). To force a value to floating-point,
evaluate it in a numerical context: $foo = 0 + "10.5";
But if you are looking to play around with floats and precisions you need to use the GMP functions.
Values stores inside https://github.com/php/php-src/blob/master/Zend/zend.h#L322
For float use double type. Size of float and double.
So, php support 64-bit ieee float.

why pow(3^47) gives wrong answer? [duplicate]

This question already has answers here:
Unexpected results when working with very big integers on interpreted languages
(36 answers)
Working with large numbers in PHP
(8 answers)
Closed 9 years ago.
using php, this code echo sprintf('%.9F',pow(3,47)); outputs 26588814358957501972480.000000000
where as other sites like this gives 3^47 = 26588814358957503287787. Is there any bug with PHP? How to resolve it?
btw, i'd like to know what is the maximum digit php can handle for calculation using pow,sprintf,fmod. Is it 300 digits?
You need to use an external program or a PHP extension (library).
I've just tested it with BC Math and GMP using the bcpow()/gmp_pow() functions and it works perfectly:
<?php
// both output 26588814358957503287787
echo bcpow('3', '47');
echo gmp_strval( gmp_pow('3', '47') );
The PHP.net documentation has a nice section about that topic:
Floating point precision
Floating point numbers have limited precision. Although it depends on
the system, PHP typically uses the IEEE 754 double precision format,
which will give a maximum relative error due to rounding in the order
of 1.11e-16. Non elementary arithmetic operations may give larger
errors, and, of course, error propagation must be considered when
several operations are compounded.
Additionally, rational numbers that are exactly representable as
floating point numbers in base 10, like 0.1 or 0.7, do not have an
exact representation as floating point numbers in base 2, which is
used internally, no matter the size of the mantissa. Hence, they
cannot be converted into their internal binary counterparts without a
small loss of precision. This can lead to confusing results: for
example, floor((0.1+0.7)*10) will usually return 7 instead of the
expected 8, since the internal representation will be something like
7.9999999999999991118....
So never trust floating number results to the last digit, and do not
compare floating point numbers directly for equality. If higher
precision is necessary, the arbitrary precision math functions and gmp
functions are available.
For a "simple" explanation, see the » floating point guide that's also
titled "Why don’t my numbers add up?"
— http://php.net/manual/en/language.types.float.php
Note that the php.ini configuration value 'precision' can also modify the precision when converting from floats to strings.
bcpow() (a function of the BCMath Arbitrary Precision Mathematics library) can be used in this case.
echo bcpow('3', '47'); //as mentioned in ComFreek's answer
//outputs 26588814358957503287787
The reason why pow isn't work here is because pow() uses float, and there's a size limitation for float.
See this excerpt from the PHP Manual:
The size of a float is platform-dependent, although a maximum of ~1.8e308 with a precision of roughly 14 decimal digits is a common value (the 64 bit IEEE format)
In other words, the maximum possible size may vary -- i.e. (may be different for 32-bit, and 64-bit systems).
Hope that answers your question!

How to parse long value in php?

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.

How much precision for a bcmath PHP library?

I'm writing a PHP library that has a Number class that uses the bcmath extension for arbitrary precision.
I have two questions:
How much slower is bcmath compared to using the built-in int and float types?
bcmath has an optional scale argument (that defaults to 3 digits). For an general purpose Number class that anyone could use, what would be a good level of precision? How do languages like Perl (that have arbitrary precision numbers) deal with scale?
I would decide what range of numbers you need to support. The built in values will be faster than any value that requires calculation and conversion to/from some other format.
Built in integers are good until 32 bits on any system, some systems support 64 bit values. You can check what your system supports by checking the value of the constant PHP_INT_MAX and determine if you want to carry the overhead of the math library after that. For systems with 32 bit integers, anything above 32 bits will be converted to a float automatically. This isn't an issue unless you are using built in functions for things like round, printf, modulus etc.
I was bit by this using modulus to divide traffic coming to my site as well as with formatting integers using %d in sprintf: http://af-design.com/blog/2009/10/28/php-64-bit-integer-modulus-almost/

Categories