Why is this microtime showing up weird in PHP
$start4 = microtime(true);
// run some php code
$end4 = microtime(true);
print "Time4: ". ($end4 - $start4)."<br />";
The above is showing:
Time4: 2.69412994385E-5
Something with a more complex, longer running process shows up like this instead:
Time1: 0.000292062759399
E-5 is scientific notation. Seems to happen when you concatenate it with the string value. Try using number_format... ?
print "Time4: ". number_format($end4 - $start4, 10)."<br />";
//or use: printf(".10f", $end - $start)
It's normal for long numbers (very small or very large) to be converted to powers of 10. The E-5 just means that the number displayed is beeing multiplied by (10/10/10/10/10) which makes it a very small number.
0.000000000000123 is much harder to read than
1.23E-13 (for example).
Nevertheless if you do want to view the number in another format:
$start = microtime(true);
$end = microtime(true);
echo "Time: ", number_format($end - $start, 50);
This will add 50 decimal houses to the number on display.
Hope it helps!
It seems that this microtime() is showing up weird because PHP has a threshold, on either side of which it displays either a number in scientific notation or one in decimal notation. Both of these are technically "floats" (see documentation).
It seems that this threshold is somewhere between 0.8 seconds and 0.9 seconds; at least that's what my tests concluded. Using the following code will show scientific notation:
$start4 = microtime(true);
sleep(0.8);
$end4 = microtime(true);
echo 'Time4: ' . ($end4 - $start4) . '<br />';
But if we change our wait time to sleep(0.9), a decimal number is produced. This may or may not be the case on all systems or installations, but this is at least what my tests showed.
You can counteract this yourself by using the sprintf() function, like this:
$start4 = microtime(true);
sleep(0.8);
$end4 = microtime(true);
echo 'Time4: ' . sprintf('%f', $end4 - $start4) . '<br />';
This will always show the time as a decimal number.
Dont forget float:
number_format( (float) microtime(true), 10 );
------------------^
update: appended to top answer.
Related
this might be a stupid question but I have searched again and again without finding any results.
So, what I want is to show all the decimal places of a number without knowing how many decimal places it will have. Take a look at this small code:
$arrayTest = array(0.123456789, 0.0123456789);
foreach($arrayTest as $output){
$newNumber = $output/1000;
echo $newNumber;
echo "<br>";
}
It gives this output:
0.000123456789
1.23456789E-5
Now, I tried using 'number_format', but I don't think that is a good solution. It determines an exact amount of decimal places, and I do not know the amount of decimal places for every number. Take a look at the below code:
$arrayTest = array(0.123456789, 0.0123456789);
foreach($arrayTest as $output){
$newNumber = $output/1000;
echo number_format($newNumber,13);
echo "<br>";
}
It gives this output:
0.0001234567890
0.0000123456789
Now, as you can see there is an excess 0 in the first number, because number_format forces it to have 13 decimal places.
I would really love some guidance on how to get around this problem. Is there a setting in PHP.ini which determines the amount of decimals?
Thank you very much in advance!
(and feel free to ask if you have any further questions)
It is "impossible" to answer this question properly - because a binary float representation of a decimal number is approximate: "What every computer scientist should know about floating point"
The closest you can come is write yourself a routine that looks at a decimal representation of a number, and compares it to the "exact" value; once the difference becomes "small enough for your purpose", you stop adding more digits.
This routine could then return the "correct number of digits" as a string.
Example:
<?php
$a = 1.234567890;
$b = 0.123456789;
echo returnString($a)."\n";
echo returnString($b)."\n";
function returnString($a) {
// return the value $a as a string
// with enough digits to be "accurate" - that is, the value returned
// matches the value given to 1E-10
// there is a limit of 10 digits to cope with unexpected inputs
// and prevent an infinite loop
$conv_a = 0;
$digits=0;
while(abs($a - $conv_a) > 1e-10) {
$digits = $digits + 1;
$conv_a = 0 + number_format($a, $digits);
if($digits > 10) $conv_a = $a;
}
return $conv_a;
}
?>
Which produces
1.23456789
0.123456789
In the above code I arbitrarily assumed that being right to within 1E-10 was good enough. Obviously you can change this condition to whatever is appropriate for the numbers you encounter - and you could even make it an optional argument of your function.
Play with it - ask questions if this is not clear.
How can I get php to not use 1.297503E+17 on large int but 129750300000000000
code:
$dag = 29;
$maand = 03;
$jaar = 2012;
$expdate = $dag . "-" . $maand . "-" . $jaar;
$unixstamp = strtotime($expdate);
echo $unixstamp."<br />";
$winstamp = ($unixstamp + 11644560000) * 10000000;
I'm trying to use the number for a Timestamp in ldap.
That's what I would do (tested on 32b platform)
>> number_format(1.297503E+17,0,'.','')
'129750300000000000'
just be aware, that what you get back is a string, an will be converted back to float if you try doing any arithemtics on it. If you need to do math on large integers look into bc_math extension
PHP internally uses big enough integers. Your problem here is the use of echo:
printf ("%d", $winstamp);
$winstamp++;
printf ("%d", $winstamp);
output:
129775320000000000
129775320000000001
Hope this helps
echo rtrim(sprintf("%0.15f", $winstamp), "0.");
This uses sprintf to print a maximum of 15 decimal places, and then trims off any trailing 0 or . chars. (Of course, there's no guarantee that everything will be rounded nicely with trailing zeros as you might expect.)
If you just want a fixed size, then you can adjust the 15 and remove the rtrim.
Apparently, when PHP encounters a number that exceeds the upper limit of 2,147,483,647 for an integer, it automatically converts the number’s type from integer into a double.
Fortunately, we can format these numbers in scientific notation back to their standard integer form using the number_format() function. Here is how to do it:
$winstamp = 1202400000;
$formatted_stamp = number_format($winstamp , 0, '.', '');
echo $formatted_stamp; //outputs 1202400000 as expected
How can I regenerate random decimal from -0.0010 to 0.0010 with php rand() or some other method?
Divide rand() by the maximum random numer, multiply it by the range and add the starting number:
<?php
// rand()/getrandmax() gives a float number between 0 and 1
// if you multiply it by 0.002 you'll get a number between 0 and 0.002
// add the starting number -0.001 and you'll get a number between -0.001 and 0.001
echo rand()/getrandmax()*0.002-0.001;
?>
.
$val = (rand(0,20)-10)/10000;
This uses two rand() calls but I think that the readability makes up for it tenfold.
The first part makes either a -1 or +1. The second part can be anything between 0 and your limit for +/- numbers.
$rand = (rand(0,1)*2-1)*rand(0, 100);
echo $rand;
Unless you require LOTs of random numbers in a gigantic loop, you probably won't even notice the speed difference. I ran some tests (50.000 iterations) and it came up to around 0.0004 milliseconds to get a random number by my function. The alternatives are around half that time, but again, unless you are inside a really big loop, you are probably better of optimizing somewhere else.
Speed testing code:
$start = microtime();
$loopCount = 50000;
for($i=0;$i<$loopCount;$i++)
{
(0*2-1)*rand(0, 100);
}
$end = microtime();
echo "Timing: ", ((($end-$start)*1000.0)/((float)$loopCount)), " milliseconds.";
This will return any possible number between -0.001 and +0.001
$random = ((rand()*(0.002/getrandmax()))-0.001)
// or without paranthesis:
$random = rand()*0.002/getrandmax()-0.001
$randselect=rand(0,(array_sum($adarray)*100000000));
$cumilativevalue=0;
foreach ($adarray as $key => $value) {
$cumilativevalue=$cumilativevalue+$value*100000000;
if($randselect<$cumilativevalue){$selectedad=$key;break;}
}
Random float with one decimal between -1,1
$random = round((rand(0,1) - floatVal('0.'.rand(0,9).rand(0,9))), 1);
I'm having a terrible time convincing myself what I've done here is a good idea. The specific section I find objectionable is:
return ((float)($now+$sec).'.'.$mic);
In order to preserve the floating point precision, I'm forced to either fall back on the BC or GMP libraries (neither of which is always available). In this case, I've resorted to jamming the numbers together with string concatenation.
<?php
// return the current time, with microseconds
function tick() {
list($sec, $mic, $now) = sscanf(microtime(), "%d.%d %d");
return ((float)($now+$sec).'.'.$mic);
}
// compare the two given times and return the difference
function elapsed($start, $end) {
$diff = $end-$start;
// the difference was negligible
if($diff < 0.0001)
return 0.0;
return $diff;
}
// get our start time
$start = tick();
// sleep for 2 seconds (should be ever slightly more than '2' when measured)
sleep(2);
// get our end time
$end = tick();
$elapsed = elapsed($start, $end);
// should produce output similar to: float(2.00113797188)
var_dump($elapsed);
?>
If I attempt to add two numbers like 123456789 (representing a timestamp) and 0.0987654321 (representing microseconds), using the addition operator (+) I invariably end up with 123456789.099. Even when casting the integer to float, the result is the same.
Is there a solution for this issue which is 1) not a hack and 2) doesn't involve string concatenation? I shouldn't have to fall back on this sort of garbled code in order to get an accurate timestamp with microsecond resolution.
Edit: As S. Gehrig has explained, floating point numbers in PHP can be, at times, a bit tricky to display. The "precision" indicated in the PHP configuration is regarding display. The actual values are not rounded like I thought. A far simpler solution to the above code would look like so:
// return the current time, with microseconds
function tick() {
return microtime(true);
}
// compare the two given times and return the difference
function elapsed($start, $end) {
return $end-$start;
}
// get our start time
$start = tick();
// sleep for 2 seconds (should be ever slightly more than '2' when measured)
sleep(2);
// get our end time
$end = tick();
$elapsed = elapsed($start, $end);
// should produce output similar to: float(2.00113797188)
var_dump($elapsed);
If you were to examine $start or $end before subtracting one from the other, it might appear they were rounded to the hundredths position. This is not the case. It seems arbitrary precision is maintained for arithmetic while the display is limited.
Why don't you use microtime(true) which simply returns a microsecond timestamp as float? The parameter [bool] $get_as_float was added in PHP 5.0.0.
Regarding the comment about the "loss" of precision:
$start = microtime(true);
$end = microtime(true);
echo $end - $start;
// prints 7.1526861190796
microtime(true) is not limited to 2 decimal places. What the poster encounters is the effect of the configuration setting precision which controls how many decimal places will be printed when outputting float variables. This has nothing to do with the internal precision microtime(true) uses. You can always use number_format() or (s)printf() to format the output to the precision you like.
First, spligak, I see that your code contains an error.
list($sec, $mic, $now) = sscanf(microtime(), "%d.%d %d");
return ((float)($now+$sec).'.'.$mic);
If $mic has fewer than six digits, you get garbage results. Do a desk check on
the case where microtime() returns "0.000009 1234567890"
Second, you can greatly reduce the floating-point error as follows:
(WARNING: untested code!)
// compare the two given times and return the difference
// get our start time
$start = microtime();
// sleep for 2 seconds (should be ever slightly more than '2' when measured)
sleep(2);
// get our end time
$end = microtime();
// work around limited precision math
// subtract whole numbers from whole numbers and fractions from fractions
list($start_usec, $start_sec) = explode(" ", $start);
list($end_usec, $end_sec) = explode(" ", $end);
$elapsed = ((float)$end_usec)-((float)$start_usec);
$elapsed += ((float)$end_sec)-((float)$start_sec);
// please check the output
var_dump($elapsed);
Floating point types are inherently imprecise. Either live with it, or don't use them.
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.
$x = $x - floor($x)
$x = fmod($x, 1);
Here's a demo:
<?php
$x = 25.3333;
$x = fmod($x, 1);
var_dump($x);
Should ouptut
double(0.3333)
Credit.
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, '.' );
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)
If if the number is negative, you'll have to do this:
$x = abs($x) - floor(abs($x));
My PHP skills are lacking but you could minus the result of a floor from the original number
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 .75
With the other solutions, you will repeat 0 as integer below, and worse, you will get reversed fractional values for all negative numbers.
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);
You can use fmod function:
$y = fmod($x, 1); //$x = 1.25 $y = 0.25
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;