开发者

PHP - Why does my computation produce a different result when I assign it to a variable?

开发者 https://www.devze.com 2022-12-27 20:45 出处:网络
I want to round a number to a specific number of significant digits - basically I want the following function:

I want to round a number to a specific number of significant digits - basically I want the following function:

round(12345.67, 2) -> 12000
round(8888, 3) -> 8890

I have the following, but there's a strange problem.

function round_to_sf($number, $sf) 
{
 $mostsigplace = floor(log10(abs($number)))+1;
 $num = $number / pow(10, ($mostsigplace-$sf));

 echo ($number / pow(10, ($mostsigplace-$sf))).' '.$num.'<BR>';
}

round_to_sf(41918.522, 1);

Produces the following output:

4.1918522 -0

How can the result of a computation be different when 开发者_如何转开发it's assigned to a variable?


Using the commenting-out binary search method of debugging, I narrowed this down.

Apparently the following line, in another function, in a totally different file even, is the problem.

$diff = date_diff(new DateTime($lastdate), new DateTime("NOW"));

If I comment that out, I get a correct result from my rounding function.

Can anyone tell me what the .... is going on here? This had me ripping my hair out for a day. It also caused other bugs that looked like memory stomps - I'd run a calculation that should produce a float foo, and foo would get used in other calculations that produced correct output, but echoing foo would show A.KIPGGGGGGGGG.

0

精彩评论

暂无评论...
验证码 换一张
取 消