What is difference between PHP fmod function and arithmetic % operator ?
Here its give diff output
fmod(1438090136928 , 268435456)
and
1438090136928 % 268435456
When to use these functions?
Edit
Which one is more faster ?
As doc says:
Operands of modulus are converted to integers (by stripping the
decimal part) before processing.
But fmod accepts float values and return a float value also.
I do not know the differences in detail but the following information might help :-
The modulus can only handle numbers upto 4294967296 i.e 2^32
Both fmod and % are native
You should also consider using php math functions, as they can handle larger numbers and comparatively faster
Thanks, hope it helps.
Related
How can i do modulo arithmetic with decimal value since PHP only can do modulo with integer?
Example case:
echo 1.92 % 1000; // (int) 1, expected result should be 1.92
is there any library to do this correctly?
More info about this modulo arithmetic problem: https://bugs.php.net/bug.php?id=34399
There is also a floating point fmod() function
echo fmod(1.92, 1000)
http://php.net/manual/en/function.fmod.php
I think you wanted this one, this returns float result?
Given the following cod:
$number = 1050.55;
var_dump($number - floor($number));
Why does the above code returns the following result?
float(0.54999999999995)
I want a fixed value like 0.55 in this case. Can you help me please?
Floating point operations are not precise and the remainder errors are common.
If you know, what is your desired precission (eg. two digits after the dot), you can use round() function on the result.
In this case this will be:
$number = 1050.55;
var_dump(round($number - floor($number), 2));
For most floats, binary can only approximately represent the correct number. The rule is to perform floor(), ceil() or fmod() last in a series of calculations. At least only do integer math after you use them. If you cast an int to a float, as in your code, then floor() is not going to behave has you expect.
Use printf() when printing floats. Its conversion routines usually do a much better job and give you the answer you expect when truncating floats.
EDIT: Or, to be more exact, printf() works on the decimal character representation of the number when deciding where to truncate so you don't get any weird, unspecified, binary/decimal conversion artifacts.
See this question. While that is about java and you're asking about PHP the math is the same.
Does modulus division only return integers? I need a float return. See the following:
var_dump(12 % 10); // returns 2, as expected
var_dump(11.5 % 10); // returns 1 instead of 1.5?
Yes. the % operator returns an integer.
If you want a floating point result, use the fmod() function instead.
See the manual.
Operands of modulus are converted to integers (by stripping the
decimal part) before processing.
11.5 becomes 11.
11 % 10 = 1 remainder **1**
Your solution: fmod(), as tom_yes_tom suggests.
Quoting the documentation page:
"Operands of modulus are converted to integers (by stripping the
decimal part) before processing."
Is there any workaround for this?
mathematics...
11.5 - floor(11.5 / 10) * 10 == 1.5
$nt=(float) number_format("26031.87",2,".",""); // 26031.87
$nt2=(float) 546669.02-520637.15; // 26031.87
if($nt>$nt2)
echo "$nt / $nt2 ⇽ What's wrong with this!? :#";
the point is why this happen?, if visually looks the same, a chunky solution is doing number_format() to $nt2, but... WHY??
updating :: $nt-$nt2 outputs 3.6379788070917E-12
http://php.net/manual/en/language.types.float.php see that big red warning banner. :)
For comparing floats you can use:
if (abs($nt1-$nt2) < 0.00001) {
echo "Equal!";
}
(change 0.00001 to comparison precision you need).
Per Zend:
PHP doesn't seem to do the logical thing when comparing two floats, and this is due to the internal representation of the numbers. The solution is simply never compare floats for equality!
Convert them to INT before comparing them or use bc_math.
I need to simulate a ∞ in PHP.
So that min(∞,$number) is always $number.
I suppose that, for integers, you could use PHP_INT_MAX , the following code :
var_dump(PHP_INT_MAX);
Gives this output, on my machine :
int 2147483647
But you have to be careful ; see Integer overflow (quoting) :
If PHP encounters a number beyond the
bounds of the integer type, it will
be interpreted as a float instead.
Also, an operation which results in a
number beyond the bounds of the
integer type will return a float
instead.
And, from the Floating point numbers documentation page :
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).
Considering the integer overflow, and depending on your case, using this kind of value might be a better (?) solution...
Use the constant PHP_INT_MAX.
http://php.net/manual/en/language.types.integer.php
You could potentially use the PHP_INT_MAX constant (click for PHP manual docs).
However, you may want to think about whether you really need to use it - it seems like a bit of an odd request.
PHP actually has a predefined constant for "infinity": INF. This isn't true infinity, but is essentially the largest float value possible. On 64-bit systems, the largest float is roughly equal to 1.8e308, so this is considered to be equal to infinity.
$inf = INF;
var_dump(min($inf,PHP_INT_MAX)); // outputs int(9223372036854775807)
var_dump(min($inf,1.79e308)); // outputs float(1.79E+308)
var_dump(min($inf,1.799e308)); // outputs float(INF)
var_dump(min($inf,1.8e308)); // outputs float(INF)
var_dump($inf === 1.8e308); // outputs bool(true)
Note, any number with a value larger than the maximum float value will be cast to INF. So therefore if we do, var_dump($inf === 1e50000);, this will also output true even though the maximum float is less than this.
I suppose, assuming this is an integer, you could use PHP_INT_MAX constant.
min($number, $number + 1) ??
In Perl you can use
$INF = 9**9E9;
which is larger than any value you can store in IEEE floating point numbers. And that really works as intended: any non-infinite number will be smaller than $INF:
$N < $INF
is true for any "normal" number $N.
Maybe you use it in PHP too?
min($number,$number) is always $number (also true for max() of course).
If your only concern is comparison function then yes, you can use array(), it will be always larger then any number
like
echo min(array(), 9999999999999999);
or
if (array() > 9999999999999999) {
echo 'array won';
} else {
echo 'number won';
}