PHP Common Mathematical function usage

Source: Internet
Author: User
Tags mathematical functions modulus natural logarithm sin cos
This article mainly introduces the common mathematical function usage of PHP, briefly enumerates the common mathematical functions of PHP and gives the function and use skill of BC high-precision function with the example form, and the need of friends can refer to

This paper describes the common mathematical functions of PHP and the usage of BC high-precision mathematical functions. Share to everyone for your reference, as follows:

1. Bcadd the sum of arbitrary precision numbers
2. Subtraction of bcsub arbitrary precision number
3. Bcmul Multiplication, bcp division
4. Bcmod take the remainder. (more powerful than% function)
5. Bcpow Power function operation
6. bcsqrt Square Root
7. sqrt Square root operation
7. Pow exponentiation
8. ABS to seek absolute value
9. Pi to get PI value

Trigonometric function

Sin Cos Tan asin ACOS ATAN (expressed in radians)
Deg2rad Angle converted to radians
Convert rad2deg radians to angles

Index

Log for natural logarithm, log10 for 10-bit base logarithm
Exp to the power of the base E

Conversion of various number numbering

Base_convert General-purpose arbitrary-numbering conversion
Bindec binary conversion to decimal
Decbin decimal into binary
Dechex decimal into hexadecimal
Decoct decimal into octal
Hexdec 16 Conversion into decimal
Octdec octal into decimal

BC High Accuracy Function Usage Example:

<?php/*************************************************************************************** *php BC High accuracy function Library * php BC Math contains: Add, compare, divide, subtract, balance, multiply, n-squared, configure default decimal number, squared * These functions are useful when it comes to the calculation of money ***************************************** * Two high-precision number comparison * encountered in the work of a situation, 0.00!  = 0 *int Bccomp (string $left _operand, String $right _operand [, int $scale]) * $left = $right returns 0 * $left < $right return-1 * $left > $right returns 1 * $scale the number of decimal places ******************************************************************************* /$a = 4.45; $b = 5.54;if (Bccomp ($a, $b, 2) = = 0) {echo "Exactly equal";} /*************************************************************************************** * Two high-precision number addition *string Bcadd ( String $left _operand, String $right _operand [, int $scale]) * $scale the number of decimal points returned ****************************************** /$a = 1.0321456; $b = 0.0123456; $c = Bcadd ($a, $b, 2); Var_dump ($c);/****** *********************************** Two high-precision number subtraction *sstring bcsub (String $left _operand, String $right _operand [, int $scale]) * $scale number of decimal points returned ***************************************************************************************/$a = 1.0321456; $b = 3.0123456; $c = bcsub ($a, $b, 2); Var_dump ($c);/******************************************************* * * Two high-precision numbers for residual/modulus *string bcmod (String $left _operand, string $modulus) ************************************************* /$a = 6; $b = 4; $c = Bcmod ($a, $b); Var_dump ($c);/*************************************************************** * Two high-precision numbers divide *string bcp (String $left _operand, String $right _operand [, int $scale]) * $scale decimal point The number of digits defaults to 0 ***************************************************************************************/$a = 6; $b = 5; $c = bcp ($a, $b, 3); Var_dump ($c);/*************************************************************************************** * Two high-precision numbers multiplied *string Bcmul (String $left _oPerand, String $right _operand [, int $scale]) * $scale The default number of decimal places is 0 ****************************************************** /$a = 3.1415926; $b = 2.4569874566; $c = Bcmul ($a, $b, 6); Var_dump ($c);/*************** * Two high-precision number of the secondary value *string Bcpow (String $left _ Operand, string $right _operand [, int $scale]) * $scale The default number of decimal places is 0 ***************************************************** /$a = 3.1415926; $b = 2; $c = Bcpow ($a, $b, 3); Var_dump ($c);/************************* * The square root of the *string bcsqrt (string $operand [, int $scale]) * $scale The default number of decimal places is 0 **************** /$b = BCSQRT ($a, 6); Var_dump ($b);/****************************** * Set the number of decimal places for BC functions * BOOL Bcscale (int $scale) * $scale The default number of decimal places is 0 ******************************/bcscale (2);? >

Operation Result:

String (4) "1.04" string (5) " -1.98" string (1) "2" string (5) "1.200" string (8) "7.718853" string (5) "9.869" string (8) " 1.772453 "

Related recommendations:

Development of PHP Extensions a library of our own math functions

JavaScript Arrays-strings-math functions

PHP common mathematical functions and BC high-precision mathematical Function example sharing

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.