search
HomeBackend DevelopmentPHP TutorialSharing examples of common PHP mathematical functions and BC high-precision mathematical functions

This article mainly introduces the usage of common PHP mathematical functions and BC high-precision mathematical functions. It briefly lists common PHP mathematical operation functions and gives the functions and usage skills of BC high-precision functions in the form of examples. Friends in need can refer to the following , hope it can help everyone.

The examples in this article describe the usage of common PHP mathematical functions and BC high-precision mathematical functions. Share it with everyone for your reference, the details are as follows:

1. bcadd Addition of arbitrary precision numbers
2. bcsub Subtraction of arbitrary precision numbers
3. bcmul multiplication, bcp 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 value of pi

Trigonometric function

sin cos tan asin acos atan (expressed in radians)
deg2rad Convert angles to radians
rad2deg Convert radians into angles

Exponent

log is used to find the natural logarithm, log10 is used to find the logarithm to the base 10
exp is the power with e as the base

Various number system conversion

base_convert Universal arbitrary number system conversion
bindec Binary to decimal conversion
decbin Decimal to binary conversion
dechex Decimal to Hexadecimal
decoct Convert decimal to octal
hexdec Convert hexadecimal to decimal
octdec Convert octal to decimal

BC high-precision function usage example:


<?php
 /***************************************************************************************
 *php BC高精确度函数库
 *php bc math 包含了:相加,比较,相除,相减,求余,相乘,n次方,配置默认小数点数目,求平方
 *这些函数在涉及到有关金钱的计算时比较有用
 ***************************************************************************************
 *两个高精度数比较
 *工作中遇到一种情况,0.00 != 0
 *int bccomp ( string $left_operand , string $right_operand [, int $scale ] )
 *$left=$right 返回 0
 *$left<$right 返回 -1
 *$left>$right 返回 1
 *$scale 小数点位数
 ***************************************************************************************/
$a = 4.45;
$b = 5.54;
if(bccomp($a, $b, 2) == 0)
{
 echo "完全相等";
}
/***************************************************************************************
 *两个高精度数相加
 *string bcadd ( string $left_operand , string $right_operand [, int $scale ] )
 *$scale 返回的小数点个数
 ***************************************************************************************/
$a = 1.0321456;
$b = 0.0123456;
$c = bcadd($a, $b, 2);
var_dump($c);
/***************************************************************************************
 *两个高精度数相减
 *sstring bcsub ( string $left_operand , string $right_operand [, int $scale ] )
 *$scale 返回的小数点个数
 ***************************************************************************************/
$a = 1.0321456;
$b = 3.0123456;
$c = bcsub($a, $b, 2);
var_dump($c);
/********************************************************
 *两个高精度数求余/取模
 *string bcmod ( string $left_operand , string $modulus )
 *******************************************************/
$a = 6;
$b = 4;
$c = bcmod($a, $b);
var_dump($c);
/***************************************************************************************
 *两个高精度数相除
 *string bcp ( string $left_operand , string $right_operand [, int $scale ] )
 *$scale小数点位数默认为 0
 ***************************************************************************************/
 $a = 6;
 $b = 5;
 $c = bcp($a, $b, 3);
 var_dump($c);
/***************************************************************************************
 *两个高精度数相乘
 *string bcmul ( string $left_operand , string $right_operand [, int $scale ] )
 *$scale小数点位数默认为 0
 ***************************************************************************************/
$a = 3.1415926;
$b = 2.4569874566;
$c = bcmul($a, $b, 6);
var_dump($c);
/***************************************************************************************
 *两个高精度数的次方值
 *string bcpow ( string $left_operand , string $right_operand [, int $scale ] )
 *$scale小数点位数默认为 0
 ***************************************************************************************/
$a = 3.1415926;
$b = 2;
$c = bcpow($a, $b, 3);
var_dump($c);
/**************************************************
 *求高精度数的平方根
 *string bcsqrt ( string $operand [, int $scale ] )
 *$scale小数点位数默认为 0
 ***************************************************/
$b = bcsqrt($a, 6);
var_dump($b);
/******************************
 *设置bc函数的小数点位数
 *bool bcscale ( int $scale )
 *$scale小数点位数默认为 0
 ******************************/
bcscale(2);
?>

Running 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:

php high-precision math function library function summary

Simple summary of MySQL math functions

php math function Extension

The above is the detailed content of Sharing examples of common PHP mathematical functions and BC high-precision mathematical functions. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How can you prevent session fixation attacks?How can you prevent session fixation attacks?Apr 28, 2025 am 12:25 AM

Effective methods to prevent session fixed attacks include: 1. Regenerate the session ID after the user logs in; 2. Use a secure session ID generation algorithm; 3. Implement the session timeout mechanism; 4. Encrypt session data using HTTPS. These measures can ensure that the application is indestructible when facing session fixed attacks.

How do you implement sessionless authentication?How do you implement sessionless authentication?Apr 28, 2025 am 12:24 AM

Implementing session-free authentication can be achieved by using JSONWebTokens (JWT), a token-based authentication system where all necessary information is stored in the token without server-side session storage. 1) Use JWT to generate and verify tokens, 2) Ensure that HTTPS is used to prevent tokens from being intercepted, 3) Securely store tokens on the client side, 4) Verify tokens on the server side to prevent tampering, 5) Implement token revocation mechanisms, such as using short-term access tokens and long-term refresh tokens.

What are some common security risks associated with PHP sessions?What are some common security risks associated with PHP sessions?Apr 28, 2025 am 12:24 AM

The security risks of PHP sessions mainly include session hijacking, session fixation, session prediction and session poisoning. 1. Session hijacking can be prevented by using HTTPS and protecting cookies. 2. Session fixation can be avoided by regenerating the session ID before the user logs in. 3. Session prediction needs to ensure the randomness and unpredictability of session IDs. 4. Session poisoning can be prevented by verifying and filtering session data.

How do you destroy a PHP session?How do you destroy a PHP session?Apr 28, 2025 am 12:16 AM

To destroy a PHP session, you need to start the session first, then clear the data and destroy the session file. 1. Use session_start() to start the session. 2. Use session_unset() to clear the session data. 3. Finally, use session_destroy() to destroy the session file to ensure data security and resource release.

How can you change the default session save path in PHP?How can you change the default session save path in PHP?Apr 28, 2025 am 12:12 AM

How to change the default session saving path of PHP? It can be achieved through the following steps: use session_save_path('/var/www/sessions');session_start(); in PHP scripts to set the session saving path. Set session.save_path="/var/www/sessions" in the php.ini file to change the session saving path globally. Use Memcached or Redis to store session data, such as ini_set('session.save_handler','memcached'); ini_set(

How do you modify data stored in a PHP session?How do you modify data stored in a PHP session?Apr 27, 2025 am 12:23 AM

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Give an example of storing an array in a PHP session.Give an example of storing an array in a PHP session.Apr 27, 2025 am 12:20 AM

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

How does garbage collection work for PHP sessions?How does garbage collection work for PHP sessions?Apr 27, 2025 am 12:19 AM

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft