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; The following two statements are functionally identical. The bcpowmod() version however, executes in less time and can accept larger parameters. ...


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

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

The following two statements are functionally identical. The bcpowmod() version however, executes in less time and can accept larger parameters.

PHP Code:
<?php
$a 
bcpowmod($x$y$mod);

$b bcmod(bcpow($x$y), $mod);

// $a and $b are equal to each other. 

?>
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #42 (permalink)  
Old 04-24-2008, 06:24 AM
sureshbabu sureshbabu is offline
D-Web Programmer
 
Join Date: Jul 2007
Location: India
Posts: 98
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

deg2rad() function
The deg2rad() function converts a degree to its radian number.


Syntax

deg2rad(degree_number)

Example 1

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

The output of the code above will be:

0.523598775598
0.174532925199
27.6983752292
1.2217304764
__________________
Thanks
Regards
Sureshbabu Harikrishnan
+91 9884320017
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #43 (permalink)  
Old 04-25-2008, 01:52 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,159
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Mathematical functions in PHP

Hi,
To Set default scale parameter for all bc math functions we can use bcscale() function. This function sets the default scale parameter for all subsequent bc math functions that do not explicitly specify a scale parameter.

For e.g
PHP Code:
<?php

// default scale : 3
bcscale(3);
echo 
bcdiv('105''6.55957'); // 16.007

// this is the same without bcscale()
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
  #44 (permalink)  
Old 04-25-2008, 01:54 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,159
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Mathematical functions in PHP

To get the square root of an arbitrary precision number we can use bcsqrt() function

For example
PHP Code:
<?php

echo bcsqrt('2'3); // 1.414

?>
__________________
With,
J. Jeyaseelan

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

To subtract one arbitrary precision number from another we can use bcsub()

PHP Code:
<?php

$a 
'1.234';
$b '5';
 
echo 
bcsub($a$b);     // -3
echo bcsub($a$b4);  // -3.7660

?>
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #46 (permalink)  
Old 04-25-2008, 06:45 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

The atan() function returns the arctangent of a number as a numeric value between -PI/2 and PI/2 radians.

<?php
echo(atan(0.50) . "<br />");
echo(atan(-0.50) . "<br />");
echo(atan(5) . "<br />");
echo(atan(10) . "<br />");
echo(atan(-5) . "<br />");
echo(atan(-10))
?>

ans:

0.463647609001
-0.463647609001
1.37340076695
1.4711276743
-1.37340076695
-1.4711276743
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #47 (permalink)  
Old 04-25-2008, 06:46 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

The atan2() function returns the angle theta of an (x,y) point as a numeric value between -PI and PI radians.

<?php
echo(atan2(0.50,0.50) . "<br />");
echo(atan2(-0.50,-0.50) . "<br />");
echo(atan2(5,5) . "<br />");
echo(atan2(10,20) . "<br />");
echo(atan2(-5,-5) . "<br />");
echo(atan2(-10,10))
?>

ans
0.785398163397
-2.35619449019
0.785398163397
0.463647609001
-2.35619449019
-0.785398163397
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #48 (permalink)  
Old 04-29-2008, 03:16 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: Mathematical functions in PHP

Hi,

mt_srand() Definition and Usage

The mt_srand() function seeds the random number generator for mt_rand().

This function should be called only once per script, and it must be called before any calls to mt_rand().
__________________
Regards,
Senraj.A
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #49 (permalink)  
Old 04-29-2008, 03:17 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: Mathematical functions in PHP

Hi,

In this example we will seed the random number generator:

PHP Code:
mt_srand(mktime());
echo(
mt_rand()); 
The output of the code above could be:

1132656473
__________________
Regards,
Senraj.A
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #50 (permalink)  
Old 04-29-2008, 03:19 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: Mathematical functions in PHP

Hi,

mt_srand()

Note: In PHP 4.2.0 and later, there is no need to seed the random generator with mt_srand(). This is done automatically.

Tip: The greater the randomness of the seed, the more random number you'll get. A suitable number to seed this function with, is mktime().
__________________
Regards,
Senraj.A
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #51 (permalink)  
Old 04-29-2008, 10:24 PM
chetan1 chetan1 is offline
D-Web Trainee
 
Join Date: Apr 2008
Posts: 17
chetan1 is on a distinguished road
Default Re: Mathematical functions in PHP

Math functions will handle values in the range of the integer and float types.
These are predefined functions within library.
__________________
web design company
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #52 (permalink)  
Old 04-29-2008, 10:33 PM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,159
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Mathematical functions in PHP

Hi,
How we can use these functions effectively in PHP scripts?
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #53 (permalink)  
Old 04-30-2008, 03:36 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Thumbs down Re: Mathematical functions in PHP

Hi,

The pow() Definition and Usage

function raises the first argument to the power of the second argument, and returns the result.

Example

PHP Code:
<?php
echo pow(4,2) . "<br />";
echo 
pow(6,2) . "<br />";
echo 
pow(-6,2) . "<br />";
echo 
pow(-6,-2) . "<br />";
echo 
pow(-6,5.5);
?>
The output of the code above will be:

16
36
36
0.0277777777778
-1.#IND
__________________
Regards,
Senraj.A
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #54 (permalink)  
Old 04-30-2008, 09:10 PM
chetan1 chetan1 is offline
D-Web Trainee
 
Join Date: Apr 2008
Posts: 17
chetan1 is on a distinguished road
Default Re: Mathematical functions in PHP

Nice one examples buddy.
__________________
web design company
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #55 (permalink)  
Old 05-01-2008, 11:20 PM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,159
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Mathematical functions in PHP

Hi,
These mathematical functions are used to create arithmetical calculation and logic in a web page.

For example while generating the reports of anything, there we can go for these functions.
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #56 (permalink)  
Old 05-02-2008, 05:15 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: Mathematical functions in PHP

Hi,

pi() Definition and Usage

The pi() function returns the value of PI.

Example

PHP Code:
<?php
echo pi();
?>
The output of the code above will be:

3.14159265359
__________________
Regards,
Senraj.A
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #57 (permalink)  
Old 05-02-2008, 07:08 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
$oct = "0031";
$dec = base_convert($oct,8,10);
echo "$oct in octal is equal to $dec in decimal.";
?>

output

0031 in octal is equal to 25 in decimal.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #58 (permalink)  
Old 05-02-2008, 07:11 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

The lcg_value() function returns a pseudo random number in the range of 0 and 1.

<?php
echo lcg_value();
?>

The output of the code above could be:

0.408212029326
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #59 (permalink)  
Old 05-02-2008, 07:12 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

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).

<?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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #60 (permalink)  
Old 05-02-2008, 07:14 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

The rad2deg() function converts a radian number to its degree.

<?php
$rad = M_PI;
$deg = rad2deg($rad);
echo "$rad radians is equal to $deg degrees";
?>

The output of the code above will be:

3.14159265359 radians is equal to 180 degrees
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
COM functions in php Kamalakannan PHP Programming 24 07-12-2008 03:20 AM
PHP FTP Functions Kamalakannan PHP Programming 54 04-14-2008 04:42 AM
erroneous results for certain mathematical calculations aramesh Flash Actionscript Programming 1 04-03-2008 02:00 AM
Sql functions itbarota Database Support 11 02-29-2008 01:08 AM
What are Virtual Functions? How to implement virtual functions in "C"? Sabari C and C++ Programming 4 09-10-2007 10:35 PM


All times are GMT -7. The time now is 02:58 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.