That is my code below to get "100 power of 2 in PHP",
echo pow(2,100);
I would get the result as 1.2676506002282E+30, However, I wish to get a whole integer of the result.
How should I do it with PHP?
That is my code below to get "100 power of 2 in PHP",
echo pow(2,100);
I would get the result as 1.2676506002282E+30, However, I wish to get a whole integer of the result.
How should I do it with PHP?
I guess by "accurate" you mean you want to get an exact integer. You will have to use additional library for working with big integers. GMP for example will work for what you need. It's is a free library for arbitrary precision arithmetic, operating on signed integers, rational numbers, and floating-point numbers.
Here is an example for the specific question:
<?php
$pow = gmp_pow("2", 100);
echo gmp_strval($pow);
?>
It will not work directly. You will have to first open php.ini and uncomment the "extension=gmp" line.
And that's not the only solution. There are more libraries available as variants for doing that.
if by accurate, you want to have the exact value of 2 to the power of 100 that is equal to 1,267,650,600,228,229,401,496,703,205,376,
you can simply use
echo number_format(pow(2,100));
Update: like @Olivier said in comments, number_format
does not return the exact results in some cases, so be careful in using that.