Mass Deface
).
Internally, your computer represents floating-point numbers in binary.
Digital (as in powers of two) computers cannot store all numbers
exactly. Some real numbers lose precision in the process. This is a
problem with how computers store numbers and affects all computer
languages, not just Perl.
L shows the gory details of number representations and
conversions.
To limit the number of decimal places in your numbers, you can use the
C or C function. See
L for more details.
printf "%.2f", 10/3;
my $number = sprintf "%.2f", 10/3;
=head2 Why is int() broken?
Your C is most probably working just fine. It's the numbers that
aren't quite what you think.
First, see the answer to "Why am I getting long decimals
(eg, 19.9499999999999) instead of the numbers I should be getting
(eg, 19.95)?".
For example, this
print int(0.6/0.2-2), "\n";
will in most computers print 0, not 1, because even such simple
numbers as 0.6 and 0.2 cannot be presented exactly by floating-point
numbers. What you think in the above as 'three' is really more like
2.9999999999999995559.
=head2 Why isn't my octal data interpreted correctly?
(contributed by brian d foy)
You're probably trying to convert a string to a number, which Perl only
converts as a decimal number. When Perl converts a string to a number, it
ignores leading spaces and zeroes, then assumes the rest of the digits
are in base 10:
my $string = '0644';
print $string + 0; # prints 644
print $string + 44; # prints 688, certainly not octal!
This problem usually involves one of the Perl built-ins that has the
same name a Unix command that uses octal numbers as arguments on the
command line. In this example, C on the command line knows that
its first argument is octal because that's what it does:
%prompt> chmod 644 file
If you want to use the same literal digits (644) in Perl, you have to tell
Perl to treat them as octal numbers either by prefixing the digits with
a C<0> or using C:
chmod( 0644, $filename ); # right, has leading zero
chmod( oct(644), $filename ); # also correct
The problem comes in when you take your numbers from something that Perl
thinks is a string, such as a command line argument in C<@ARGV>:
chmod( $ARGV[0], $filename ); # wrong, even if "0644"
chmod( oct($ARGV[0]), $filename ); # correct, treat string as octal
You can always check the value you're using by printing it in octal
notation to ensure it matches what you think it should be. Print it
in octal and decimal format:
printf "0%o %d", $number, $number;
=head2 Does Perl have a round() function? What about ceil() and floor()? Trig functions?
Remember that C merely truncates toward 0. For rounding to a
certain number of digits, C or C is usually the
easiest route.
printf("%.3f", 3.1415926535); # prints 3.142
The L module (part of the standard Perl distribution)
implements C, C, and a number of other mathematical
and trigonometric functions.
use POSIX;
my $ceil = ceil(3.5); # 4
my $floor = floor(3.5); # 3
In 5.000 to 5.003 perls, trigonometry was done in the L
module. With 5.004, the L module (part of the standard Perl
distribution) implements the trigonometric functions. Internally it
uses the L module and some functions can break out from
the real axis into the complex plane, for example the inverse sine of
2.
Rounding in financial applications can have serious implications, and
the rounding method used should be specified precisely. In these
cases, it probably pays not to trust whichever system of rounding is
being used by Perl, but instead to implement the rounding function you
need yourself.
To see why, notice how you'll still have an issue on half-way-point
alternation:
for (my $i = 0; $i < 1.01; $i += 0.05) { printf "%.1f ",$i}
0.0 0.1 0.1 0.2 0.2 0.2 0.3 0.3 0.4 0.4 0.5 0.5 0.6 0.7 0.7
0.8 0.8 0.9 0.9 1.0 1.0
Don't blame Perl. It's the same as in C. IEEE says we have to do
this. Perl numbers whose absolute values are integers under 2**31 (on
32-bit machines) will work pretty much like mathematical integers.
Other numbers are not guaranteed.
=head2 How do I convert between numeric representations/bases/radixes?
As always with Perl there is more than one way to do it. Below are a
few examples of approaches to making common conversions between number
representations. This is intended to be representational rather than
exhaustive.
Some of the examples later in L use the L
module from CPAN. The reason you might choose L over the
perl built-in functions is that it works with numbers of ANY size,
that it is optimized for speed on some operations, and for at least
some programmers the notation might be familiar.
=over 4
=item How do I convert hexadecimal into decimal
Using perl's built in conversion of C<0x> notation:
my $dec = 0xDEADBEEF;
Using the C function:
my $dec = hex("DEADBEEF");
Using C:
my $dec = unpack("N", pack("H8", substr("0" x 8 . "DEADBEEF", -8)));
Using the CPAN module C:
use Bit::Vector;
my $vec = Bit::Vector->new_Hex(32, "DEADBEEF");
my $dec = $vec->to_Dec();
=item How do I convert from decimal to hexadecimal
Using C:
my $hex = sprintf("%X", 3735928559); # upper case A-F
my $hex = sprintf("%x", 3735928559); # lower case a-f
Using C:
my $hex = unpack("H*", pack("N", 3735928559));
Using L:
use Bit::Vector;
my $vec = Bit::Vector->new_Dec(32, -559038737);
my $hex = $vec->to_Hex();
And L supports odd bit counts:
use Bit::Vector;
my $vec = Bit::Vector->new_Dec(33, 3735928559);
$vec->Resize(32); # suppress leading 0 if unwanted
my $hex = $vec->to_Hex();
=item How do I convert from octal to decimal
Using Perl's built in conversion of numbers with leading zeros:
my $dec = 033653337357; # note the leading 0!
Using the C function:
my $dec = oct("33653337357");
Using L:
use Bit::Vector;
my $vec = Bit::Vector->new(32);
$vec->Chunk_List_Store(3, split(//, reverse "33653337357"));
my $dec = $vec->to_Dec();
=item How do I convert from decimal to octal
Using C:
my $oct = sprintf("%o", 3735928559);
Using L:
use Bit::Vector;
my $vec = Bit::Vector->new_Dec(32, -559038737);
my $oct = reverse join('', $vec->Chunk_List_Read(3));
=item How do I convert from binary to decimal
Perl 5.6 lets you write binary numbers directly with
the C<0b> notation:
my $number = 0b10110110;
Using C:
my $input = "10110110";
my $decimal = oct( "0b$input" );
Using C and C:
my $decimal = ord(pack('B8', '10110110'));
Using C and C for larger strings:
my $int = unpack("N", pack("B32",
substr("0" x 32 . "11110101011011011111011101111", -32)));
my $dec = sprintf("%d", $int);
# substr() is used to left-pad a 32-character string with zeros.
Using L:
my $vec = Bit::Vector->new_Bin(32, "11011110101011011011111011101111");
my $dec = $vec->to_Dec();
=item How do I convert from decimal to binary
Using C (perl 5.6+):
my $bin = sprintf("%b", 3735928559);
Using C:
my $bin = unpack("B*", pack("N", 3735928559));
Using L:
use Bit::Vector;
my $vec = Bit::Vector->new_Dec(32, -559038737);
my $bin = $vec->to_Bin();
The remaining transformations (e.g. hex -> oct, bin -> hex, etc.)
are left as an exercise to the inclined reader.
=back
=head2 Why doesn't & work the way I want it to?
The behavior of binary arithmetic operators depends on whether they're
used on numbers or strings. The operators treat a string as a series
of bits and work with that (the string C<"3"> is the bit pattern
C<00110011>). The operators work with the binary form of a number
(the number C<3> is treated as the bit pattern C<00000011>).
So, saying C<11 & 3> performs the "and" operation on numbers (yielding
C<3>). Saying C<"11" & "3"> performs the "and" operation on strings
(yielding C<"1">).
Most problems with C<&> and C<|> arise because the programmer thinks
they have a number but really it's a string or vice versa. To avoid this,
stringify the arguments explicitly (using C<""> or C) or convert them
to numbers explicitly (using C<0+$arg>). The rest arise because
the programmer says:
if ("\020\020" & "\101\101") {
# ...
}
but a string consisting of two null bytes (the result of C<"\020\020"
& "\101\101">) is not a false value in Perl. You need:
if ( ("\020\020" & "\101\101") !~ /[^\000]/) {
# ...
}
=head2 How do I multiply matrices?
Use the L or L modules (available from CPAN)
or the L extension (also available from CPAN).
=head2 How do I perform an operation on a series of integers?
To call a function on each element in an array, and collect the
results, use:
my @results = map { my_func($_) } @array;
For example:
my @triple = map { 3 * $_ } @single;
To call a function on each element of an array, but ignore the
results:
foreach my $iterator (@array) {
some_func($iterator);
}
To call a function on each integer in a (small) range, you B use:
my @results = map { some_func($_) } (5 .. 25);
but you should be aware that in this form, the C<..> operator
creates a list of all integers in the range, which can take a lot of
memory for large ranges. However, the problem does not occur when
using C<..> within a C loop, because in that case the range
operator is optimized to I over the range, without creating
the entire list. So
my @results = ();
for my $i (5 .. 500_005) {
push(@results, some_func($i));
}
or even
push(@results, some_func($_)) for 5 .. 500_005;
will not create an intermediate list of 500,000 integers.
=head2 How can I output Roman numerals?
Get the L module.
=head2 Why aren't my random numbers random?
If you're using a version of Perl before 5.004, you must call C
once at the start of your program to seed the random number generator.
BEGIN { srand() if $] < 5.004 }
5.004 and later automatically call C at the beginning. Don't
call C more than once--you make your numbers less random,
rather than more.
Computers are good at being predictable and bad at being random
(despite appearances caused by bugs in your programs :-). The
F article in the "Far More Than You Ever Wanted To Know"
collection in L, courtesy
of Tom Phoenix, talks more about this. John von Neumann said, "Anyone
who attempts to generate random numbers by deterministic means is, of
course, living in a state of sin."
Perl relies on the underlying system for the implementation of
C and C; on some systems, the generated numbers are
not random enough (especially on Windows : see
L).
Several CPAN modules in the C