简体   繁体   English

PHP高精度数学 - 性能

[英]PHP High Precision Mathematics - Performance

My application (PHP) requires to perform lot of high precision mathematics (A total of 100 digits may even occur) 我的应用程序(PHP)需要执行大量高精度数学(甚至可能出现总共100位数)

Through last couple of posts in this forum, I could figure out that I have to use any high precision libraries like BC Math or GMP because the float type can not deal with lengthy data as their max size is limited 通过本论坛的最后几篇文章,我可以弄清楚我必须使用任何高精度库,如BC Math或GMP,因为float类型无法处理冗长的数据,因为它们的最大大小有限

Since my application requires decimals number and GMP do not support decimals, the choice is obvious that to go with BC Math 由于我的应用程序需要小数位数而GMP不支持小数,因此可以选择与BC Math一起使用

I am just trying to figure out the best approach now in terms of performance 我只是想在性能方面找出最好的方法

Case 1 : Check the numbers before any mathematical operation Perform calculations in the usual way if the result exceeds the floating limit, perform BC Math operations and get accurate result 情况1:在任何数学运算之前检查数字如果结果超出浮动限制,以通常的方式执行计算,执行BC数学运算并获得准确的结果

Case 2 : Directly use BC Math from the begining 案例2:从一开始就直接使用BC Math

Assume that 50% of calculations does not need high precision mathematics. 假设50%的计算不需要高精度数学。 In this case, which one will be better in terms of performance 在这种情况下,哪一个在性能方面会更好

Suggestions please 建议请

I used WolframAlpha to calculate some math problems which is hard to PHP. 我使用WolframAlpha来计算一些难以PHP的数学问题。 Therefore WolframAlpha is speedy from our computers and if you have an internet connection , you can use it. 因此,WolframAlpha在我们的计算机上很快,如果你有互联网连接,你可以使用它。 It's the example for calculate root of largest numbers. 这是计算最大数字的根的示例。 And the result is awfull , check it. 结果很糟糕,检查一下。

<?php
    function pow_a($a,$b){
    $a = file_get_contents("http://www.wolframalpha.com/input/?i=$a"."%5E"."$b");
    if(preg_match_all('/first\sbtn\"\>\<a\shref\=\"(.*?)\"/', $a, $m))
        $b = file_get_contents("http://www.wolframalpha.com/input/".htmlspecialchars_decode($m[1][0]));
        if(preg_match_all("/\"stringified\"\:\s\"(.*?)\.\.\./i", $b, $k))
            return $k[1][0];
    }
    echo pow_a("9999999999999999999999999", "0.2222");
?>

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

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