我在PHP中想要比较两个浮点数,由于内部比较容易出错,所以我使用了GMP库,但是在简单的比较时它给了我一个错误
if (gmp_cmp(0.2, 0.1) > 0) echo "First number is bigger";
错误是“gmp_cmp():无法将变量转换为GMP-错误类型”。
我在PHP中想要比较两个浮点数,由于内部比较容易出错,所以我使用了GMP库,但是在简单的比较时它给了我一个错误
if (gmp_cmp(0.2, 0.1) > 0) echo "First number is bigger";
a = "0.2";
b = "0.1";
a_s = explode(".", $a); // split on decimal point
b_s = explode(".", $b);
if (gmp_cmp(a_s[0], b_s[0]) > 0 ||
(gmp_cmp(a_s[0], b_s[0]) == 0 && gmp_cmp(a_s[1], b_s[1]) > 0))
echo "First number is bigger";