Common Mathematical Functions of PHP and Usage Examples of High Precision Mathematical Functions of BC

  • 2021-08-12 02:08:24
  • OfStack

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

1. Addition of arbitrary precision numbers of bcadd
2. bcsub Subtraction of Arbitrary Precision Numbers
3. bcmul multiplication, bcdiv division
4. bcmod takes the remainder. (More powerful than%)
5. bcpow power function operation
6. bcsqrt Square Root
7. sqrt Square Root Operation
7. pow Exponentiation
8. abs Find Absolute Value
9. pi gets the pi value

3-angle function

sin cos tan asin acos atan (expressed in radians)
deg2rad Angle Conversion to Radians
rad2deg Radian to Angle

Index

log finds the natural logarithm, log10 finds the bottom logarithm of 10 bits
Power of exp based on e

Various number system conversions

base_convert Universal Arbitrary Number System Conversion
bindec conversion from binary to decimal
decbin conversion from decimal to binary
dechex conversion from decimal to 106-decimal
decoct Decimal to Octal
hexdec 106 to 10
octdec octal to decimal

BC High Precision Function Usage Example:


<?php
 /***************************************************************************************
 *php BC High precision function library 
 *php bc math  Including: addition, comparison, division, subtraction, surplus, multiplication, n Power, configure the default decimal point number, and square it 
 * These functions are useful when it comes to calculations about money 
 ***************************************************************************************
 * Comparison of two high-precision numbers 
 * Encountered at work 1 In this case, 0.00  ! = 0
 *int bccomp ( string $left_operand , string $right_operand [, int $scale ] )
 *$left=$right  Return  0
 *$left<$right  Return  -1
 *$left>$right  Return  1
 *$scale  Number of decimal points 
 ***************************************************************************************/
$a = 4.45;
$b = 5.54;
if(bccomp($a, $b, 2) == 0)
{
 echo " Exactly equal ";
}
/***************************************************************************************
 * Addition of two high-precision numbers 
 *string bcadd ( string $left_operand , string $right_operand [, int $scale ] )
 *$scale  Number of decimal points returned 
 ***************************************************************************************/
$a = 1.0321456;
$b = 0.0123456;
$c = bcadd($a, $b, 2);
var_dump($c);
/***************************************************************************************
 * Subtraction of two high-precision numbers 
 *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);
/********************************************************
 * Finding the remainder of two high-precision numbers / Mould taking 
 *string bcmod ( string $left_operand , string $modulus )
 *******************************************************/
$a = 6;
$b = 4;
$c = bcmod($a, $b);
var_dump($c);
/***************************************************************************************
 * Dividing two high-precision numbers 
 *string bcdiv ( string $left_operand , string $right_operand [, int $scale ] )
 *$scale The number of decimal points defaults to  0
 ***************************************************************************************/
 $a = 6;
 $b = 5;
 $c = bcdiv($a, $b, 3);
 var_dump($c);
/***************************************************************************************
 * Multiplication of two high-precision numbers 
 *string bcmul ( string $left_operand , string $right_operand [, int $scale ] )
 *$scale The number of decimal points defaults to  0
 ***************************************************************************************/
$a = 3.1415926;
$b = 2.4569874566;
$c = bcmul($a, $b, 6);
var_dump($c);
/***************************************************************************************
 * The power value of two high-precision numbers 
 *string bcpow ( string $left_operand , string $right_operand [, int $scale ] )
 *$scale The number of decimal points defaults to  0
 ***************************************************************************************/
$a = 3.1415926;
$b = 2;
$c = bcpow($a, $b, 3);
var_dump($c);
/**************************************************
 * Find the square root of high precision number 
 *string bcsqrt ( string $operand [, int $scale ] )
 *$scale The number of decimal points defaults to  0
 ***************************************************/
$b = bcsqrt($a, 6);
var_dump($b);
/******************************
 * Settings bc The number of decimal points of a function 
 *bool bcscale ( int $scale )
 *$scale The number of decimal points defaults to  0
 ******************************/
bcscale(2);
?>

Run results:


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"

PS: Here are some computing tools recommended for your reference in the next step:

On-line 1 yuan function (equation) solution calculation tool;
http://tools.ofstack.com/jisuanqi/equ_jisuanqi

Scientific calculator online _ advanced calculator online calculation:
http://tools.ofstack.com/jisuanqi/jsqkexue

Online Calculator _ Standard Calculator:
http://tools.ofstack.com/jisuanqi/jsq

More readers interested in PHP can check the topic of this site: "PHP Mathematical Operation Skills Summary", "PHP Operation and Operator Usage Summary", "php String (string) Usage Summary", "PHP Array (Array) Operation Skills Encyclopedia", "PHP Common Traversal Algorithms and Skills Summary", "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithm Summary", "php Regular Expression Usage Summary" and "php Common Database Operation Skills Summary"

I hope this paper is helpful to everyone's PHP programming.


Related articles: