PHP MATHEMATICS [MATH] FUNCTIONS - 1

in #utopian-io7 years ago (edited)

What Will I Learn?

  • PHP Math Functions

Requirements

  • PHP
  • Linux

Difficulty

  • Basic

Tutorial Contents

You can also perform advanced mathematical operations outside of the four operations using the PHP language. You can use mathematical functions added to the PHP kernel to make operations easier. For example, if you need to write a function to find a binary number base in any statement, you can use the simple (Math) operations can be easily accomplished using functions. Now let's examine the mathematical functions in order and perform examples to find out how they are used.

Advanced Calculations

The PHP language has functions that are ready to perform many mathematical calculations other than 4 operations. The functions for which many of the required mathematical operations are performed are given in the following tablature explanations.

FunctionExplanation
abs()Returnsthe absolute value of the count.
sqrt()Calculate the square root of the count.
pow()The count calculates the rank at the entered rank.
fmod()Gives the remainder (mod) of the count divided by the number entered.
log()The natural logarithm performs the account.
log10()Perform logarithm calculations at base 10.
exp()Calculates the exponential of the entered number.
hypot()The two sides calculate the hypotenuse of the specified right triangle.
<?php
echo 'Absolute value : ' . abs (-20) . ' <br> ' ;
echo ' Square root of 16 : ' . sqrt (16) . ' <br> ' ;
echo ' 3^4 (3 base 4 ) : ' .pow (3 , 4  ) . ' <br > ' ;
echo ' Part 5 of 12 : ' . fmod ( 12 , 5 ) . ' <br > ' ;
echo ' log ( 6 ) : ' . log ( 6 ) . '  < br > ' ;
echo ' 10 base log ( 6 ) : '  . log10 (6) . ' <br>' ;
echo 'e base -2 : ' . exp (-2 ). '<br>' ;
echo 'hypotenuse of triangles of length 3 and 4 : ' .hypot (3,4).'<br>'; 
?>

Examples of how to use the functions in the table are given above. Control which results the functions return in which cases by changing the values.

Calling and Rounding

When working with floating point (float, double) data types, you may sometimes want to get the desired number of digits and the remainder to be discarded. When rounding is performed, you can decide whether the count is to be truncated or rounded up or down. The list of functions that can be used for rounding operations is given in the following table.

ceil()If there is a next part, it is discarded and the number is rounded up.
floor()If there is a next part from the benefit, it is discarded and the number is rounded down.
round()If the first number is 5 and above, it is rounded up, otherwise it is rounded down.
<?php
$pi=pi();
echo $pi . '<br>' ;                                 #Result: 3.14159265359
echo ceil ($pi) . '<br>';                      #Result: 4
echo floor ($pi) . '<br>' ;                  #Result: 4
echo round ($pi, 3);                            #Result: 3.142
?>

In the above example, truncation and rounding functions have affected the number of pi.

In the example, down is performed with ceil() and floor(). In a somewhat different way, we want round() to take 3 digits after the call, and the next digits to be discarded.

NOTE: In PHP 5.3.0 and higher, parameter 3 can also be entered for the round () function. With this newly added feature, whatever the value of the next number is, the rounding is done as specified in the software.
With this development, the round() function

PHP_ROUND_HALF_UP
PHP_ROUND_HALF_DOWN
PHP_ROUND_HALF_EVEN
PHP_ROUND_HALF_ODD

values.

Conversiors

In this section, we will examine the conversions between numbers. We will learn how to convert a radian expression to a base of binary numbers in degrees or decimal numbers. The conversion functions that come with the php language are given in the table below.

FunctionExplanation
base_convert()Any board converts the expressed number to another number base.
bindec()Converts a binary number base to a decimal number base.
decbin()Converts a decimal number base to a binary number base.
hexdec()Converts hexadecimal number base to decimal number base.
dechex()Converts a decimal number base to a hexadecimal number base.
octdec()Octal converts a number base to a decimal base.
decoct()Converts the decimal number base to octal number base.
deg2rad()Transforms the expression entered in degrees into the radian type.
rad2deg()Transforms the expression entered as radians to degrees.
<?php
$number = 12 ;
echo base _convert ($number , 10 , 8 ) ; # Result: 14
?> 

In the above example, 12 values have been loaded into the $ number variable. Using the base converter function, $number is converted from decimal number base to octal number base.

<?php
echo bindec (1111000) . '<br>' ;                            #Result: 120
echo decbin (120) . '<br> <br>' ;                           #Result: 1111000
echo hexdec ('1 A') . '<br>' ;                                      #Result: 26
echo dechex (44) . ' <br > <br>' ;                            #Result: 2c
echo octdec (14 ). ' < br>' ;                                         #Result: 12
echo decoct (12) . ' <br> <br>' ;                               #Result : 14
echo deg 2 rad (90) . '<br>' ;                                       #Result : 1.57079632679
echo rad 2 deg (1.57079632679) . '<br>' ;          #Result : ~90
?>

Above are examples of other conversions.
Thank you for reading. The first part of our Mathematical Functions series is over. In the second part, we will process Trigonometry and Random Numbers.

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Mathematics seems like a mystery to me till date maybe because I'm poor in it

@resteemator is a new bot casting votes for its followers. Follow @resteemator and vote this comment to increase your chance to be voted in the future!

Hey @bedis I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • This is your first accepted contribution here in Utopian. Welcome!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.15
TRX 0.15
JST 0.028
BTC 53554.32
ETH 2224.75
USDT 1.00
SBD 2.29