PERL - Numbers
Numbers are scalar data. They exist in PERL as real numbers, float, integers, exponents, octal, and hexidecimal numbers.perlnumbers.pl:
$real = 27; $float = 3.14159; $integer = -4; $exponent = 10e12;
PERL - Mathematical Functions
With numbers comes math. Simple arithmetic operations are discussed in the PERL Operators lesson.Some mathematical functions require some additional PERL Modules. Here's a few trigonomic functions that will only function if your build of PERL has the Math::Trig module installed.
perltrig.pl:
#!/usr/bin/perl
use Math::Trig; #USE THIS MODULE
print "content-type: text/html \n\n"; #HTTP HEADER
$real = 27;
$float = 3.14159;
$integer = -4;
$exponent = 10e12;
print tan($real); #TANGENT FUNCTION
print "<br />";
print sin($float); #SINE FUNCTION
print "<br />";
print acos($integer); #COSINE FUNCTION
perltrig.pl:
-3.27370380042812
2.65358979335273e-06
3.14159265358979-2.06343706889556i
2.65358979335273e-06
3.14159265358979-2.06343706889556i
PERL - Numbers with Operators
Numbers aren't much without arithmetic operations. This next example is a sneak peak of the next lesson, PERL Operators.arithmeticoperations.pl:
#!/usr/bin/perl print "content-type: text/html \n\n"; #HTTP HEADER #PICK TWO NUMBERS $x = 14; $y = 10; #MULTIPLICATION OPERATOR $area = ($x * $y); print $area; print "<br />";
arithmeticoperations.pl:
140
PERL - Formatting Numbers
Computers are capable of calculating numbers that you and I probably never knew existed. This is especially true with calculations involving decimals, floating-point numbers, or percentages.You may find that one of the best solutions is to first convert your numbers when possible to integers (get rid of the decimal). You may then go ahead and perform the required operations such as multiplication, division, addition, or whatever and finally reintroduce the decimal using division.
peskydecimals.pl:
#!/usr/bin/perl print "content-type: text/html \n\n"; #HTTP HEADER $hourlyrate = 7.50; #DECIMAL TO BE RID OF $hoursworked = 35; $no_decimal_rate = ($hourlyrate * 100); $netpay = ($no_decimal_rate * $hoursworked); $paycheck = ($netpay / 100); print "Hourly Wage: $hourlyrate<br />"; print "Hours: $hoursworked<br />"; print "No Decimal: $no_decimal_rate<br />"; print "Net Pay: $netpay<br />"; print "Pay Check: $paycheck<br />";
peskydecimals.pl:
Hourly Wage: 7.5
Hours: 35
No Decimal: 750
Net Pay: 26250
Pay Check: 262.5
In this example we followed the steps stated above, first we removed
the decimal from each number involved in the calculation, ($hourlyrate
and $hoursworked). Then we performed the operation ($netpay), and
finally introduced the decimal again by dividing our $netpay by the same
number we used to get rid of the decimal in the first place (100).No Decimal: 750
Net Pay: 26250
Pay Check: 262.5
- Convert to real numbers.
- Perform the operation(s).
- Convert back to a decimal.
0 comments:
Post a Comment