如何在PHP中两个相等的数字不相等

时间:2011-08-20 05:00:13

标签: php

我有两个数字应该等于返回差异,我没有意义......

能够在这里重现此问题的唯一方法是我必须对我的数组进行base64_encode,

这是脚本:

基本上脚本会将“1 234,5”之类的数字修改为“1234.5”并进行计算,但最终返回

            First Number: 4784.47
            Second Number: 4784.47
            Difference: 9.0949470177293E-13

我只是不明白?????????


            $aa = 'YTo3OntpOjA7YToxMDp7czoxMToiRGVzY3JpcHRpb24iO3M6MTI6IkvDqWsgc3rDoW1vayI7czo2OiJQZXJpb2QiO3M6MTI6IjA4LjEwLTA4LjEwLiI7czo2OiJBbW91bnQiO3M6MToiMSI7czoxMjoiRHVyYXRpb25UaW1lIjtzOjc6IjA6MDQ6MDAiO3M6MTI6Ik5ldFVuaXRQcmljZSI7czo1OiIzNSwyMCI7czo5OiJOZXRBbW91bnQiO3M6NjoiMTQwLDgwIjtzOjEwOiJWQVRQZXJjZW50IjtzOjM6IjI1JSI7czozOiJWQVQiO3M6NToiMzUsMjAiO3M6MTE6Ikdyb3NzQW1vdW50IjtzOjY6IjE3NiwwMCI7czo4OiJJdGVtQ29kZSI7czo0OiJCTFVFIjt9aToxO2E6MTA6e3M6MTE6IkRlc2NyaXB0aW9uIjtzOjE3OiJCZWxmw7ZsZGkgaMOtdsOhcyI7czo2OiJQZXJpb2QiO3M6MTI6IjA3LjIyLTA4LjExLiI7czo2OiJBbW91bnQiO3M6MjoiMTIiO3M6MTI6IkR1cmF0aW9uVGltZSI7czo3OiIwOjIxOjQzIjtzOjEyOiJOZXRVbml0UHJpY2UiO3M6NToiMTUsMjAiO3M6OToiTmV0QW1vdW50IjtzOjY6IjMzMCwxMCI7czoxMDoiVkFUUGVyY2VudCI7czozOiIyNSUiO3M6MzoiVkFUIjtzOjU6IjgyLDUyIjtzOjExOiJHcm9zc0Ftb3VudCI7czo2OiI0MTIsNjIiO3M6ODoiSXRlbUNvZGUiO3M6ODoiRklYX0ZMQVQiO31pOjI7YToxMDp7czoxMToiRGVz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jMgODI3LDU4IjtzOjE0OiJTdW1tYXJ5SXRlbVZBVCI7czo2OiI5NTYsODkiO3M6MTc6IlN1bW1hcnlJdGVtQnJ1dHRvIjtzOjg6IjQgNzg0LDQ3Ijt9';

            $a = unserialize(base64_decode($aa));
            $t = unserialize(base64_decode($tt));


            function calculate_call_fees($a,$t){

                $or_item = 0;

                foreach($a as $k => $r) {
                    $or_item += fix_num($r['GrossAmount']); 
                }        

                $br =  fix_num($t['SummaryItemBrutto']);   

                if($br>$or_item){
                    $diff = $br-$or_item;
                } else {
                    $diff = 0;
                }

                echo 'First Number: ' . $br.'<br/>';
                echo 'Second Number: ' . $or_item.'<br />';
                echo 'Difference: ' . $diff.'<br />';

                echo '<hr />';
                echo '<pre>';
                print_r($a);
                echo '</pre>';
                echo '<hr />';
                echo '<pre>';
                print_r($t);
                echo '</pre>';    

            }

            function fix_num($n){
                return floatval(str_replace(Array(" ",","),array("","."),$n));
            }

            calculate_call_fees($a,$t);

3 个答案:

答案 0 :(得分:3)

使用“等于”与浮点数进行比较是危险的,因为浮点数限制精度 - 由于涉及舍入,您可能会得到很小的差异。

相反,如果您想查看两个浮点数是否“相同”,只需看看它们的差异是否低于某个阈值:

if( abs($a - $b) < 0.00000001) {
    // a and b are "equal"
}

答案 1 :(得分:1)

这不仅仅是PHP。存在表示计算机中的分数的一般问题。它受各种类型的溢出,下溢,精度问题等的影响。 PHP's manual阐明了这个话题。

一般规则 - 如果要求两个'似乎相等'的数字保证相等 - 不要使用浮点数据类型(浮点数,双精度数),而是使用固定点数(十进制数字)

答案 2 :(得分:1)

看来这是“机器epsilon”问题: http://en.wikipedia.org/wiki/Machine_epsilon

尝试将它们之间的差异与0.000001进行比较,而不是直接比较它们。