'0' // $num2 = -0.000005; // (string) -0.000005 => '-5.05E-6' // echo bcadd($num1, $num2, 6); // => '0.000000' // // setlocale(LC_NUMERIC, 'de_DE'); // uses a decimal comma // $num2 = 1.2; // (string) 1.2 => '1,2' // echo bcsub($num1, $num2, 1); // => '0.0' // [/php] // // LITERATURE_SOURCES // * PHP_NET (2023-09-18) // URL: https://www.php.net/manual/en/intro.bc.php // INTRODUCTION - END // // INSTALLING_CONFIGURING - BEGIN // Installing/Configuring // // REQUIREMENTS // INSTALLATION // RUNTIME_CONFIGURATION // RESOURCE_TYPES // // REQUIREMENTS - BEGIN // Requirements // // No external libraries are needed to build this extension. // // LITERATURE_SOURCES // * PHP_NET (2023-09-18) // URL: https://www.php.net/manual/en/bc.requirements.php // REQUIREMENTS - END // // INSTALLATION - BEGIN // Installation // // These functions are only available if PHP was configured with --enable-bcmath. // The Windows version of PHP has built-in support for this extension. You do not need to load any additional extensions in order to use these functions. // // LITERATURE_SOURCES // * PHP_NET (2023-09-18) // URL: https://www.php.net/manual/en/bc.installation.php // INSTALLATION - END // // RUNTIME_CONFIGURATION - BEGIN // Runtime Configuration // // The behaviour of these functions is affected by settings in php.ini. // // BC math configuration options // Name | Default | Changeable | Changelog // bcmath.scale | "0" | PHP_INI_ALL | // // For further details and definitions of the PHP_INI_* modes, see the Where a configuration setting may be set. // Here's a short explanation of the configuration directives. // // bcmath.scale int - Number of decimal digits for all bcmath functions. See also bcscale(). // // LITERATURE_SOURCES // * PHP_NET (2023-09-18) // URL: https://www.php.net/manual/en/bc.configuration.php // RUNTIME_CONFIGURATION - END // // RESOURCE_TYPES - BEGIN // Resource Types // // This extension has no resource types defined. // // LITERATURE_SOURCES // * PHP_NET (2023-09-18) // URL: https://www.php.net/manual/en/bc.resources.php // RESOURCE_TYPES - END // // LITERATURE_SOURCES // * PHP_NET (2023-09-18) // URL: https://www.php.net/manual/en/bc.setup.php // INSTALLING_CONFIGURING - END // // PREDEFINED_CONSTANTS - BEGIN // Predefined Constants // // This extension has no constants defined. // // LITERATURE_SOURCES // * PHP_NET (2023-09-18) // URL: https://www.php.net/manual/en/bc.constants.php // PREDEFINED_CONSTANTS - END // // BC_MATH_FUNCTIONS - BEGIN // BC Math Functions // // Table of Contents // * 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 numbers // * bcpow - Raise an arbitrary precision number to another // * bcpowmod - Raise an arbitrary precision number to another, reduced by a specified modulus // * bcscale - Set or get 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 // // LITERATURE_SOURCES // * PHP_NET (2023-09-18) // URL: https://www.php.net/manual/en/ref.bc.php // BC_MATH_FUNCTIONS - END // // LITERATURE_SOURCES // * PHP_NET (2023-09-18) // URL: https://www.php.net/manual/en/book.bc.php // BCMATH_ARBITRARY_PRECISION_MATHEMATICS - END // ============================== // ============================== BEGIN // PHP_MATHEMATICAL_BCMATH_BCADD // ============================== PUBLIC // ============================== ABOUT // Add two arbitrary precision numbers. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // bcadd() - PHP_4, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_mathematical_bcmath_bcadd($num1, $num2, $scale = null) { $return_bcadd = null; // ========== BCADD - BEGIN // ===== ABOUT // Add two arbitrary precision numbers // ===== DESCRIPTION // Sums num1 and num2. // ===== SUPPORTED // PHP_4, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // bcadd(string $num1, string $num2, ?int $scale = null): string // ===== CODE $return_bcadd = bcadd( $num1, // string num1 - The left operand, as a string. $num2, // string num2 - The right operand, as a string. $scale // int scale - This optional parameter is used to set the number of digits after the decimal place in the result. If omitted, it will default to the scale set globally with the bcscale() function, or fallback to 0 if this has not been set. ); // Return Values // The sum of the two operands, as a string. // // Changelog // Version - Description // 8.0.0 - scale is now nullable. // // [examples] // Examples // [example] // Example #1 bcadd() example // [php] // // $a = '1.234'; // $b = '5'; // // echo bcadd($a, $b); // 6 // echo bcadd($a, $b, 4); // 6.2340 // // [/php] // [/example] // [/examples] // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-05) // URL: https://www.php.net/manual/en/function.bcadd.php // ========== BCADD - END // SYNTAX: // string bcadd(string $num1, string $num2, int $scale = null) return $return_bcadd; // string } // ============================== END // PHP_MATHEMATICAL_BCMATH_BCADD // ============================== // ============================== BEGIN // PHP_MATHEMATICAL_BCMATH_BCCOMP // ============================== PUBLIC // ============================== ABOUT // Compare two arbitrary precision numbers. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // bccomp() - PHP_4, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_mathematical_bcmath_bccomp($num1, $num2, $scale = null) { $return_bccomp = 0; // ========== BCCOMP - BEGIN // ===== ABOUT // Compare two arbitrary precision numbers // ===== DESCRIPTION // Compares the num1 to the num2 and returns the result as an integer. // ===== SUPPORTED // PHP_4, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // bccomp(string $num1, string $num2, ?int $scale = null): int // ===== CODE $return_bccomp = bccomp( $num1, // string num1 - The left operand, as a string. $num2, // string num2 - The right operand, as a string. $scale // int scale - The optional scale parameter is used to set the number of digits after the decimal place which will be used in the comparison. ); // Return Values // Returns 0 if the two operands are equal, 1 if the num1 is larger than the num2, -1 otherwise. // // Changelog // Version - Description // 8.0.0 - scale is now nullable. // // [examples] // Examples // [example] // Example #1 bccomp() example // [php] // // echo bccomp('1', '2') . "\n"; // -1 // echo bccomp('1.00001', '1', 3); // 0 // echo bccomp('1.00001', '1', 5); // 1 // // [/php] // [/example] // [/examples] // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-05) // URL: https://www.php.net/manual/en/function.bccomp.php // ========== BCCOMP - END // SYNTAX: // int bccomp(string $num1, string $num2, int $scale = null) return $return_bccomp; // int } // ============================== END // PHP_MATHEMATICAL_BCMATH_BCCOMP // ============================== // ============================== BEGIN // PHP_MATHEMATICAL_BCMATH_BCDIV // ============================== PUBLIC // ============================== ABOUT // Divide two arbitrary precision numbers. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // bcdiv() - PHP_4, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_mathematical_bcmath_bcdiv($num1, $num2, $scale = null) { $return_bcdiv = null; // ========== BCDIV - BEGIN // ===== ABOUT // Divide two arbitrary precision numbers // ===== DESCRIPTION // Divides the num1 by the num2. // ===== SUPPORTED // PHP_4, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // bcdiv(string $num1, string $num2, ?int $scale = null): string // ===== CODE $return_bcdiv = bcdiv( $num1, // string num1 - The dividend, as a string. $num2, // string num2 - The divisor, as a string. $scale // int scale - This optional parameter is used to set the number of digits after the decimal place in the result. If omitted, it will default to the scale set globally with the bcscale() function, or fallback to 0 if this has not been set. ); // Return Values // Returns the result of the division as a string, or null if num2 is 0. // // Changelog // Version - Description // 8.0.0 - scale is now nullable. // // [examples] // Examples // [example] // Example #1 bcdiv() example // [php] // // echo bcdiv('105', '6.55957', 3); // 16.007 // // [/php] // [/example] // [/examples] // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-05) // URL: https://www.php.net/manual/en/function.bcdiv.php // ========== BCDIV - END // SYNTAX: // string bcdiv(string $num1, string $num2, int $scale = null) return $return_bcdiv; // string } // ============================== END // PHP_MATHEMATICAL_BCMATH_BCDIV // ============================== // ============================== BEGIN // PHP_MATHEMATICAL_BCMATH_BCMOD // ============================== PUBLIC // ============================== ABOUT // Get modulus of an arbitrary precision number. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // bcmod() - PHP_4, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_mathematical_bcmath_bcmod($num1, $num2, $scale = null) { $return_bcmod = null; // ========== BCMOD - BEGIN // ===== ABOUT // Get modulus of an arbitrary precision number // ===== DESCRIPTION // Get the remainder of dividing num1 by num2. Unless num2 is zero, the result has the same sign as num1. // ===== SUPPORTED // PHP_4, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // bcmod(string $num1, string $num2, ?int $scale = null): string // ===== CODE $return_bcmod = bcmod( $num1, // string num1 - The dividend, as a string. $num2, // string num2 - The divisor, as a string. $scale // int $scale ); // Return Values // Returns the modulus as a string, or null if num2 is 0. // // Changelog // Version - Description // 8.0.0 - scale is now nullable. // 7.2.0 - num1 and num2 are no longer truncated to integer, so now the behavior of bcmod() follows fmod() rather than the % operator. // 7.2.0 - The scale parameter was added. // // [examples] // Examples // [example] // Example #1 bcmod() example // [php] // bcscale(0); // echo bcmod( '5', '3'); // 2 // echo bcmod( '5', '-3'); // 2 // echo bcmod('-5', '3'); // -2 // echo bcmod('-5', '-3'); // -2 // [/php] // [/example] // [example] // Example #2 bcmod() with decimals // [php] // bcscale(1); // echo bcmod('5.7', '1.3'); // 0.5 as of PHP 7.2.0; 0 previously // [/php] // [/example] // [/examples] // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-05) // URL: https://www.php.net/manual/en/function.bcmod.php // ========== BCMOD - END // SYNTAX: // string bcmod(string $num1, string $num2, int $scale = null) return $return_bcmod; // string } // ============================== END // PHP_MATHEMATICAL_BCMATH_BCMOD // ============================== // ============================== BEGIN // PHP_MATHEMATICAL_BCMATH_BCMUL // ============================== PUBLIC // ============================== ABOUT // Multiply two arbitrary precision numbers. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // bcmul() - PHP_4, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_mathematical_bcmath_bcmul($num1, $num2, $scale = null) { $return_bcmul = null; // ========== BCMUL - BEGIN // ===== ABOUT // Multiply two arbitrary precision numbers // ===== DESCRIPTION // Multiply the num1 by the num2. // ===== SUPPORTED // PHP_4, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // bcmul(string $num1, string $num2, ?int $scale = null): string // ===== CODE $return_bcmul = bcmul( $num1, // string num1 - The left operand, as a string. $num2, // string num2 - The right operand, as a string. $scale // int scale - This optional parameter is used to set the number of digits after the decimal place in the result. If omitted, it will default to the scale set globally with the bcscale() function, or fallback to 0 if this has not been set. ); // Return Values // Returns the result as a string. // // Changelog // Version - Description // 8.0.0 - scale is now nullable. // 7.3.0 - bcmul() now returns numbers with the requested scale. Formerly, the returned numbers may have omitted trailing decimal zeroes. // // [examples] // Examples // [example] // Example #1 bcmul() example // [php] // echo bcmul('1.34747474747', '35', 3); // 47.161 // echo bcmul('2', '4'); // 8 // [/php] // [/example] // [/examples] // // Notes // Note: // Before PHP 7.3.0 bcmul() may return a result with fewer digits after the decimal point than the scale parameter would indicate. This only occurs when the result doesn't require all of the precision allowed by the scale. For example: // [example] // Example #2 bcmul() scale example // [php] // echo bcmul('5', '2', 2); // prints "10", not "10.00" // [/php] // [/example] // ===== LITERATURE_SOURCES // * PHP_NET (2023-10-01) // URL: https://www.php.net/manual/en/function.bcmul.php // ========== BCMUL - END // SYNTAX: // string bcmul(string $num1, string $num2, int $scale = null) return $return_bcmul; // string } // ============================== END // PHP_MATHEMATICAL_BCMATH_BCMUL // ============================== // ============================== BEGIN // PHP_MATHEMATICAL_BCMATH_BCPOW // ============================== PUBLIC // ============================== ABOUT // Raise an arbitrary precision number to another. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // bcpow() - PHP_4, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_mathematical_bcmath_bcpow($num, $exponent, $scale = null) { $return_bcpow = null; // ========== BCPOW - BEGIN // ===== ABOUT // Raise an arbitrary precision number to another // ===== DESCRIPTION // Raise num to the power exponent. // ===== SUPPORTED // PHP_4, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // bcpow(string $num, string $exponent, ?int $scale = null): string // ===== CODE $return_bcpow = bcpow( $num, // string num - The base, as a string. $exponent, // string exponent - The exponent, as a string. If the exponent is non-integral, it is truncated. The valid range of the exponent is platform specific, but is at least -2147483648 to 2147483647. $scale // int scale - This optional parameter is used to set the number of digits after the decimal place in the result. If omitted, it will default to the scale set globally with the bcscale() function, or fallback to 0 if this has not been set. ); // Return Values // Returns the result as a string. // // Changelog // Version - Description // 7.3.0 - bcpow() now returns numbers with the requested scale. Formerly, the returned numbers may have omitted trailing decimal zeroes. // // [examples] // Examples // [example] // Example #1 bcpow() example // [php] // // echo bcpow('4.2', '3', 2); // 74.08 // // [/php] // [/example] // [/examples] // // Notes // Note: // Before PHP 7.3.0 bcpow() may return a result with fewer digits after the decimal point than the scale parameter would indicate. This only occurs when the result doesn't require all of the precision allowed by the scale. For example: // [example] // Example #2 bcpow() scale example // [php] // echo bcpow('5', '2', 2); // prints "25", not "25.00" // [/php] // [/example] // ===== LITERATURE_SOURCES // * PHP_NET (2023-10-01) // URL: https://www.php.net/manual/en/function.bcpow.php // ========== BCPOW - END // SYNTAX: // string bcpow(string $num, string $exponent, int $scale = null) return $return_bcpow; // string } // ============================== END // PHP_MATHEMATICAL_BCMATH_BCPOW // ============================== // ============================== BEGIN // PHP_MATHEMATICAL_BCMATH_BCPOWMOD // ============================== OFFLINE // ============================== ABOUT // Raise an arbitrary precision number to another, reduced by a specified modulus. // ============================== SUPPORT // PHP_5 - PHP_8 // ============================== USING FUNCTIONS (1) // bcpowmod() - PHP_5, PHP_7, PHP_8 // ============================== CODE /* function php_mathematical_bcmath_bcpowmod($num, $exponent, $modulus, $scale = null) { $return_bcpowmod = null; // ========== BCPOWMOD - BEGIN // ===== ABOUT // Raise an arbitrary precision number to another, reduced by a specified modulus // ===== DESCRIPTION // Use the fast-exponentiation method to raise num to the power exponent with respect to the modulus modulus. // ===== SUPPORTED // PHP_5, PHP_7, PHP_8 // ===== SYNTAX // bcpowmod( // string $num, // string $exponent, // string $modulus, // ?int $scale = null // ): string // ===== CODE $return_bcpowmod = bcpowmod( $num, // string num - The base, as an integral string (i.e. the scale has to be zero). $exponent, // string exponent - The exponent, as an non-negative, integral string (i.e. the scale has to be zero). $modulus, // string modulus - The modulus, as an integral string (i.e. the scale has to be zero). $scale // int scale - This optional parameter is used to set the number of digits after the decimal place in the result. If omitted, it will default to the scale set globally with the bcscale() function, or fallback to 0 if this has not been set. ); // Return Values // Returns the result as a string, or false if modulus is 0 or exponent is negative. // // Changelog // Version - Description // 8.0.0 - scale is now nullable. // // [examples] // Examples // [example] // The following two statements are functionally identical. The bcpowmod() version however, executes in less time and can accept larger parameters. // [php] // $a = bcpowmod($x, $y, $mod); // // $b = bcmod(bcpow($x, $y), $mod); // // // $a and $b are equal to each other. // // [/php] // [/example] // [/examples] // // Notes // Note: Because this method uses the modulus operation, numbers which are not positive integers may give unexpected results. // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-05) // URL: https://www.php.net/manual/en/function.bcpowmod.php // ========== BCPOWMOD - END // SYNTAX: // string bcpowmod(string $num, string $exponent, string $modulus, int $scale = null) return $return_bcpowmod; // string } */ // ============================== END // PHP_MATHEMATICAL_BCMATH_BCPOWMOD // ============================== // ============================== BEGIN // PHP_MATHEMATICAL_BCMATH_BCSCALE // ============================== PUBLIC // ============================== ABOUT // Set or get default scale parameter for all bc math functions. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // bcscale() - PHP_4, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_mathematical_bcmath_bcscale($scale) { $return_bcscale = 0; // ========== BCSCALE - BEGIN // ===== ABOUT // Set or get default scale parameter for all bc math functions // ===== DESCRIPTION // Gets the current scale factor. // ===== SUPPORTED // PHP_4, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // bcscale(int $scale): int // Sets the default scale parameter for all subsequent calls to bc math functions that do not explicitly specify a scale parameter. // // bcscale(null $scale = null): int // ===== CODE $return_bcscale = bcscale( $scale // int scale - The scale factor. ); // Return Values // Returns the old scale when used as setter. Otherwise the current scale is returned. // // Changelog // Version - Description // 8.0.0 - scale is now nullable. // 7.3.0 - bcscale() can now be used to get the current scale factor; when used as setter, it now returns the old scale value. Formerly, scale was mandatory, and bcscale() always returned true. // // [examples] // Examples // [example] // Example #1 bcscale() example // [php] // // // default scale : 3 // bcscale(3); // echo bcdiv('105', '6.55957'); // 16.007 // // // this is the same without bcscale() // echo bcdiv('105', '6.55957', 3); // 16.007 // // [/php] // [/example] // [/examples] // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-05) // URL: https://www.php.net/manual/en/function.bcscale.php // ========== BCSCALE - END // SYNTAX: // int bcscale(int $scale) return $return_bcscale; // int } // ============================== END // PHP_MATHEMATICAL_BCMATH_BCSCALE // ============================== // ============================== BEGIN // PHP_MATHEMATICAL_BCMATH_BCSQRT // ============================== PUBLIC // ============================== ABOUT // Get the square root of an arbitrary precision number. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // bcsqrt() - PHP_4, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_mathematical_bcmath_bcsqrt($num, $scale = null) { $return_bcsqrt = null; // ========== BCSQRT - BEGIN // ===== ABOUT // Get the square root of an arbitrary precision number // ===== DESCRIPTION // Return the square root of the num. // ===== SUPPORTED // PHP_4, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // bcsqrt(string $num, ?int $scale = null): string // ===== CODE $return_bcsqrt = bcsqrt( $num, // string num - The operand, as a well-formed BCMath numeric string. $scale // int scale - This optional parameter is used to set the number of digits after the decimal place in the result. If omitted, it will default to the scale set globally with the bcscale() function, or fallback to 0 if this has not been set. ); // Return Values // Returns the square root as a well-formed BCMath numeric string. // // Errors/Exceptions // This function throws a ValueError in the following cases: // * num is not a well-formed BCMath numeric string // * num is less than 0 // * scale is outside the valid range // // Changelog // Version - Description // 8.0.0 - If num is not a well-formed BCMath numeric string, or less than 0, a ValueError is thrown. Previously, E_WARNING was raised instead. // 8.0.0 - scale now needs to be between 0 and 2147483647; previously, negative scales have been silently treated as 0. // 8.0.0 - scale is now nullable. // // [examples] // Examples // [example] // Example #1 bcsqrt() example // [php] // // echo bcsqrt('2', 3); // 1.414 // // [/php] // [/example] // [/examples] // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-05) // URL: https://www.php.net/manual/en/function.bcsqrt.php // ========== BCSQRT - END // SYNTAX: // string bcsqrt(string $num, int $scale = null) return $return_bcsqrt; // string } // ============================== END // PHP_MATHEMATICAL_BCMATH_BCSQRT // ============================== // ============================== BEGIN // PHP_MATHEMATICAL_BCMATH_BCSUB // ============================== PUBLIC // ============================== ABOUT // Subtract one arbitrary precision number from another. // ============================== SUPPORT // PHP_4 - PHP_8 // ============================== USING FUNCTIONS (1) // bcsub() - PHP_4, PHP_5, PHP_7, PHP_8 // ============================== CODE function php_mathematical_bcmath_bcsub($num1, $num2, $scale = null) { $return_bcsub = null; // ========== BCSUB - BEGIN // ===== ABOUT // Subtract one arbitrary precision number from another // ===== DESCRIPTION // Subtracts the num2 from the num1. // ===== SUPPORTED // PHP_4, PHP_5, PHP_7, PHP_8 // ===== SYNTAX // bcsub(string $num1, string $num2, ?int $scale = null): string // ===== CODE $return_bcsub = bcsub( $num1, // string num1 - The left operand, as a string. $num2, // string num2 - The right operand, as a string. $scale // int scale - This optional parameter is used to set the number of digits after the decimal place in the result. If omitted, it will default to the scale set globally with the bcscale() function, or fallback to 0 if this has not been set. ); // Return Values // The result of the subtraction, as a string. // // Changelog // Version - Description // 8.0.0 - scale is now nullable. // // [examples] // Examples // [example] // Example #1 bcsub() example // [php] // // $a = '1.234'; // $b = '5'; // // echo bcsub($a, $b); // -3 // echo bcsub($a, $b, 4); // -3.7660 // // [/php] // [/example] // [/examples] // ===== LITERATURE_SOURCES // * PHP_NET (2024-01-05) // URL: https://www.php.net/manual/en/function.bcsub.php // ========== BCSUB - END // SYNTAX: // string bcsub(string $num1, string $num2, int $scale = null) return $return_bcsub; // string } // ============================== END // PHP_MATHEMATICAL_BCMATH_BCSUB // ============================== // ============================== END // PHP_MATHEMATICAL_BCMATH // ============================== ?>