IT Community - Software Programming, Web Development and Technical Support

Mathematical functions in PHP

This is a discussion on Mathematical functions in PHP within the PHP Programming forums, part of the Web Development category; pi() returns an approximation of pi. The returned float has a precision based on the precision directive in php.ini, ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > PHP Programming

Register FAQ Members List Calendar Mark Forums Read
  #21 (permalink)  
Old 04-18-2008, 04:27 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Mathematical functions in PHP

pi() returns an approximation of pi. The returned float has a precision based on the precision directive in php.ini, which defaults to 14. Also, you can use the M_PI constant which yields identical results to pi().

PHP Code:
<?php
echo pi(); // 3.1415926535898
echo M_PI// 3.1415926535898
?>
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #22 (permalink)  
Old 04-18-2008, 04:28 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Mathematical functions in PHP

log() - Natural logarithm

If the optional base parameter is specified, log() returns logbase arg, otherwise log() returns the natural logarithm of arg.
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #23 (permalink)  
Old 04-18-2008, 04:28 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Mathematical functions in PHP

As always you can calculate the logarithm in base b of a number n, but using the mathematical identity: logb(n) = log(n)/log(b), where log is the neperian (or natural) logarithm.
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #24 (permalink)  
Old 04-18-2008, 06:43 AM
sureshbabu sureshbabu is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Location: India
Posts: 101
sureshbabu is on a distinguished road
Send a message via AIM to sureshbabu Send a message via MSN to sureshbabu Send a message via Yahoo to sureshbabu Send a message via Skype™ to sureshbabu
Default Re: Mathematical functions in PHP

The log()
The log() function returns the natural logarithm (base E) of a number.

Syntax
log(x,base)
Example
In the following example we will use the log() function on different numbers:

<?php
echo(log(2.7183) . "<br />");
echo(log(2) . "<br />");
echo(log(1) . "<br />");
echo(log(0) . "<br />");
echo(log(-1))
?>
__________________
Thanks
Regards
Sureshbabu Harikrishnan
+91 9884320017
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #25 (permalink)  
Old 04-18-2008, 09:15 PM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Mathematical functions in PHP

For arbitrary precision mathematics PHP offers the Binary Calculator which supports numbers of any size and precision, represented as strings.
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #26 (permalink)  
Old 04-18-2008, 09:16 PM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Mathematical functions in PHP

These functions are only available if PHP was configured with --enable-bcmath. In PHP 3, these functions are only available if PHP was not configured with --disable-bcmath.

The windows version of PHP has built in support for this extension. You do not need to load any additional extension in order to use these functions.
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #27 (permalink)  
Old 04-18-2008, 09:17 PM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Mathematical functions in PHP

bcadd() adds the left_operand to the right_operand and returns the sum in a string. The optional scale parameter is used to set the number of digits after the decimal place in the result.

For example:
PHP Code:
<?php

$a 
'1.234';
$b '5';

echo 
bcadd($a$b);     // 6
echo bcadd($a$b4);  // 6.2340

?>
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #28 (permalink)  
Old 04-19-2008, 01:20 AM
saravanan saravanan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 181
saravanan is on a distinguished road
Default Re: Mathematical functions in PHP

<?php
echo decoct("30") . "<br />";
echo decoct("10") . "<br />";
echo decoct("1587") . "<br />";
echo decoct("70");
?>

It convert decimal number into octal number

output is
36
12
3063
106
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #29 (permalink)  
Old 04-19-2008, 01:23 AM
saravanan saravanan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 181
saravanan is on a distinguished road
Default Re: Mathematical functions in PHP

<?php
echo octdec("36") . "<br />";
echo octdec("12") . "<br />";
echo octdec("3063") . "<br />";
echo octdec("106");
?>
It converts an octal number to a decimal number
output
30
10
1587
70
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #30 (permalink)  
Old 04-19-2008, 01:25 AM
saravanan saravanan is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Posts: 181
saravanan is on a distinguished road
Default Re: Mathematical functions in PHP

<?php
echo dechex("30") . "<br />";
echo dechex("10") . "<br />";
echo dechex("1587") . "<br />";
echo dechex("70");
?>

IT convert decimal number into hexadecimal number
output
1e
a
633
46
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #31 (permalink)  
Old 04-21-2008, 12:49 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Mathematical functions in PHP

bccomp() compares the left_operand to the right_operand and returns the result as an integer. The optional scale parameter is used to set the number of digits after the decimal place which will be used in the comparison. The return value is 0 if the two operands are equal. If the left_operand is larger than the right_operand the return value is +1 and if the left_operand is less than the right_operand the return value is -1.

For e.g
PHP Code:
<?php
echo bccomp('1''2') . "\n";   // -1
echo bccomp('1.00001''1'3); // 0
echo bccomp('1.00001''1'5); // 1
?>
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #32 (permalink)  
Old 04-21-2008, 12:51 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Mathematical functions in PHP

bcdiv() divides the left_operand by the right_operand and returns the result. The optional scale sets the number of digits after the decimal place in the result, which defaults to 0.

For e.g
PHP Code:
<?php
echo bcdiv('105''6.55957'3);  // 16.007
?>
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #33 (permalink)  
Old 04-21-2008, 12:52 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Mathematical functions in PHP

bcmul() Multiply two arbitrary precision number and Multiply the left_operand by the right_operand and returns the result. The optional scale sets the number of digits after the decimal place in the result.

For e.g
PHP Code:
<?php
echo bcmul('1.34747474747''35'3); // 47.162
echo bcmul('2''4'); // 8
?>
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #34 (permalink)  
Old 04-21-2008, 02:37 AM
Falcon Falcon is offline
D-Web Analyst
 
Join Date: Nov 2007
Location: Chennai
Posts: 289
Falcon is on a distinguished road
Default Re: Mathematical functions in PHP

Hi

log10()
The log10() function returns the base-10 logarithm of a number.
for example
PHP Code:
<?php
echo(log10(2.7183) . "<br />");
echo(
log10(2) . "<br />");
echo(
log10(1) . "<br />");
echo(
log10(0) . "<br />");
echo(
log10(-1))
?>
OutPut for this example
Code:
0.434297385125
0.301029995664
0
-1.#INF
-1.#IND
Regards
Falcon
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #35 (permalink)  
Old 04-21-2008, 06:00 AM
sureshbabu sureshbabu is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Location: India
Posts: 101
sureshbabu is on a distinguished road
Send a message via AIM to sureshbabu Send a message via MSN to sureshbabu Send a message via Yahoo to sureshbabu Send a message via Skype™ to sureshbabu
Default Re: Mathematical functions in PHP

hypot()
The hypot() function returns the length of the hypotenuse of a right-angle triangle.

Syntax
hypot(x,y)

Example
<?php
echo hypot(2,3) . "<br />";
echo hypot(3,6) . "<br />";
echo hypot(3,6) . "<br />";
echo hypot(1,3);
?>

The output of the code above will be:

3.60555127546
6.7082039325
6.7082039325
3.16227766017
__________________
Thanks
Regards
Sureshbabu Harikrishnan
+91 9884320017
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #36 (permalink)  
Old 04-21-2008, 06:07 AM
sureshbabu sureshbabu is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Location: India
Posts: 101
sureshbabu is on a distinguished road
Send a message via AIM to sureshbabu Send a message via MSN to sureshbabu Send a message via Yahoo to sureshbabu Send a message via Skype™ to sureshbabu
Default Re: Mathematical functions in PHP

mt_rand()
The mt_rand() function returns a random integer using the Mersenne Twister algorithm.

If this function is called without parameters, it returns a pseudo-random value between 0 and RAND_MAX.

If you want a random number between 10 and 100 (inclusive), use mt_rand (10,100).

Syntax
mt_rand(min,max)

Example
In this example we will return some random numbers:

<?php
echo(mt_rand() . "<br />");
echo(mt_rand() . "<br />");
echo(mt_rand(10,100))
?>

The output of the code above could be:

1150905288
613289478
21
__________________
Thanks
Regards
Sureshbabu Harikrishnan
+91 9884320017
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #37 (permalink)  
Old 04-22-2008, 12:34 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Mathematical functions in PHP

bcmod() Get modulus of an arbitrary precision number

For e.g
PHP Code:
<?php
echo bcmod('4''2'); // 0
echo bcmod('2''4'); // 2
?>
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #38 (permalink)  
Old 04-22-2008, 12:35 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Mathematical functions in PHP

bcpow() Raise an arbitrary precision number to another

Raise x to the power y

For e.g
PHP Code:
<?php

echo bcpow('4.2''3'2); // 74.08

?>
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #39 (permalink)  
Old 04-23-2008, 06:29 AM
sureshbabu sureshbabu is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Location: India
Posts: 101
sureshbabu is on a distinguished road
Send a message via AIM to sureshbabu Send a message via MSN to sureshbabu Send a message via Yahoo to sureshbabu Send a message via Skype™ to sureshbabu
Default Re: Mathematical functions in PHP

expm1()
The expm1() function returns the value of Ex-1, where E is Euler's constant (approximately 2.7183) and x is the number passed to it.

Syntax

expm1(x)

Parameter Description
x Required. A number

Example

In the following example we will use the expm1() function on different numbers:

<?php
echo(expm1(1) . "<br />");
echo(expm1(-1) . "<br />");
echo(expm1(5) . "<br />");
echo(expm1(10))
?>
__________________
Thanks
Regards
Sureshbabu Harikrishnan
+91 9884320017
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #40 (permalink)  
Old 04-23-2008, 10:49 PM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,162
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Mathematical functions in PHP

bcpowmod() Raise an arbitrary precision number to another, reduced by a specified modulus

Use the fast-exponentiation method to raise x to the power y with respect to the modulus modulus. The optional scale can be used to set the number of digits after the decimal place in the result.

Because this method uses the modulus operation, non-natural numbers may give unexpected results. A natural number is any positive non-zero integer.
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes