bcmath

Calculating Nth root with bcmath in PHP

断了今生、忘了曾经 提交于 2020-01-01 05:44:10
问题 We are looking for the Nth root in PHP. We need to do this with a very large number, and the windows calculator returns 2. With the following code we are getting 1. Does anybody have an idea how this works? echo bcpow(18446744073709551616, 1/64); 回答1: Well it seems that PHP and the BC lib has some limits, and after searching on the internet i found this interesting article/code: So you should use this function: <?php function NRoot($num, $n) { if ($n<1) return 0; // we want positive exponents

How to round/ceil/floor a bcmath number in PHP?

。_饼干妹妹 提交于 2019-12-29 07:30:48
问题 Is there any library function for this purpose, so I don't do it by hand and risk ending in TDWTF? echo ceil(31497230840470473074370324734723042.6); // Expected result 31497230840470473074370324734723043 // Prints <garbage> 回答1: This will work for you: $x = '31497230840470473074370324734723042.9'; bcscale(100); var_dump(bcFloor($x)); var_dump(bcCeil($x)); var_dump(bcRound($x)); function bcFloor($x) { $result = bcmul($x, '1', 0); if ((bccomp($result, '0', 0) == -1) && bccomp($x, $result, 1))

bcdiv using very small float with scientific notation cause “Division by zero” error

牧云@^-^@ 提交于 2019-12-22 10:50:02
问题 Using bcdiv, i can't divide with small float using scientific notation : Working code : bcscale(30); $a = '1' ; $b = '0.00000001'; $result = bcdiv($a, $b); var_dump($result); Results in : string(20) "100000000.0000000000" Non-working code : bcscale(30); $a = '1' ; $b = '1e-8'; $result = bcdiv($a, $b); var_dump($result); Results in : Warning: bcdiv() [function.bcdiv]: Division by zero in C:\wamp\www\utilitaires\test_bcdiv.php on line XX NULL How can i do this division properly, with the less

Arbitrary-Precision Math in PHP

淺唱寂寞╮ 提交于 2019-12-18 19:07:25
问题 I'm currently trying to figure out how to work with arbitrary-precision numbers in PHP. So I guess my first question would be what exactly is arbitrary-precision math. I tried Googling for a good definition but for some reason nobody can put it in simple enough words. Second, what are the differences between the BCMath and GMP libraries in PHP? I've heard claims that GMP's API is "fresher", but idk. Is one better? And my final question would be what type of numbers BCMath/GMP takes. Obviously

Fast arbitrary-precision logarithms with bcmath

隐身守侯 提交于 2019-12-12 11:40:12
问题 Here's what I've got function bcln($n, $scale=10) { $iscale = $scale+3; $result = '0.0'; $i = 0; do { $pow = (1 + (2 * $i++)); $mul = bcdiv('1', $pow, $iscale); $fraction = bcmul($mul, bcpow(bcsub($n, '1', $iscale) / bcadd($n, '1.0', $iscale), $pow, $iscale), $iscale); $lastResult = $result; $result = bcadd($fraction, $result, $iscale); } while($result !== $lastResult); return bcmul('2', $result, $scale); } But this takes 5.7 seconds to run bcln(100) (natural log of 100, 10 decimal places).

bcmath operations with very small numbers

天涯浪子 提交于 2019-12-11 07:22:00
问题 I want to use bcmath for precise operations with very small numbers, but it fails. I am trying to calculate cryptocurrency prices and thought that bcmath is better than converting float to integers This working: php > echo number_format(0.000005 * 0.0025, 10); 0.0000000125 And this is not working: php > echo number_format(bcmul(0.000005, 0.0025, 10), 10); 0.0000000000 php > echo number_format(bcadd(0.000005, 0.00000025, 10), 10); 0.0000000000 Is there some configurations for bcmath or this is

Enable BCMath using php.ini?

烈酒焚心 提交于 2019-12-09 16:39:27
问题 I need to enable BC Math, but I don't want to do it using --enable-bcmath, primarily because I don't understand that route. Is there a way to do this using php.ini only? 回答1: To the best of my knowledge you must compile php with the --enable-bcmath option. Without it, the required code won't exist in the binary. Therefore, there is nothing that you can set in php.ini 回答2: Before recompiling, check the php.ini file and search for "bcmath". You may find bcmath.scale=0. If so, change the 0 to a

Fatal error: Uncaught Error: Call to undefined function bcadd()

霸气de小男生 提交于 2019-12-08 19:05:23
问题 After installed "eduTrac SIS" and accessing "dashboard" got this error Ubuntu 16.4, PHP 7.0(php7.0-fpm), Apache2, Nginx, URL gives error 500 and nginx/error.log displays, FastCGI sent in stderr: "PHP message: PHP Fatal error: Uncaught Error: Call to undefined function PHPBenchmark\bcadd() in /var/www/html/eduTrac-SIS/app/src/vendor/phpbenchmark/phpbenchmark/lib/PHPBenchmark/Utils.php:18 回答1: PHP does not recognize "bcadd()" gives the error. "bcadd()" function is included in "bcmath" PHP

Should I use BCMath for values with about 1,2 or 3 decimals?

狂风中的少年 提交于 2019-12-07 07:20:55
问题 I have about 10-15 numbers with precision of 1, 2 or 3 decimals in my db, both signed and unsigned. Example of datatypes being used: decimal(10,3), decimal(10,2), decimal(10,1) I'm calculating them in PHP something like this: $result = ($value1from_col1 + ($value2from_col2 * 30)/500) * 0.453; I'm then using some round() functions like this: $result_round = round($result, 2, PHP_ROUND_HALF_UP); Result of $result_round would be at largest: 100.000,999 I'm checking this: How much precision for a

centos安装PHP扩展(bcmath)

烈酒焚心 提交于 2019-12-06 06:31:24
东西多容易忘记!写下来备份; linux下用phpize给PHP动态添加扩展 使用php的常见问题是编译php时忘记添加某扩展,后来想添加扩展,但是因为安装php后又装了一些东西如PEAR等,不想删除目录重装,这里就需要用到phpize了。 如我想增加bcmath扩展的支持,这是一个支持大整数计算的扩展。windows自带而且内置,linux“本类函数仅在 PHP 编译时配置了 --enable-bcmath 时可用”(引号内是手册中的话) 注意,有些扩展需要和php的版本保持一致才可以的. 解压bcmath包,进入里面的ext/bcmath目录,然后执行 /usr/local/php/bin/phpize ,(其实在PHP源码安装包里面进行)phpize在php安装完以后会有这个命令的, 会发现当前目录下多了一些configure文件,然后再执行./configure命令即可. #/usr/local/php/bin/phpize #./configure --with-php-config=/usr/local/php/bin/php-config 注意要先确保 /usr/local/php/bin/php-config 存在。 (如果你的php安装路径不是默认的,请修改为php安装的路径) 如果没有报错,则make,再make install ,然后它告诉你一个目录.