Reputation: 780
How would you find the fractional part of a floating point number in PHP?
For example, if I have the value 1.25
, I want to return 0.25
.
Upvotes: 55
Views: 40535
Reputation: 41
To stop the confusion on this page actually this is the best answer, which is fast and works for both positive and negative values of $x:
$frac=($x<0) ? $x-ceil($x) : $x-floor($x);
I ran speed tests of 10 million computations on PHP 7.2.15 and even though both solutions give the same results, fmod is slower than floor/ceil.
$frac=($x<0) ? $x-ceil($x) : $x-floor($x);
-> 490-510 ms (depending on the sign of $x)
$frac=fmod($x, 1);
-> 590 - 1000 ms (depending on the value of $x)
Whereas the actual empty loop itself takes 80 ms (which is included in above timings).
Test script:
$x=sqrt(2)-0.41421356237;
$time_start = microtime(true);
for ($i=0;$i<=9999999;$i++) {
//$frac=fmod($x, 1); // version a
$frac=($x<0) ? $x-ceil($x) : $x-floor($x); // version b
}
$time_end = microtime(true);
$time = $time_end - $time_start;
Upvotes: 0
Reputation: 30911
$x = fmod($x, 1);
Here's a demo:
<?php
$x = 25.3333;
$x = fmod($x, 1);
var_dump($x);
Should ouptut
double(0.3333)
Upvotes: 24
Reputation: 37
Some of the preceding answers are partial. This, I believe, is what you need to handle all situations:
function getDecimalPart($floatNum) {
return abs($floatNum - intval($floatNum));
}
$decimalPart = getDecimalPart($floatNum);
Upvotes: 1
Reputation:
However, if you are dealing with something like perlin noise or another graphical representation, the solution which was accepted is correct. It will give you the fractional part from the lower number.
i.e:
.25
: 0 is integer below, fractional part is .25-.25
: -1 is integer below, fractional part is .75With the other solutions, you will repeat 0 as integer below, and worse, you will get reversed fractional values for all negative numbers.
Upvotes: 1
Reputation: 2504
The answer provided by nlucaroni will only work for positive numbers. A possible solution that works for both positive as well as negative numbers is:
$x = $x - intval($x)
Upvotes: 11
Reputation: 166106
Don't forget that you can't trust floating point arithmetic to be 100% accurate. If you're concerned about this, you'll want to look into the BCMath Arbitrary Precision Mathematics functions.
$x = 22.732423423423432;
$x = bcsub(abs($x),floor(abs($x)),20);
You could also hack on the string yourself
$x = 22.732423423423432;
$x = strstr ( $x, '.' );
Upvotes: 15
Reputation: 176743
If if the number is negative, you'll have to do this:
$x = abs($x) - floor(abs($x));
Upvotes: 9
Reputation: 11517
My PHP skills are lacking but you could minus the result of a floor from the original number
Upvotes: 3