Questions tagged [bcmath]

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

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

BC Math Functions

  • bcadd() ~ Add two arbitrary precision numbers
  • bccomp() ~ Compare two arbitrary precision numbers
  • bcdiv() ~ Divide two arbitrary precision numbers
  • bcmod() ~ Get modulus of an arbitrary precision number
  • bcmul() ~ Multiply two arbitrary precision number
  • bcpow() ~ Raise an arbitrary precision number to another
  • bcpowmod() ~ Raise an arbitrary precision number to another, reduced by a specified modulus
  • bcscale() ~ Set default scale parameter for all bc math functions
  • bcsqrt() ~ Get the square root of an arbitrary precision number
  • bcsub() ~ Subtract one arbitrary precision number from another
100 questions
70
votes
19 answers

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.
JDesigns
  • 2,284
  • 7
  • 25
  • 39
27
votes
4 answers

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 install apt install php-bcmath, it simply installed…
Vajira Lasantha
  • 2,435
  • 3
  • 23
  • 39
23
votes
6 answers

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 enough for me since it lacks support for negative…
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
18
votes
2 answers

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 definition but for some reason nobody can put it in…
parent5446
  • 898
  • 7
  • 17
16
votes
14 answers

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 upper boundaries only the number of bits per limb…
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
14
votes
2 answers

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: "PHP message: PHP Fatal error: Uncaught Error:…
Isanka Wijerathne
  • 3,746
  • 3
  • 26
  • 34
11
votes
3 answers

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 31497230840470473074370324734723043 // Prints
Eduardo Marinho
  • 446
  • 4
  • 14
10
votes
2 answers

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?
user1262516
9
votes
2 answers

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 Package php-bcmath is a virtual package provided…
Jackson
  • 1,426
  • 3
  • 27
  • 60
8
votes
2 answers

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!
Treecj
  • 427
  • 2
  • 19
7
votes
2 answers

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\Internal\bccomp() in …
7
votes
1 answer

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 them in PHP something like this: $result =…
bestprogrammerintheworld
  • 5,417
  • 7
  • 43
  • 72
7
votes
1 answer

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 when you try to calculate the square root (or any…
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
6
votes
3 answers

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 float will allow, so normal arithmetic functions…
jchamberlain
  • 1,038
  • 8
  • 26
6
votes
2 answers

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 works? echo bcpow(18446744073709551616, 1/64);
Deep Frozen
  • 2,053
  • 2
  • 25
  • 42
1
2 3 4 5 6 7