[Image of Linux]
Linux's Witness Ministry
The Personal Web Pages of Chris X. Edwards

ex-mathfun.pl

--------------------------
#!/usr/bin/perl
#ex-mathfun.pl
#example of various numerical functions in perl

srand(time); #seed the random number generator

sub show {print "A=", $a, " B=", $b, " C=", $c, "\n";}

$a=1; $b=2; $c=3;
show;

$c++;
print "C++ ";
show;

$b--;
print "B-- ";
show;
print "\n"; 
print "defining pi using arctan\n";
$pi=atan2($a,$b)*$c;
print "pi=atan2($a,$b)*$c";
print "=",$pi, "\n";

$c=$pi;
show;

print "\n"; 
print "sin and cosine functions\n";
$c=45; print "c= ",$c,"  cos(c)=", cos($c), "\n";
$c=45; print "c= ",$c,"  sin(c)=", sin($c), "\n";

print "\n"; 
print "square root (sqrt) function\n";
$c=16; print "c= ",$c,"  sqrt(c)=", sqrt($c), "\n";

print "\n"; 
print "int function\n";
$c=1.49; print "c= ",$c,"  int(c)=", int($c);
$c=1.50; print "c= ",$c,"  int(c)=", int($c), "\n";
$c=-1.49; print "c= ",$c,"  int(c)=", int($c);
$c=-1.50; print "c= ",$c,"  int(c)=", int($c), "\n";

print "\n"; 
print "absolute value (abs) function\n";
$c=-3; print "c= ",$c,"  abs(c)=", abs($c), "\n";
$c=4; print "c= ",$c,"  abs(c)=", abs($c), "\n";

print "\n"; 
print "logarithmic functions\n";
$c=1; print "c= ",$c,"  exp(c)=", exp($c), "\n";
$c=2.71828182846; print "c= ",$c,"  log(c)=", log($c), "\n";

print "\n"; 
print "base conversion functions\n";
$c="CE"; print "c= ",$c,"  hex(c)=", hex($c), "\n";
$c="12"; print "c= ",$c,"  oct(c)=", oct($c), "\n";

print "\n"; 
print "random number (rand) functions\n";
print "a=rand b=rand(10) c=rand(6)+5\n";
for ($d=0; $d<10; $d++){
$a=rand; $b=rand(10); $c=rand(6)+5;
show;
}

print "\n"; 
print "random numbers, integers, defined decimal place functions\n";
print "a=int(rand(10)) b=int(rand()*1000)/1000 c=int((rand(5)+5)*100)/100\n";
for ($d=0; $d<10; $d++){
$a=int(rand(10)); $b=int(rand()*1000)/1000; $c=int((rand(5)+5)*100)/100;
show;
}


--------------------------
Return to Program Examples
This page was created with only free, open-source, publicly licensed software.
This page was designed to be viewed with any browser on any system.
Chris X. Edwards ~ January 2003