Request for Comments: PHP.INI Setting to Force All Math Operations Through BCMath Library


The problems of floating point math are well documented, and a grand gotcha to any non-computer science student user of PHP (or any other programming language). I still remember my shock and surprise at this (since I'm not a computer science student by background):

34.17 + 18.83 = 52.99999999999999999999999

The BCMath library exists to avoid this problem, but code can become very, very hard to read if a lot of numerical calculations are going on.


A php.ini setting that, when set true, routes all the floating point math operator functions to the bcmath library. If that library isn't enabled and this setting is set to true an E_ERROR is raised. With this setting in place

34.17 + 18.83 = 53.00

Assuming a scale of 2. The setting can be activated at runtime, by htaccess or in the core php.ini file.

Note that this proposal is just for the math operators, not for all the math functions. Even with this setting on, ++ and – would increment/decrement by 1.

Potential Problems

This will be a performance hit when enabled. Other than that though I can't think of any major problems.


Portable applications would have to perform QA stress tests under both settings to be sure the (probably major) performance issues aren't going to kill it.

Or it needs to be easily turned “off” from within an app and in .htaccess

Even then, somebody who turns this “on” and suddenly their entire website goes down because of the performance penalty is not going to be happy…

Can you write some sample code to measure the performance difference between normal PHP and re-writing the sample to use BCMath?


2012-03-06 Created.

rfc/php_ini_bcmath_default.txt · Last modified: 2017/09/22 13:28 (external edit)