Assertions
This page is under construction. Please come back later.
The
assert
function is not a built-in part of Perl. You will need to install the CPAN module Devel::Assert
to use it.
$ sudo CPAN install Devel::Assert
#!/usr/bin/env perl use utf8; use Devel::Assert 'on'; use Utils; use strict; use warnings; our $TEST_BASE = 0.9; sub exponentiation { my ($base, $powerArg) = @_; ::assert($powerArg >= 0); my $power = $powerArg; my $result = 1; while ($power > 0) { $result *= $base; --$power; } return $result; } MAIN: { for (my $p = 10; $p >= -1; --$p) { print Utils::messageFormat("\{0:f\} ^ \{1:d\} = \{2:f\}", $TEST_BASE, $p, exponentiation($TEST_BASE, $p)), "\n"; } }
Output
$ perl Assertions1.pl
Assertion '$powerArg >= 0' failed at Assertions1.pl line 12.
main::exponentiation(0.9, -1) called at Assertions1.pl line 25
0.900000 ^ 10 = 0.348678
0.900000 ^ 9 = 0.387420
0.900000 ^ 8 = 0.430467
0.900000 ^ 7 = 0.478297
0.900000 ^ 6 = 0.531441
0.900000 ^ 5 = 0.590490
0.900000 ^ 4 = 0.656100
0.900000 ^ 3 = 0.729000
0.900000 ^ 2 = 0.810000
0.900000 ^ 1 = 0.900000
0.900000 ^ 0 = 1.000000
#!/usr/bin/env perl use utf8; use Devel::Assert 'on'; use Utils; use strict; use warnings; our $TEST_BASE = 0.9; sub exponentiation { my ($base, $powerArg) = @_; ::assert($powerArg >= 0); my $power = $powerArg; my $result = 1; while ($power > 0) { $result *= $base; --$power; } return $result; } MAIN: { for (my $p = 10; $p >= -1; --$p) { print Utils::messageFormat("\{0:f\} ^ \{1:d\} = \{2:f\}", $TEST_BASE, $p, exponentiation($TEST_BASE, $p)), "\n"; } }
Output
$ perl Assertions2.pl
Assertion '$powerArg >= 0' failed at Assertions2.pl line 12.
main::exponentiation(0.9, -1) called at Assertions2.pl line 25
0.900000 ^ 10 = 0.348678
0.900000 ^ 9 = 0.387420
0.900000 ^ 8 = 0.430467
0.900000 ^ 7 = 0.478297
0.900000 ^ 6 = 0.531441
0.900000 ^ 5 = 0.590490
0.900000 ^ 4 = 0.656100
0.900000 ^ 3 = 0.729000
0.900000 ^ 2 = 0.810000
0.900000 ^ 1 = 0.900000
0.900000 ^ 0 = 1.000000
#!/usr/bin/env perl use utf8; use Devel::Assert 'on'; use Utils; use strict; use warnings; our $TEST_BASE = 0.9; sub exponentiation { my ($base, $powerArg) = @_; ::assert($powerArg >= 0); my $power = $powerArg; my $result = 1; while ($power > 0) { $result *= $base; --$power; } # This line will make the result incorrect. $result = -$result; # If power is odd and base is negative, then result should be negative. # Otherwise result should be positive. ::assert((Utils::bitwiseAnd32($power, 1)) == 1 && ($base < 0) ? $result < 0 : $result >= 0); return $result; } MAIN: { for (my $p = 10; $p >= -1; --$p) { print Utils::messageFormat("\{0:f\} ^ \{1:d\} = \{2:f\}", $TEST_BASE, $p, exponentiation($TEST_BASE, $p)), "\n"; } }
Output
$ perl Assertions3.pl
Assertion 'Utils::bitwiseAnd32($power, 1) == 1 && $base < 0 ? $result < 0 : $result >= 0' failed at Assertions3.pl line 23.
main::exponentiation(0.9, 10) called at Assertions3.pl line 30
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
- Base Conversion
- Fast Exponentiation
- Fibonacci Numbers with Memoization
- Football Passer Rating (with Assertions)
- Julian Day Number
- Number of Primes (Estimate)
- Ordinal Date
- Ordinal Date (Revisited)
- Shuffle Deck
- Significant Digits
References
- [[Perl Language Reference]]
- [[Comprehensive Perl Archive Network (CPAN)]]
- [[Beginning Perl]], Simon Cozens
- [[Perl Monks Tutorials]]