简体   繁体   English

如何在PHP中比较大数?

[英]How to compare large number in PHP?

I confuse that how I aseert quite large number by PHPUnit. 我混淆了我如何通过PHPUnit获得相当大的数字。 I wrote fibonacci number. 我写了斐波纳契数。 But in case 100, I wrote below. 但在案例100中,我在下面写道。

http://github.com/sanemat/kata-fibonacci-phpunit/commit/b6b2945d2eff1b95e7e25b8be8c7bff11098d44d

I expect this return TRUE. 我希望这个返回TRUE。 It failed. 它失败了。

http://www.php.net/manual/en/language.types.float.php

I understood 'never compare for equality.' 我理解'从不比较平等'。 and so how to aseert it? 那么如何看待它呢? At the end, I compare with to_string each other. 最后,我与to_string相互比较。 How to compare large number in PHP ? 如何在PHP中比较大数?

http://github.com/sanemat/kata-fibonacci-phpunit/commit/70a0e47bc0737be47da8fe8726634edd851d940d

I feel it as bad smell. 我觉得这很难闻。 But I do not know better way. 但我不知道更好的方法。 Please tell me how do you assert it? 请告诉我你是如何断言的?

====
php > $a = 354224848179261915075;
php > echo $a;
3.5422484817926E+20
php > $b = 3.5422484817926E+20;
php > echo $b;
3.5422484817926E+20
php > echo ($a == $b) ? 'TRUE' : 'FALSE';
FALSE
php > echo gmp_cmp($a, $b);
PHP Warning:  gmp_cmp(): Unable to convert variable to GMP - wrong type in php shell code on line 1
php > echo (strval($a) == strval($b)) ? 'TRUE' : 'FALSE';
TRUE
====

environment
$ uname -a
Linux localhost.localdomain 2.6.33.6-147.2.4.fc13.x86_64 #1 SMP Fri Jul 23 17:14:44 UTC 2010 x86_64 x86_64 x86_64 GNU/Linux
$ php --version
PHP 5.3.3 (cli) (built: Jul 22 2010 15:57:00) 
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

为了处理大数,有PHP的BCMath libray

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM