Wrong modulus result on PHP but right result on Python - php

I got the problem when operating this number on PHP
ex:
72 ** 79 % 3337
When i use
echo 72 ** 79 % 3337 // Result is int(0)
Then i try to split into this one
$num = number_format(72 ** 79, 0, '', '');
echo $num % 3337; // Result is 1069
Then again i try using fmod() and bcmod()
$num = number_format(72 ** 79, 0, '', '');
echo fmod($num, 3337); // Result is 2255
echo bcmod($num, 3337); // Result is 2255
But, the result that i want is 285 and when using Python the answer is right.
Why does this happen? Any Solution?

You are overflowing floating point resolution, so you need to do all the math with bcmath, including the exponentiation:
$num = bcpow(72, 79);
echo bcmod($num, 3337);
Output
285
Demo on 3v4l.org

Related

PHP Modulo and pow()

Can this be made robust? at various points I'm finding that 5 % 2 = 0
and I've never encountered this 'quirk' before, and probably due to ignorance around precision :/
$checkPrimeCubeRoots = array(125, 124);
foreach ($checkPrimeCubeRoots as $int)
{
$cubeRoot = pow($int, 1/3); // double
// $int = 125, gives $cubeRoot = 5
// $int = 124, gives $cubeRoot = 4.986....
// some code that check for prime, but let's just check if divisble by 2
echo $cubeRoot % 2; // 0 -> yup 5 is divisible by 2
echo intval($cubeRoot) % 2; // 0 -> yup
// try round -> but gives false positive for 124
echo round ($cubeRoot) %2; // 1 -> nope, but in both cases :/
}
The % is intended to be used with integers only. The result of using it on floating point numbers does is somewhat unpredictable. And even though the result of pow(125, 1/3) might seem an integer, it's being stored internally as a floating point (there is an interesting article by NikiC if you want to know more about the internals).
One quick solution is to use fmod() instead, which is the floating point version.
echo fmod(pow(125, 1/3), 2); # 1
echo fmod(pow(124, 1/3), 2); # 0.98663095223865
When working with any float/double type values, there is a possibility that some slight difference in the internal representation and the actual value are stored. Also you can use fmod() which works with floating point numbers better...
$checkPrimeCubeRoots = array(125, 124);
foreach ($checkPrimeCubeRoots as $int)
{
$cubeRoot = pow($int, 1/3); // double
// $int = 125, gives $cubeRoot = 5
// $int = 124, gives $cubeRoot = 4.986....
// some code that check for prime, but let's just check if divisble by 2
echo "%=".$cubeRoot % 2 .PHP_EOL;; // 0 -> yup 5 is divisible by 2
echo "intval=".intval($cubeRoot) % 2 .PHP_EOL;; // 0 -> yup
echo "fmod()=".fmod($cubeRoot,2).PHP_EOL;
// try round -> but gives false positive for 124
echo round ($cubeRoot) %2 .PHP_EOL; // 1 -> nope, but in both cases :/
}
Which gives...
%=0
intval=0
fmod()=1
1
%=0
intval=0
fmod()=0.98663095223865
1

Function round php not work correctly

php function round not working correctly.
I have number 0.9950.
I put code:
$num = round("0.9950", 2);
And I get 1.0? Why?? Why I can't get 0.99?
You can add a third parameter to the function to make it do what you need.
You have to choose from one of the following :
PHP_ROUND_HALF_UP
PHP_ROUND_HALF_DOWN
PHP_ROUND_HALF_EVEN
PHP_ROUND_HALF_ODD
This constants are easy enough to understand, so just use the adapted one :)
In your example, to get 0.99, you'll need to use :
<?php echo round("0.9950", 2, PHP_ROUND_HALF_DOWN); ?>
DEMO
When you round 0.9950 to two decimal places, you get 1.00 because this is how rounding works. If you want an operation which would result in 0.99 then perhaps you are looking for floating point truncation. One option to truncate a floating point number to two decimal places is to multiply by 100, cast to integer, then divide again by 100:
$num = "0.9950";
$output = (int)(100*$num) / 100;
echo $output;
0.99
This trick works because after the first step 0.9950 becomes 99.50, which, when cast to integer becomes just 99, discarding everything after the second decimal place in the original number. Then, we divide again by 100 to restore the original number, minus what we want truncated.
Demo
Just tested in PHP Sandbox... PHP seems funny sometimes.
<?php
$n = 16.90;
echo (100*$n)%100, "\n"; // 89
echo (int)(100*$n)%100, "\n"; // 89
echo 100*($n - (int)($n)), "\n"; // 90
echo (int)(100*($n - (int)($n))), "\n"; // 89
echo round(100*($n - (int)($n))), "\n"; // 90

PHP: modulo result truncates into two digits

I am just wondering why in PHP when I echo the modulo of 123.456 and 100, the result is only 23, but the quotient of 123.456 / 100 is 1.23456. For instance:
<?php
echo 123.456 % 100;
echo 123.456 / 100;
?>
OUTPUT:
23
1.23456
I am expecting that it should return the complete decimal places which is 23456, but it round it off into two decimal places instead. I am new to PHP, and I have no idea if this is a normal behavior of modulo. And if it is, is there any way to get my expected result?
Thanks in advance! :)
What if you force it to be float?
echo (float)(123.456 % 100);
The problem seems to be that it's parsing it as an integer.

Weird output, when number starts with 0

1. script:
$num = "00445790";
echo $num;
returns:
00445790
2. script
$num = 00445790;
echo $num;
returns:
2351
Can somebody explain why I get 2351 on the second script?
Integers that start with zero are consider octal. Because octal integers only use numbers from 0 to 8 everything from the 9 on are ignored.
So 00445790 becomes 004457 which is 2351 in decimal.

PHP: number_format rounding

Hi I've been having a problem rounding numbers to -0 instead of just a 0
code:
<?php
$num= 0.000000031;
$round = number_format((float)$num, 1, '.', '');
echo $round * -1;
?>
output:
-0
expected output:
0
I've been looking to any solution but nothing found.
kindly explain & help me why it rounds up to -0 instead of 0? thank you
Not the rounding makes it -0.
The $round variable contains this before the last line:
string(3) "0.0"
You can verify this with adding this line:
var_dump($round);
before the echo.
So if you multiply "0.0" (string) with -1 then the result will be "-0"
Because (string)0 is casted to (float)0 before the multiplication and
(float)0 * -1 = -0
php5 -r 'var_dump((float)0*-1);'
float(-0)
Which is completely normal based on the floating numbers behaviour. (More details: http://en.wikipedia.org/wiki/Signed_zero )
If this is a problem you can add 0 to avoid this "magic":
php5 -r 'var_dump((float)0*-1+0);'
float(0)
Since number_format returns string, you need to cast it to get expected result.
<?php
$num= 0.000000031;
$round = number_format((float)$num, 1, '.', '');
echo (int)$round * (-1); //print 0
?>
PHP Sandbox
You PHP code it's disorganized.
I assume the var $xx on the second line it's $num.
Then, you must first do all operations (operation layer) and then do presentations (presentation layer):
<?php
// Operation layer
$num = 0.000000031;
$round = $num * -1;
// Presentation layer
echo number_format($round, 1, '.', '');
When you do a number_format retrieve a string, not a number.

Categories