bcmath

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

我是研究僧i 提交于 2019-12-06 04:09:55
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 precision loss ? That is because, actually, bcmath doesn't support scientific notation. It isn't

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

情到浓时终转凉″ 提交于 2019-12-05 16:21:17
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 bcmath PHP library? and the answer states that it wouldn't be an issue if you're not using functions

Enable BCMath using php.ini?

老子叫甜甜 提交于 2019-12-04 03:16:57
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? 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 Before recompiling, check the php.ini file and search for "bcmath". You may find bcmath.scale=0. If so, change the 0 to a 2. 来源: https://stackoverflow.com/questions/16217871/enable-bcmath-using-php-ini

Calculating Nth root with bcmath in PHP

落花浮王杯 提交于 2019-12-03 16:22:16
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); HamZa 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 if ($num<=0) return 0; // we want positive numbers if ($num<2) return 1; // n-th root of 1 or 2 give

PHP, Call to undefined function bccomp [duplicate]

☆樱花仙子☆ 提交于 2019-12-02 06:23:11
问题 This question already has answers here : Reference - What does this error mean in PHP? (34 answers) Closed 2 years ago . I am getting this error PHP Fatal error: Call to undefined function bccomp() Anyone know how to solve it? --------------------edit ------------ I am using Ubuntu, and I installed PHP-5.6 using APT-GET command Thank you, 回答1: bccomp() function belongs to BCMath package; if it's undefined, apparently, there's something wrong with the package itself. To be more specific, it's

PHP, Call to undefined function bccomp [duplicate]

断了今生、忘了曾经 提交于 2019-12-02 02:03:55
This question already has an answer here: Reference - What does this error mean in PHP? 34 answers I am getting this error PHP Fatal error: Call to undefined function bccomp() Anyone know how to solve it? --------------------edit ------------ I am using Ubuntu, and I installed PHP-5.6 using APT-GET command Thank you, bccomp() function belongs to BCMath package; if it's undefined, apparently, there's something wrong with the package itself. To be more specific, it's not enabled (as since PHP 4.0.4, libbcmath is bundled with PHP). Quoting the docs : These functions are only available if PHP was

Arbitrary-Precision Math in PHP

一笑奈何 提交于 2019-11-30 17:48:55
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 it takes normal integers in string form (e.g. "5.34"), but I've seen implementations where BCMath

How to install bcmath in Ubuntu for PHP 7.1

◇◆丶佛笑我妖孽 提交于 2019-11-30 14:56:49
问题 I'm using an Ubuntu 16 server for testing with php7.1 . One of my app uses bcadd function. I know that I need to install bcmath module for that but I'm unable to find php7.1-bcmath . When I tried to install apt install php-bcmath , it simply installed php7.0-bcmath module. I was unable to find php7.1-bcmath module anywhere to install on my server. Does anybody has an idea? apt install php7.1-bcmath returned following, E: Unable to locate package php7.1-bcmath E: Couldn't find any package by

Calculating roots with bc_math or GMP

瘦欲@ 提交于 2019-11-29 11:20:20
I'm having trouble calculating roots of rather large numbers using bc_math, example: - pow(2, 2) // 4, power correct - pow(4, 0.5) // 2, square root correct - bcpow(2, 2) // 4, power correct - bcpow(4, 0.5) // 1, square root INCORRECT Does anybody knows how I can circumvent this? gmp_pow() also doesn't work. I'm not a PHP programmer but looking at the manual it says you have to pass them in as strings i.e. bcpow( '4', '0.5' ) Does that help? Edit : The user contributed notes in the manual page confirm that it doesn't support non-integer exponents. I did come across this discussion of a PHP N

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

北战南征 提交于 2019-11-29 09:39:14
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> 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)) $result = bcsub($result, 1, 0); return $result; } function bcCeil($x) { $floor = bcFloor($x); return bcadd(