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
1
vote
2 answers

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 to a float/double, and without dropping the === to…
Chris
  • 54,599
  • 30
  • 149
  • 186
1
vote
1 answer

Converting floats from db to be used with bcmath() OR how to use bcmath?

I am getting a couple of values from a database query. The values that I am getting look like: 10.810000000000000497 and I want to use just '10.81'. or 2.6899999999999999467 and I want to use just 2.69 when using ini_set('precision', 20); How how…
Valentin Despa
  • 40,712
  • 18
  • 80
  • 106
1
vote
2 answers

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 to divide it by 86.400 to get number of days…
bosniamaj
  • 838
  • 4
  • 10
  • 17
0
votes
1 answer

Are PHP BC Math functions automatically converting integers and flaots to strings?

Are PHP BC Math functions automatically converting integers and flaots to strings? The example below works fine on PHP 7.4 on windows, but some people claim that BC Math only accepts strings as input. Why is bcdiv always returning 0? For…
Jimski
  • 826
  • 8
  • 23
0
votes
1 answer

bcmath extention is enabled in my ubuntu system for php 8.2, but drupal 10 commerce module it is showing error

$ php -i | grep -i bcmath PHP Warning: Module "gd" is already loaded in Unknown on line 0 /etc/php/8.2/cli/conf.d/20-bcmath.ini, bcmath BCMath support => enabled bcmath.scale => 1 => 1 my docker compose file setup: version: '3.8' services: …
0
votes
1 answer

count significant digits in PHP

i am writing a class that does simple bcmath operations on numbers. tho i need to set the scale automatically throughout this so i need a way to determine how many significant digits a number has. for small and simple numbers converting it to string…
0
votes
0 answers

In PHP Is it better to leave the result of bcmath functions as strings or cast the result back to a float?

If I use a bcmath function, they all take values as strings and return a string. I have several functions that use bcmath in order to work on monetary values. The database stores these values as a DECIMAL(15,3) in most cases. Here is a code example…
objecttothis
  • 45
  • 1
  • 7
0
votes
0 answers

PHP Modules not matching with PHP Info file

So I am trying to install the bcmath module and it seems to have worked but when I check my phpinfo it says it is not on there. This is the output I get when I do php -m [PHP…
0
votes
0 answers

Twig fixed point arithmetic

I'm trying to do precise arithmetics calculations inside a Twig template, but I am struggling with floating point's precision error and I can't find a way to solve this. Is there a way to solve this in my Twig file(like a bcmath extension) or my…
Agnohendrix
  • 480
  • 1
  • 7
  • 17
0
votes
1 answer

Large number multiplication and division

I am trying the below mentioned code(PHP) to find probability. The calculation includes combination calculation of large numbers, using BCmaths function but not getting results. Please suggest, how this can be done. function combin($n, $r) { $C =…
0
votes
1 answer

Unable to install php7.1-bcmath ubuntu 14.04

I have a VPS with ubuntu 14.04, php7.1-fpm installed and running for ages. I am trying to install php7.1-bcmath. it keeps saying E: Unable to locate package php7.1-bcmath E: Couldn't find any package by regex 'php7.1-bcmath' I have already tried…
0
votes
1 answer

Install php7.1-bcmath on Debian

I'm trying to install bcmath for PHP 7.1 on Debian GNU/Linux 9 (stretch) : apt install php7.1-bcmath throw a not found error. Lecture des listes de paquets... Fait Construction de l'arbre des dépendances Lecture des informations d'état...…
fdehanne
  • 1,658
  • 1
  • 16
  • 32
0
votes
1 answer

why are extensions available on command line but not in script delivered by nginx?

Im running nginx/php-fpm server in a docker container and I added an extension with > docker-php-ext-install bcmath bcmath functions are available on command line but not in script i.e. this works: > bash-4.4# php -r "echo bcdiv('105', '6.55957',…
Laurence Fass
  • 1,614
  • 3
  • 21
  • 43
0
votes
1 answer

How can I base-convert a large/high-precision floating-point number in PHP?

As generic of a question as this seems, I'm having a really hard time learning specifically about how to base-convert large high-precision float values in PHP using BCMath. I'm trying to base-convert something…
i336_
  • 1,813
  • 1
  • 20
  • 41
0
votes
1 answer

MySQL how to preserve precisions when using division?

Let us say we have values we've retrieved in the tables, these values are strings so it's fine to divide them. We have this kind of report, but I don't like to handle it thru a Programming Language, yet to run it thru 1 sql query to retrieve all…
NosiaD
  • 587
  • 1
  • 6
  • 18