Extension:BCmath
Release status: experimental |
|
---|---|
Implementation | API |
Description | Provides arbitrary-precision arithmetic for Scribunto. |
Author(s) | |
Latest version | 0.1.0 |
Compatibility policy | Master maintains backward compatibility. |
MediaWiki | >= 1.33 |
Database changes | No |
License | GNU General Public License 2.0 or later |
Download | GitHub: Note: README, LDoc |
|
|
Translate the BCmath extension | |
BCmath provides arbitrary-precision arithmetic to Lua modules. With the lib from this extension it would be completely valid to do calculations on π with 125 characters, π β 3,141β592β653β589β793β238β462β643β383β279β502β884β197β169β399β375β105β820β974β944β592β307β816β406β286β208β998β628β034β825β342β117β067β982β148β086β513β282β306β647β093β8β¦ (Actually, the real π is even infinitely much longer!)
The extension uses PHP BCMath Arbitrary Precision Mathematics api through use of phpseclib/bcmath_compat to access the bc programming language.
Installation
[edit]Expect depends on modules from the Scribunto extension.
- Download and place the file(s) in a directory called
BCmath
in yourextensions/
folder. - Add the following code at the bottom of your LocalSettings.php file:
wfLoadExtension( 'BCmath' );
Done β Navigate to Special:Version on your wiki to verify that the extension is successfully installed.
Usage
[edit]The workflow is to first define a BCmath instance, and then use that in ordinary equations, in chained operations, or as part of function calls. Existence of an instance in supported operations will trigger use of the special functions and methods.
-- Used for chained operations
local sum1 = mw.bcmath.new( 0.0 ):add( 42.0 ) -- 42.000000000000000
local sum2 = mw.bcmath.new( '0' ):add( '42' ) -- 42
-- Used in an equation
local sum3 = sum1 * sum2 + 3.14 -- 1767.140000000000000
-- Used in function calls
local sum4 = mw.bcmath.add( mw.bcmath.mul( sum1, sum2 ), 3.14 ) -- 1767.140000000000000
local str1 = sum4 'sci' -- 1.767140000000000000e3
local str2 = sum4( 'sci', 4 ) -- 1.767e3
For further help, see the generated LDoc documentation.
Development
[edit]For recreating the Vagrant-based development environment, see BCmath: Manual/Vagrant.
Alternatives
[edit]The best and perhaps only real alternative is the decNumber C-library, which implements IEEE 754r, and the ldecNumber Lua wrapper library for interfacing with decNumber. These two must be used together.
- decNumber (C-library)
- ldecNumber (Lua-library)
The decNumber library should have a security audit before it is used, but it is probably safe. The Lua library is archived, and is hopelessly outdated with a last version (ldecNumber-21) from August 2007. There are no known MediaWiki-integration for decNumber/ldecNumber.