Jump to content

User:Yonicstudios/sandbox

From mediawiki.org
This page contains changes which are not marked for translation.
MediaWiki extensions manual
BigInteger
Release status: experimental
Implementation API
Description Provides integer arbitrary-precision arithmetic for Scribunto.
Author(s)
  • Mario Martínez (Yonicstudiostalk)
Compatibility policy Snapshots releases along with MediaWiki. Master is not backward compatible.
MediaWiki 1.42+
Database changes No
License GNU General Public License 2.0 or later
Download
README, LDoc
Translate the Yonicstudios/sandbox extension

BigInteger provides integer arbitrary-precision arithmetic to Lua modules.

For decimal arbitrary-precision arithmetic, use BCmath, although it is much slower than BigInteger when operating between integers. Both extensions can operate between each other.

Requirements

[edit]

BigInteger has the following dependencies:

  • The Scribunto extension.
  • A PHP installation containing support for GMP. Most installations should already have this.

You can see the version of GMP used by PHP by viewing a phpinfo() web page, or from the command line with the following command:

php -r 'echo "gmp: " . ( extension_loaded( "gmp" ) ? GMP_VERSION : "no" ) . "\n";'

Installation

[edit]

Follow these steps to install:

  • Download and move the extracted BigInteger folder to your extensions/ directory.
    Developers and code contributors should install the extension from Git instead, using:
    cd extensions/
    git clone https://gerrit.wikimedia.org/r/mediawiki/extensions/BigInteger
    
  • Add the following code at the bottom of your LocalSettings.php file:
    wfLoadExtension( 'BigInteger' );
    
  • Yes Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

Usage

[edit]

Create a BigInteger object, then you can use it in chained operations, expressions (with mixed type support) and function calls. You can use strings and numbers to create BigIntegers and to operate between them. The decimal part is truncated in both types. Note that with Lua numbers, IEEE 754-exclusive values (NaNs and infinities) will throw an error, while 0 has the sign dropped.

-- Shortname for using the library
local BigInt = mw.extensions.biginteger
-- Used for chained operations
local sum1 = BigInt.new( 0 ):add( 42 )                       -- 42
local sum2 = BigInt.new( 0.0 ):add( 42.0 )                   -- 42
local sum3 = BigInt.new( '0' ):add( '42' )                   -- 42

-- Used in an expression
local sum4 = sum1 * sum2 + 3.14                                      -- 1767

-- Used in function calls
local sum4 = BigInt.add( mw.bcmath.mul( sum1, sum2 ), 3.14 )         -- 1767

-- Conversion to string and to BCmath
local str = tostring( sum4 )                                         -- "1767"
local bcnum = mw.bcmath.new( str ):add( 3.14 )                       -- 1770.140000000

For further help, see the generated LDoc documentation.

Configuration

[edit]

The following configuration variables are available:

$wgGMPMaxDigits
Sets the maximum number of digits used for operating between big integers. The least significant digits are trimmed when an integer exceeds this amount. Note that the result may have more digits than this limit when converted to a string. By default, it's set to 100.
$wgGMPMaxFactorial
Sets the maximum value to calculate the factorial of an integer. Calculating the factorial of a number larger than this will result in a Lua error. By default, it's set to 1000.

See also

[edit]