开发者

Smarty if statements and big numbers

This code

{if 10111060108552250999929 == 10111060108552250999924} 
true1 
{/if} 
{if 10111060108552250999929 == 20111060108552250999924} 
true2 
开发者_如何学Go{/if}

outputs 'true1' but neither statement is true.

Does Smarty have a maximum size for comparing integers? If so, why does it appear to be a bit "fuzzy"?

Hopefully I've just made a stupid mistake.


It's the problem with PHP's integer type overflow. It varies based on the OS bit (32/64).

Please see:

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.

http://www.php.net/manual/en/language.types.integer.php#language.types.integer.overflow


Since these numbers are too large for the regular int type, Smarty will (as per the automatic conversion PHP does) be comparing them as floats, resulting in loss of precision.


You should consider writing a comparsion plugin for smarty usin BCMath, therefore big numbers will be handled correctly.


I might be wrong, but your second if statement is always false...as those numbers are not the same..

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜