PHP High-precision Math function library Function Summary

Source: Internet
Author: User
Tags php language modulus php programming square root types of functions
The PHP language provides a binary calculator (binary Calculator) for any mathematical calculation of arbitrary precision, and it supports numbers of any size and precision, as described in string form.

In PHP, a high-accuracy library of functions is available:BC High-accuracy function library . It contains the following calculations: Add, compare, divide, subtract, balance, multiply, n-order, configure the default number of decimal points, squared, and so on. These functions are useful when it comes to calculations about money.

Here's a look at these high-precision functions:

1. Bcadd adds two high-precision numbers

The syntax format is as follows:

String Bcadd (String Left_operand, string right_operand [, int scale])

return value: String

Types of functions: mathematical operations

Description: This function adds two high-precision numbers, passing in two strings, and returning the result as a string. Scale is an optional option that represents the number of digits required after the decimal point of the return value.

Here's a code example to show you:

<?php    $a = 1.0321456;      $b = 0.0123456;      $c = Bcadd ($a, $b, 2);      Var_dump ($c);  ? >

2. Bccomp comparison of two high-precision numbers

The syntax format is as follows:

int Bccomp (string left_operand, string right_operand [, int scale])

return value: Integer

Types of functions: mathematical operations

Description: This function compares two high-precision numbers. Enter two strings, and return 0 if the two string is the same size, and return +1 if the left operand is larger than the right operand, or 1 if the left number string is smaller than the right. Scale is an optional option that represents the number of digits required after the decimal point of the return value. In simple terms,$left = $right returns 0,$left < $right returns-1,$left > $right returns 1.

In fact the example code is as follows:

<?php    $first = ' 2.2 ';    $second = ' 13.00 ';    $third = ' 10.80 ';    $sub = $second-$third;    Echo ' $first, $sub two numbers compared to:<br/> ';    Echo Bccomp ($first, $sub, 2); Echo ' <br/> ';    Echo ' $first, $second two numbers compared to:<br/> ';    Echo Bccomp ($first, $second, 2); Echo ' <br/> ';    Echo ' $second, $third two numbers compared to:<br/> ';    Echo Bccomp ($second, $third, 2); Echo ' <br/> ';? >

3. Bcsub two high-precision numbers

The syntax format is as follows:

String Bcsub (String Left_operand, string right_operand [, int scale])

return value: String

Types of functions: mathematical operations

Description: This function subtracts two high-precision numbers, passing in two strings, subtracting the right operand value from the left operand. Scale is an optional option that represents the number of digits required after the decimal point of the return value.

In fact the example code is as follows:

<?php    $a = 1.0321456;      $b = 3.0123456;      $c = Bcsub ($a, $b, 2);      Var_dump ($c);  ? >

4. Bcmul multiplies two high-precision numbers

The syntax format is as follows:

String Bcmul (String Left_operand, string right_operand [, int scale])

return value: String

Types of functions: mathematical operations

Description: This function multiplies two high-precision numbers, passing in two strings, multiplying the left operand by the number string to the right (the operand). The result is also returned as a string. Scale is an optional option that represents the number of digits required after the decimal point of the return value. The scale decimal point defaults to 0.

In fact the example code is as follows:

<?php    $a = 3.1415926;      $b = 2.4569874566;      $c = Bcmul ($a, $b, 6);      Var_dump ($c);?>

5. Bcdiv divides two high-precision numbers

The syntax format is as follows:

String Bcdiv (String Left_operand, string right_operand [, int scale])

return value: String

Types of functions: mathematical operations

Description: This function divides two high-precision numbers, passing in two strings, dividing the left operand by the number string to the right (the operand). The result is also returned as a string. Scale is an optional option that represents the number of digits required after the decimal point of the return value. The scale decimal point defaults to 0.

In fact the example code is as follows:

<?php  $a = 6;   $b = 5;   $c = Bcdiv ($a, $b, 3);   Var_dump ($c);?>

6. Bcmod Obtaining the remainder/modulus of high precision numbers

The syntax format is as follows:

String Bcmod (String Left_operand, String modulus)

return value: String

Types of functions: mathematical operations

Content Description: Gets the remainder string after the left operand is modulus.

In fact the example code is as follows:

<?php    $a = 6;      $b = 4;      $c = Bcmod ($a, $b);        Var_dump ($c);  ? >

7. Bcpow to find a high-precision digital sub-square value

The syntax format is as follows:

String Bcpow (string x, string y [, int scale])

return value: String

Types of functions: mathematical operations

Description: This function is to find the Y-square of a high-precision digital x. The result is also returned as a string. Scale is an optional option that represents the number of digits required after the decimal point of the return value. The scale decimal point defaults to 0.

In fact the example code is as follows:

<?php    $a = 3.1415926;      $b = 2;      $c = Bcpow ($a, $b, 3);      Var_dump ($c);  ? >

8. Bcsqrt to find the square root of a high-precision number

The syntax format is as follows:

String bcsqrt (string operand [, int scale])

return value: String

Types of functions: mathematical operations

Description: This function asks for a high precision number to open square root value. Scale is an optional option that represents the number of digits required after the decimal point of the return value. The scale decimal point defaults to 0.

In fact the example code is as follows:

<?php    $b = bcsqrt ($a, 6);      Var_dump ($b);?>

9. Number of decimal places for all BC libraries in the Bcscale configuration program

The syntax format is as follows:

BOOL Bcscale (int scale)

Return Value: Boolean

Types of functions: mathematical operations

Content Description: Configures the number of digits required after the decimal point for all returned values. If the program is not specifically specified, the number of decimal places is configured accordingly.

In fact the example code is as follows:

<?php    Bcscale (3);    Echo Bcdiv (' 105 ', ' 6.55957 '); 16.007?>

"Related tutorials Recommended"

1. "Php.cn lonely Nine Cheap (4)-php video Tutorial"

2. PHP real-Combat video tutorial

3. PHP programming from getting started to mastering the full set of tutorials

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.