Questions tagged [bcmath]

BC Math is a binary calculator for PHP which supports numbers of any size and precision, represented as strings.

Filter by
Sorted by
Tagged with
70votes
19answers
234kviews

How to install bcmath module?

How do I install the bcmath module on a server? I tried yum update php-bcmath but it said it found nothing.
user avatar
  • 2,254
26votes
4answers
77kviews

How to install bcmath in Ubuntu for PHP 7.1

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 ...
user avatar
22votes
6answers
13kviews

How to ceil, floor and round bcmath numbers?

I need to mimic the exact functionality of the ceil(), floor() and round() functions on bcmath numbers, I've already found a very similar question but unfortunately the answer provided isn't good ...
user avatar
  • 146k
17votes
2answers
5kviews

Arbitrary-Precision Math in PHP

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 ...
user avatar
15votes
14answers
8kviews

In PHP, how do I generate a big pseudo-random number?

I'm looking for a way to generate a big random number with PHP, something like: mt_rand($lower, $upper); The closer I've seen is gmp_random() however it doesn't allow me to specify the lower and ...
user avatar
  • 146k
13votes
2answers
22kviews

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

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: ...
user avatar
10votes
3answers
5kviews

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

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 ...
user avatar
9votes
2answers
39kviews

Enable BCMath using php.ini?

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?
user avatar
8votes
2answers
1kviews

PHP|BCMath: How to get bcscale value?

How do I get the scale set in BCMath's bcscale() method? Example: bcscale(25); How do I get the 25? Thanks!
user avatar
  • 398
7votes
1answer
1kviews

Calculating Floating Point Powers (PHP/BCMath)

I'm writing a wrapper for the bcmath extension, and bug #10116 regarding bcpow() is particularly annoying -- it casts the $right_operand ($exp) to an (native PHP, not arbitrary length) integer, so ...
user avatar
  • 146k
6votes
2answers
1kviews

PHP: How to raise number to (tiny) fractional exponent?

I'm doing a calculation in PHP using bcmath, and need to raise e by a fractional exponent. Unfortunately, bcpow() only accepts integer exponents. The exponent is typically higher precision than a ...
user avatar
  • 1,028
6votes
1answer
2kviews

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

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 ...
user avatar
5votes
2answers
2kviews

Calculating Nth root with bcmath in PHP

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 ...
user avatar
  • 1,991
5votes
1answer
755views

PHP bcmath versus Python Decimal

I am using PHP's bcmath library to perform operations on fixed-point numbers. I was expecting to get the same behaviour of Python's Decimal class but I was quite surprised to find the following ...
user avatar
4votes
1answer
3kviews

Can't figure out why I get App Engine flex "Uncaught Error: Call to undefined function Google\Protobuf\Internal\bccomp()"

I'm trying to implement Google's Text-To-Speech API on App Engine flex environment, but get this error: "PHP message: PHP Fatal error: Uncaught Error: Call to undefined function Google\Protobuf\...
user avatar
4votes
1answer
677views

Why does bcmul return a number with a scale different than the one I specified?

I can't seem to find anything in the php.net documentation that explains the following results: $ php -r 'var_dump(bcsub("0.3", "0.2", 4));' string(6) "0.1000" $ php -r 'var_dump(bcmul("0.3", "0.2", ...
user avatar
  • 26.2k
4votes
1answer
863views

Fast arbitrary-precision logarithms with bcmath

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); ...
user avatar
  • 253k
4votes
1answer
1kviews

Calculating roots with bc_math or GMP

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, ...
user avatar
  • 146k
4votes
1answer
1kviews

Accounting system - MySQL and PHP precision

I'm building a kind of accounting system with PHP and MySQL. My database has DECIMAL (11,2) columns for currency, and also DECIMALfor all other values used on operations with currency (like ...
user avatar
  • 70.3k
3votes
5answers
10kviews

Raising to power in PHP

Well, i need to do some calculations in PHP script. And i have one expression that behaves wrong. echo 10^(-.01); Outputs 10 echo 1 / (10^(.01)); Outputs 0 echo bcpow('10', '-0.01') . '<br/>...
user avatar
  • 7,592
3votes
4answers
1kviews

confused by PHP's bcmul() scale

Why is this outputting 87.5 and not 87.50? <?php $quantity = 25; switch ($quantity) { case ($quantity <= 50): $price = 3.50; break; case ($quantity <= 100): ...
user avatar
  • 9,956
3votes
2answers
3kviews

Convert Wei to Ethereum with php

I'm trying to convert wei to eth by using php and the bc-math extension. when trying to convert it using this function: function wei2eth($wei) { return bcdiv($wei,1000000000000000000,18); } I ...
user avatar
  • 3,871
3votes
3answers
3kviews

What is the difference between bcpow and pow?

Can someone explain to me if I should use bcpow() instead of pow() and why? I understand that not all installations of php have bcmath enabled. So if I write an open source project, and want to have ...
user avatar
  • 31.7k
3votes
2answers
1kviews

How to add many values in a good way with bcmath?

If I want to add several values together with BCMath I could do like this: $total_cost1 = bcadd($value1, $value2); $total_cost2 = bcadd($value3, $value4); $total_cost3 = bcadd($value5, $value6); $...
user avatar
3votes
1answer
1kviews

Bcmul reporting 0

I have a simple piece of code as below. $amount = 447274.44882; $rate = 0.00001; echo floatNumber(bcmul($amount, $rate, 8), 8); This outputs 0.00000000 when it should be 4.47274449. If I change the ...
user avatar
3votes
1answer
1kviews

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

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 : ...
user avatar
  • 409
3votes
1answer
68views

echo and return print different values

I am working on php bcmath extention for factorial calculation and i find that echo and return cause different result This Code generate wrong result <?php $a = 25; function test($a){...
user avatar
2votes
1answer
4kviews

How to install bcmath on a debian:jessie php5.6?

I am running a linux instance on a docker container. It is a debian:jessie with a php 5.6. I want to install bcmath module, but I have no success. Does someone know how o do that? root@1419c032c35f:...
user avatar
  • 4,356
2votes
1answer
13kviews

Fatal error: Call to undefined function bccomp()

I am getting this error PHP Fatal error: Call to undefined function bccomp() I am using Ubuntu, and I installed PHP-5.6 using APT-GET command
user avatar
2votes
1answer
899views

Why subtraction doesn't work properly using bcmath?

Let's say I have $foo = bcsub(bcdiv(1, 3, 20), 0.00001, 20); it returns me 0.33333333333333333333 If I have $foo = bcsub(bcdiv(1, 3, 20), 0.0001, 20); it returns me 0.33323333333333333332 If I ...
user avatar
  • 1,765
2votes
3answers
4kviews

Drupal 8 - Commerce Module - BC math PHP extension not found

I'm trying to install the commerce module in Drupal 8 however I get the error 'BC math PHP extension not found'. I've searched for this problem and tried different things such as editing the PHP.ini ...
user avatar
  • 25
2votes
2answers
2kviews

bash bcmath functions

I have two functions for GNU bc in a Bash script. BC_CEIL="define ceil(x) { if (x>0) { if (x%1>0) return x+(1-(x%1)) else return x } else return -1*floor(-1*x) }\n" BC_FLOOR="define floor(x) { ...
user avatar
  • 1,758
2votes
1answer
332views

php - bcadd has different point precision

On my windows box when I run $SR = "0"; $SPR = "149"; $SR = bcadd($SR, $SPR); echo "$SR"; It outputs 149.0000000000 But when I upload the same code to my Linux host, the output is 149. ...
user avatar
  • 4,364
2votes
1answer
204views

Getting the modulo of two real numbers with BCMath

I would like to find out whether one real number is a multitude of the other one. I'm using BC-Math because the code is part of a larger system doing price calculations and using floats results in ...
user avatar
  • 3,283
2votes
2answers
526views

Why negative zero does not compare equal to positive zero using bccomp in PHP?

I try comparing "+0.00000000000" with "+0.00000000000" using bccomp. I expect the result to be 0, but actually get a 1. $ cat bcmath.php <?php var_dump(bccomp("+0.00000000000","-0.00000000000")); ?...
user avatar
  • 465
2votes
1answer
3kviews

php calculate discount or tax

I'm trying to calculate a tax or discount value using BCMATH in PHP. I need 2 DECIMAL PLACES. Here's how i'm doing it: bcscale(2); $price = '60.67'; $discount = bcmul(bcdiv($price, 100), '3.8'); // ...
user avatar
  • 3,846
2votes
1answer
741views

BCmath php 64bit

i'm using BCmath to do math with 64 bit unsigned integers, i want to make a bcmath-object to a normal int (high, low part) how can i achieve that ? Thank you for your help
user avatar
  • 23
2votes
1answer
711views

javascript equivalent of bcpowmod

I'm coding a javascript version that can decode a RSA encryption I made in PHP. everything works fine except I don't have a javascript equivalent of bcpowmod. I used the PHP JS library for the other ...
user avatar
  • 1,142
2votes
1answer
655views

Error using BCMath - Call to undefined method PEAR_Error::int2bin()

I am having this error while using BCMath - Fatal error: Call to undefined method PEAR_Error::int2bin() in login.php on line 23 I am trying to use Crypt_RSA and BCMath together. Here is my code ...
user avatar
2votes
0answers
116views

BCMath code for converting Hex to Dec - Issues - A bug in code?

I have some issue with a code trying to convert Hex values into decimal one. No matter if it is small or large integer, sometimes, the function returns me the Hex converted to Dec with 8 ceros as ...
user avatar
2votes
0answers
2kviews

Enable bcmath module PHP 7.2 server CLI

I have installed the php bcmath module for 7.2 via the command: yum install php-bcmath I can see that it's there, but it's not in the php ini file when I check with command php -i How can I enable ...
user avatar
1vote
2answers
5kviews

Docker: the requested PHP extension bcmath is missing from your system

I am getting below error while installing bcmath root@cfe90ea33de0:/var/www/web# apt install php-bcmath Reading package lists... Done Building dependency tree Reading state information... Done ...
user avatar
  • 1,138
1vote
4answers
1kviews

bcmath seems to give the wrong answer to my calculation

I am not sure what I am doing wrong but this calculation bcscale(20); echo bcmul(bcdiv('422218', '2388865'), '473'); echoes "83.59999999999999999670" but every other calculator gives me 83.6. Is ...
user avatar
  • 13
1vote
1answer
3kviews

PHP BCMath cannot handle the exponential number if it is passed to its function, PHP BCMath return " bcmath function argument is not well-formed"

I working on the few of the small decimals like 0.0000687, 0.0000063241, 0.0000454. I used BCMath as to get the most precise result because it involved with money calculation, so far BCMath it is very ...
user avatar
1vote
2answers
237views

Two files, exactly the same code, different output?

This is the weirdest thing that has ever happened to me since I am a (PHP) programmer... I have two files, with the following code (proj. euler stuff) that return different outputs. <?php $...
user avatar
  • 6,328
1vote
2answers
757views

bcmath sometimes returns float, and sometimes round result

I have a variable $newTime, which is time in seconds made by mktime(), and I want to convert it into number of days. In this case it is over 86.400 (which is a number of seconds in a day), and I try ...
user avatar
  • 768
1vote
1answer
169views

bcmath operations with very small numbers

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 ...
user avatar
1vote
3answers
704views

Trying to decrypt a PHP PseudoCrypt class

I am trying to create a way to reverse the PseudoCrypt script listed at: http://blog.kevburnsjr.com/php-unique-hash. In this code it has the following equation: $dec = ($num * $prime)-floor($num * $...
user avatar
  • 292
1vote
1answer
581views

Why is bcmath in php 4.3 so much faster than php 5+?

Decided to run a quick test to see how bcmath operates on various versions of PHP, and noticed that the latest and greatest is lacking significantly in speed when compared to 4.3, I am wondering if ...
user avatar
  • 9,288
1vote
2answers
92views

Is there a consistent way to test for 0 (zero) when using BCMath?

Running the following, I would expect to receive N, Y, Y. I understand why I'm not, because '0.00' != '0' for the second example, but is there a consistent way of testing for 0 without casting back ...
user avatar
  • 49.2k

15 30 50 per page