Website hosting service by Active-Venture.com
  

 Back to Index

Data: Numbers

Why am I getting long decimals (eg, 19.9499999999999) instead of the numbers I should be getting (eg, 19.95)?

The infinite set that a mathematician thinks of as the real numbers can only be approximated on a computer, since the computer only has a finite number of bits to store an infinite number of, um, numbers.

Internally, your computer represents floating-point numbers in binary. Floating-point numbers read in from a file or appearing as literals in your program are converted from their decimal floating-point representation (eg, 19.95) to an internal binary representation.

However, 19.95 can't be precisely represented as a binary floating-point number, just like 1/3 can't be exactly represented as a decimal floating-point number. The computer's binary representation of 19.95, therefore, isn't exactly 19.95.

When a floating-point number gets printed, the binary floating-point representation is converted back to decimal. These decimal numbers are displayed in either the format you specify with printf(), or the current output format for numbers. (See perlvar/"$#" if you use print. $# has a different default value in Perl5 than it did in Perl4. Changing $# yourself is deprecated.)

This affects all computer languages that represent decimal floating-point numbers in binary, not just Perl. Perl provides arbitrary-precision decimal numbers with the Math::BigFloat module (part of the standard Perl distribution), but mathematical operations are consequently slower.

If precision is important, such as when dealing with money, it's good to work with integers and then divide at the last possible moment. For example, work in pennies (1995) instead of dollars and cents (19.95) and divide by 100 at the end.

To get rid of the superfluous digits, just use a format (eg, printf("%.2f", 19.95)) to get the required precision. See perlop/"Floating-point Arithmetic".

Why isn't my octal data interpreted correctly?

Perl only understands octal and hex numbers as such when they occur as literals in your program. Octal literals in perl must start with a leading "0" and hexadecimal literals must start with a leading "0x". If they are read in from somewhere and assigned, no automatic conversion takes place. You must explicitly use oct() or hex() if you want the values converted to decimal. oct() interprets both hex ("0x350") numbers and octal ones ("0350" or even without the leading "0", like "377"), while hex() only converts hexadecimal ones, with or without a leading "0x", like "0x255", "3A", "ff", or "deadbeef". The inverse mapping from decimal to octal can be done with either the "%o" or "%O" sprintf() formats. To get from decimal to hex try either the "%x" or the "%X" formats to sprintf().

This problem shows up most often when people try using chmod(), mkdir(), umask(), or sysopen(), which by widespread tradition typically take permissions in octal.

 
    chmod(644,  $file);	# WRONG
    chmod(0644, $file);	# right  

Note the mistake in the first line was specifying the decimal literal 644, rather than the intended octal literal 0644. The problem can be seen with:

 
    printf("%#o",644); # prints 01204  

Surely you had not intended chmod(01204, $file); - did you? If you want to use numeric literals as arguments to chmod() et al. then please try to express them as octal constants, that is with a leading zero and with the following digits restricted to the set 0..7.

Does Perl have a round() function? What about ceil() and floor()? Trig functions?

Remember that int() merely truncates toward 0. For rounding to a certain number of digits, sprintf() or printf() is usually the easiest route.

 
    printf("%.3f", 3.1415926535);	# prints 3.142  

The POSIX module (part of the standard Perl distribution) implements ceil(), floor(), and a number of other mathematical and trigonometric functions.

 
    use POSIX;
    $ceil   = ceil(3.5);			# 4
    $floor  = floor(3.5);			# 3  

In 5.000 to 5.003 perls, trigonometry was done in the Math::Complex module. With 5.004, the Math::Trig module (part of the standard Perl distribution) implements the trigonometric functions. Internally it uses the Math::Complex 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 rounding is being used by Perl, but to instead implement the rounding function you need yourself.

To see why, notice how you'll still have an issue on half-way-point alternation:

 
    for ($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.

How do I convert between numeric representations?

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 below use the Bit::Vector module from CPAN. The reason you might choose Bit::Vector 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.

How do I convert hexadecimal into decimal

Using perl's built in conversion of 0x notation:

 
    $int = 0xDEADBEEF;
    $dec = sprintf("%d", $int);  

Using the hex function:

 
    $int = hex("DEADBEEF");
    $dec = sprintf("%d", $int);  

Using pack:

 
    $int = unpack("N", pack("H8", substr("0" x 8 . "DEADBEEF", -8)));
    $dec = sprintf("%d", $int);  

Using the CPAN module Bit::Vector:

 
    use Bit::Vector;
    $vec = Bit::Vector->new_Hex(32, "DEADBEEF");
    $dec = $vec->to_Dec();  

How do I convert from decimal to hexadecimal

Using sprint:

 
    $hex = sprintf("%X", 3735928559);  

Using unpack

 
    $hex = unpack("H*", pack("N", 3735928559));  

Using Bit::Vector

 
    use Bit::Vector;
    $vec = Bit::Vector->new_Dec(32, -559038737);
    $hex = $vec->to_Hex();  

And Bit::Vector supports odd bit counts:

 
    use Bit::Vector;
    $vec = Bit::Vector->new_Dec(33, 3735928559);
    $vec->Resize(32); # suppress leading 0 if unwanted
    $hex = $vec->to_Hex();  

How do I convert from octal to decimal

Using Perl's built in conversion of numbers with leading zeros:

 
    $int = 033653337357; # note the leading 0!
    $dec = sprintf("%d", $int);  

Using the oct function:

 
    $int = oct("33653337357");
    $dec = sprintf("%d", $int);  

Using Bit::Vector:

 
    use Bit::Vector;
    $vec = Bit::Vector->new(32);
    $vec->Chunk_List_Store(3, split(//, reverse "33653337357"));
    $dec = $vec->to_Dec();  

How do I convert from decimal to octal

Using sprintf:

 
    $oct = sprintf("%o", 3735928559);  

Using Bit::Vector

 
    use Bit::Vector;
    $vec = Bit::Vector->new_Dec(32, -559038737);
    $oct = reverse join('', $vec->Chunk_List_Read(3));  

How do I convert from binary to decimal

Perl 5.6 lets you write binary numbers directly with the 0b notation:

 
	$number = 0b10110110;  

Using pack and ord

 
    $decimal = ord(pack('B8', '10110110'));  

Using pack and unpack for larger strings

 
    $int = unpack("N", pack("B32",
	substr("0" x 32 . "11110101011011011111011101111", -32)));
    $dec = sprintf("%d", $int);

    # substr() is used to left pad a 32 character string with zeros.  

Using Bit::Vector:

 
    $vec = Bit::Vector->new_Bin(32, "11011110101011011011111011101111");
    $dec = $vec->to_Dec();  

How do I convert from decimal to binary

Using unpack;

 
    $bin = unpack("B*", pack("N", 3735928559));  

Using Bit::Vector:

 
    use Bit::Vector;
    $vec = Bit::Vector->new_Dec(32, -559038737);
    $bin = $vec->to_Bin();  

The remaining transformations (e.g. hex -> oct, bin -> hex, etc.) are left as an exercise to the inclined reader.

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 "3" is the bit pattern 00110011). The operators work with the binary form of a number (the number 3 is treated as the bit pattern 00000011).

So, saying 11 & 3 performs the "and" operation on numbers (yielding 1). Saying "11" & "3" performs the "and" operation on strings (yielding "1").

Most problems with & and | arise because the programmer thinks they have a number but really it's a string. The rest arise because the programmer says:

 
    if ("\020\020" & "\101\101") {
	# ...
    }  

but a string consisting of two null bytes (the result of "\020\020" & "\101\101") is not a false value in Perl. You need:

 
    if ( ("\020\020" & "\101\101") !~ /[^\000]/) {
	# ...
    }  

How do I multiply matrices?

Use the Math::Matrix or Math::MatrixReal modules (available from CPAN) or the PDL extension (also available from CPAN).

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:

 
    @results = map { my_func($_) } @array;  

For example:

 
    @triple = map { 3 * $_ } @single;  

To call a function on each element of an array, but ignore the results:

 
    foreach $iterator (@array) {
        some_func($iterator);
    }  

To call a function on each integer in a (small) range, you can use:

 
    @results = map { some_func($_) } (5 .. 25);  

but you should be aware that the .. operator creates an array of all integers in the range. This can take a lot of memory for large ranges. Instead use:

 
    @results = ();
    for ($i=5; $i < 500_005; $i++) {
        push(@results, some_func($i));
    }  

This situation has been fixed in Perl5.005. Use of .. in a for loop will iterate over the range, without creating the entire range.

 
    for my $i (5 .. 500_005) {
        push(@results, some_func($i));
    }  

will not create a list of 500,000 integers.

How can I output Roman numerals?

Get the http://www.cpan.org/modules/by-module/Roman module.

Why aren't my random numbers random?

If you're using a version of Perl before 5.004, you must call srand once at the start of your program to seed the random number generator. 5.004 and later automatically call srand at the beginning. Don't call srand 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 :-). see the random artitcle in the "Far More Than You Ever Wanted To Know" collection in http://www.cpan.org/olddoc/FMTEYEWTK.tgz , 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.''

If you want numbers that are more random than rand with srand provides, you should also check out the Math::TrulyRandom module from CPAN. It uses the imperfections in your system's timer to generate random numbers, but this takes quite a while. If you want a better pseudorandom generator than comes with your operating system, look at ``Numerical Recipes in C'' at http://www.nr.com/ .

How do I get a random number between X and Y?

Use the following simple function. It selects a random integer between (and possibly including!) the two given integers, e.g., random_int_in(50,120)

 
   sub random_int_in ($$) {
     my($min, $max) = @_;
      # Assumes that the two arguments are integers themselves!
     return $min if $min == $max;
     ($min, $max) = ($max, $min)  if  $min > $max;
     return $min + int rand(1 + $max - $min);
   }  

 

 

 

Domain name registration service & domain search - 
Register cheap domain name from $7.95 and enjoy free domain services 
 

Cheap domain name search service -
Domain name services at just
$8.95/year only
 


Buy domain name registration and cheap domain transfer at low, affordable price.

2002-2004 Active-Venture.com Web Site Hosting Service

 

[ Beware of bugs in the above code; I have only proved it correct, not tried it.   ]

 

 
 
 

Disclaimer: This documentation is provided only for the benefits of our web hosting customers.
For authoritative source of the documentation, please refer to http://www.perldoc.com