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
5
votes
1 answer

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
user6514731
5
votes
1 answer

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 behaviour instead: // PHP: $a = bcdiv('15.80',…
Simone Bronzini
  • 1,057
  • 1
  • 12
  • 23
5
votes
1 answer

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 percentages to be applied). I've never programmed something…
bfavaretto
  • 71,580
  • 16
  • 111
  • 150
4
votes
2 answers

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 get the following error: Warning: bcdiv(): Division…
xeraphim
  • 4,375
  • 9
  • 54
  • 102
4
votes
1 answer

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); $fraction = bcmul($mul, bcpow(bcsub($n, '1',…
mpen
  • 272,448
  • 266
  • 850
  • 1,236
4
votes
1 answer

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", 4));' string(4) "0.06" The subtraction result is…
FtDRbwLXw6
  • 27,774
  • 13
  • 70
  • 107
4
votes
1 answer

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, power correct - bcpow(4, 0.5) // 1, square…
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
3
votes
1 answer

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 helpfull to me in fixing my previous bug that I…
Lejiend
  • 1,219
  • 2
  • 16
  • 24
3
votes
4 answers

confused by PHP's bcmul() scale

Why is this outputting 87.5 and not 87.50?
Dave Kiss
  • 10,289
  • 11
  • 53
  • 75
3
votes
1 answer

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 1){ $sum = bcmul($a,…
jay suthar
  • 79
  • 7
3
votes
3 answers

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 as few dependencies/requirements as possible, I…
Dmitri
  • 34,780
  • 9
  • 39
  • 55
3
votes
2 answers

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); $total_cost4 = bcadd($value7, $value8); $total_cost =…
bestprogrammerintheworld
  • 5,417
  • 7
  • 43
  • 72
3
votes
1 answer

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 : string(20) "100000000.0000000000" Non-working code…
Eric V.
  • 418
  • 7
  • 15
3
votes
1 answer

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 rate to 0.0001 then it outputs the correct…
Linkandzelda
  • 611
  • 14
  • 24
3
votes
5 answers

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') . '
'; Outputs 1 echo bcdiv('1', bcpow('10',…
Kuroki Kaze
  • 8,161
  • 4
  • 36
  • 48