- Why am I getting long decimals (eg, 19.9499999999999) instead of the numbers I should be getting (eg, 19.95)?
- Why isn't my octal data interpreted correctly?
- Does perl have a round function? What about ceil() and floor()?
- How do I convert bits into ints?
- How do I multiply matrices?
- How do I perform an operation on a series of integers?
- How can I output Roman numerals?
- Why aren't my random numbers random?

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

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 the 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 $# 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.

To get rid of the superfluous digits, just use a format (eg, `printf("%.2f", 19.95)`

) to get the required precision.

### 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. 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. `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''.

This problem shows up most often when people try using `chmod(),`

`mkdir(),`

`umask(),`

or `sysopen(),`

which all want permissions in octal.

chmod(644, $file); # WRONG -- perl -w catches this chmod(0644, $file); # right

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

For rounding to a certain number of digits, `sprintf()`

or `printf()`

is usually the easiest route.

The POSIX module (part of the standard perl distribution) implements `ceil(),`

`floor(),`

and a number of other mathematical and trigonometric functions.

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.

### How do I convert bits into ints?

To turn a string of 1s and 0s like '10110110' into a scalar containing its binary value, use the `pack()`

function (documented in pack):

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

Here's an example of going the other way:

$binary_string = join('', unpack('B*', "/x29"));

### 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(

- Why am I getting long decimals (eg, 19.9499999999999) instead of the numbers I should be getting (eg, 19.95)?
- Why isn't my octal data interpreted correctly?
- Does perl have a round function? What about ceil() and floor()?
- How do I convert bits into ints?
- How do I multiply matrices?
- How do I perform an operation on a series of integers?
- How can I output Roman numerals?
- Why aren't my random numbers random?

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 the 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 $# 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.

To get rid of the superfluous digits, just use a format (eg, `printf("%.2f", 19.95)`

) to get the required precision.

### 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. 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. `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''.

This problem shows up most often when people try using `chmod(),`

`mkdir(),`

`umask(),`

or `sysopen(),`

which all want permissions in octal.

chmod(644, $file); # WRONG -- perl -w catches this chmod(0644, $file); # right

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

For rounding to a certain number of digits, `sprintf()`

or `printf()`

is usually the easiest route.

The POSIX module (part of the standard perl distribution) implements `ceil(),`

`floor(),`

and a number of other mathematical and trigonometric functions.

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.

### How do I convert bits into ints?

To turn a string of 1s and 0s like '10110110' into a scalar containing its binary value, use the `pack()`

function (documented in pack):

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

Here's an example of going the other way:

$binary_string = join('', unpack('B*', "/x29"));

### 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:

___FCKpd___3

For example:

@triple = map { 3 *

- Why isn't my octal data interpreted correctly?
- Does perl have a round function? What about ceil() and floor()?
- How do I convert bits into ints?
- How do I multiply matrices?
- How do I perform an operation on a series of integers?
- How can I output Roman numerals?
- Why aren't my random numbers random?

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 the 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 $# 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.

To get rid of the superfluous digits, just use a format (eg, `printf("%.2f", 19.95)`

) to get the required precision.

### 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. 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. `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''.

This problem shows up most often when people try using `chmod(),`

`mkdir(),`

`umask(),`

or `sysopen(),`

which all want permissions in octal.

chmod(644, $file); # WRONG -- perl -w catches this chmod(0644, $file); # right

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

For rounding to a certain number of digits, `sprintf()`

or `printf()`

is usually the easiest route.

The POSIX module (part of the standard perl distribution) implements `ceil(),`

`floor(),`

and a number of other mathematical and trigonometric functions.

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.

### How do I convert bits into ints?

To turn a string of 1s and 0s like '10110110' into a scalar containing its binary value, use the `pack()`

function (documented in pack):

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

Here's an example of going the other way:

$binary_string = join('', unpack('B*', "/x29"));

### 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(

- Why isn't my octal data interpreted correctly?
- Does perl have a round function? What about ceil() and floor()?
- How do I convert bits into ints?
- How do I multiply matrices?
- How do I perform an operation on a series of integers?
- How can I output Roman numerals?
- Why aren't my random numbers random?

`printf(),`

or the current output format for numbers (see $# if you use print. `$#`

has a different default value in Perl5 than it did in Perl4. Changing `$#`

yourself is deprecated.

**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.

`printf("%.2f", 19.95)`

) to get the required precision.

### Why isn't my octal data interpreted correctly?

`oct()`

or `hex()`

if you want the values converted. `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''.

`chmod(),`

`mkdir(),`

`umask(),`

or `sysopen(),`

which all want permissions in octal.

chmod(644, $file); # WRONG -- perl -w catches this chmod(0644, $file); # right

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

For rounding to a certain number of digits, `sprintf()`

or `printf()`

is usually the easiest route.

`ceil(),`

`floor(),`

and a number of other mathematical and trigonometric functions.

### How do I convert bits into ints?

`pack()`

function (documented in pack):

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

Here's an example of going the other way:

$binary_string = join('', unpack('B*', "/x29"));

### How do I multiply matrices?

### 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:

___FCKpd___3

For example:

___FCKpd___4

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

foreach $iterator (@array) { &my_func($iterator); }

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

@results = map { &my_func(

- Why isn't my octal data interpreted correctly?
- Does perl have a round function? What about ceil() and floor()?
- How do I convert bits into ints?
- How do I multiply matrices?
- How do I perform an operation on a series of integers?
- How can I output Roman numerals?
- Why aren't my random numbers random?

`printf(),`

or the current output format for numbers (see $# if you use print. `$#`

has a different default value in Perl5 than it did in Perl4. Changing `$#`

yourself is deprecated.

**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.

`printf("%.2f", 19.95)`

) to get the required precision.

### Why isn't my octal data interpreted correctly?

`oct()`

or `hex()`

if you want the values converted. `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''.

`chmod(),`

`mkdir(),`

`umask(),`

or `sysopen(),`

which all want permissions in octal.

chmod(644, $file); # WRONG -- perl -w catches this chmod(0644, $file); # right

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

For rounding to a certain number of digits, `sprintf()`

or `printf()`

is usually the easiest route.

`ceil(),`

`floor(),`

and a number of other mathematical and trigonometric functions.

### How do I convert bits into ints?

`pack()`

function (documented in pack):

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

Here's an example of going the other way:

$binary_string = join('', unpack('B*', "/x29"));

### How do I multiply matrices?

### 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(

- Why isn't my octal data interpreted correctly?
- Does perl have a round function? What about ceil() and floor()?
- How do I convert bits into ints?
- How do I multiply matrices?
- How do I perform an operation on a series of integers?
- How can I output Roman numerals?
- Why aren't my random numbers random?

`printf(),`

or the current output format for numbers (see $# if you use print. `$#`

has a different default value in Perl5 than it did in Perl4. Changing `$#`

yourself is deprecated.

**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.

`printf("%.2f", 19.95)`

) to get the required precision.

### Why isn't my octal data interpreted correctly?

`oct()`

or `hex()`

if you want the values converted. `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''.

`chmod(),`

`mkdir(),`

`umask(),`

or `sysopen(),`

which all want permissions in octal.

chmod(644, $file); # WRONG -- perl -w catches this chmod(0644, $file); # right

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

For rounding to a certain number of digits, `sprintf()`

or `printf()`

is usually the easiest route.

`ceil(),`

`floor(),`

and a number of other mathematical and trigonometric functions.

### How do I convert bits into ints?

`pack()`

function (documented in pack):

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

Here's an example of going the other way:

$binary_string = join('', unpack('B*', "/x29"));

### How do I multiply matrices?

### 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:

___FCKpd___3

For example:

@triple = map { 3 *

- Why isn't my octal data interpreted correctly?
- Does perl have a round function? What about ceil() and floor()?
- How do I convert bits into ints?
- How do I multiply matrices?
- How do I perform an operation on a series of integers?
- How can I output Roman numerals?
- Why aren't my random numbers random?

`printf(),`

or the current output format for numbers (see $# if you use print. `$#`

has a different default value in Perl5 than it did in Perl4. Changing `$#`

yourself is deprecated.

**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.

`printf("%.2f", 19.95)`

) to get the required precision.

### Why isn't my octal data interpreted correctly?

`oct()`

or `hex()`

if you want the values converted. `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''.

`chmod(),`

`mkdir(),`

`umask(),`

or `sysopen(),`

which all want permissions in octal.

chmod(644, $file); # WRONG -- perl -w catches this chmod(0644, $file); # right

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

For rounding to a certain number of digits, `sprintf()`

or `printf()`

is usually the easiest route.

`ceil(),`

`floor(),`

and a number of other mathematical and trigonometric functions.

### How do I convert bits into ints?

`pack()`

function (documented in pack):

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

Here's an example of going the other way:

$binary_string = join('', unpack('B*', "/x29"));

### How do I multiply matrices?

### 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(

- Why isn't my octal data interpreted correctly?
- Does perl have a round function? What about ceil() and floor()?
- How do I convert bits into ints?
- How do I multiply matrices?
- How do I perform an operation on a series of integers?
- How can I output Roman numerals?
- Why aren't my random numbers random?

`printf(),`

or the current output format for numbers (see $# if you use print. `$#`

has a different default value in Perl5 than it did in Perl4. Changing `$#`

yourself is deprecated.

**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.

`printf("%.2f", 19.95)`

) to get the required precision.

### Why isn't my octal data interpreted correctly?

`oct()`

or `hex()`

if you want the values converted. `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''.

`chmod(),`

`mkdir(),`

`umask(),`

or `sysopen(),`

which all want permissions in octal.

chmod(644, $file); # WRONG -- perl -w catches this chmod(0644, $file); # right

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

For rounding to a certain number of digits, `sprintf()`

or `printf()`

is usually the easiest route.

`ceil(),`

`floor(),`

and a number of other mathematical and trigonometric functions.

### How do I convert bits into ints?

`pack()`

function (documented in pack):

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

Here's an example of going the other way:

$binary_string = join('', unpack('B*', "/x29"));

### How do I multiply matrices?

### 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:

___FCKpd___3

For example:

___FCKpd___4

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

foreach $iterator (@array) { &my_func($iterator); }

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

___FCKpd___6

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, &my_func($i)); }

### How can I output Roman numerals?

Get the http://www.perl.com/CPAN/modules/by-module/Roman module.

### Why aren't my random numbers random?

The short explanation is that you're getting pseudorandom numbers, not random ones, because that's how these things work. A longer explanation is available on http://www.perl.com/CPAN/doc/FMTEYEWTK/random, courtesy of Tom Phoenix.

You should also check out the Math::TrulyRandom module from CPAN.

) } @array;

For example:

___FCKpd___4

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

___FCKpd___5

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

___FCKpd___6

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:

___FCKpd___7

### How can I output Roman numerals?

Get the http://www.perl.com/CPAN/modules/by-module/Roman module.

### Why aren't my random numbers random?

The short explanation is that you're getting pseudorandom numbers, not random ones, because that's how these things work. A longer explanation is available on http://www.perl.com/CPAN/doc/FMTEYEWTK/random, courtesy of Tom Phoenix.

You should also check out the Math::TrulyRandom module from CPAN.

} @single;

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

___FCKpd___5

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

___FCKpd___6

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:

___FCKpd___7

### How can I output Roman numerals?

Get the http://www.perl.com/CPAN/modules/by-module/Roman module.

### Why aren't my random numbers random?

The short explanation is that you're getting pseudorandom numbers, not random ones, because that's how these things work. A longer explanation is available on http://www.perl.com/CPAN/doc/FMTEYEWTK/random, courtesy of Tom Phoenix.

You should also check out the Math::TrulyRandom module from CPAN.

) } @array;

For example:

___FCKpd___4

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

___FCKpd___5

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

___FCKpd___6

`..`

operator creates an array of all integers in the range. This can take a lot of memory for large ranges. Instead use:

___FCKpd___7

### How can I output Roman numerals?

Get the http://www.perl.com/CPAN/modules/by-module/Roman module.

### Why aren't my random numbers random?

You should also check out the Math::TrulyRandom module from CPAN.

) } (5 .. 25);

`..`

operator creates an array of all integers in the range. This can take a lot of memory for large ranges. Instead use:

___FCKpd___7

### How can I output Roman numerals?

Get the http://www.perl.com/CPAN/modules/by-module/Roman module.

### Why aren't my random numbers random?

You should also check out the Math::TrulyRandom module from CPAN.

) } @array;

For example:

___FCKpd___4

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

___FCKpd___5

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

___FCKpd___6

`..`

operator creates an array of all integers in the range. This can take a lot of memory for large ranges. Instead use:

___FCKpd___7

### How can I output Roman numerals?

Get the http://www.perl.com/CPAN/modules/by-module/Roman module.

### Why aren't my random numbers random?

You should also check out the Math::TrulyRandom module from CPAN.

} @single;

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

___FCKpd___5

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

___FCKpd___6

`..`

operator creates an array of all integers in the range. This can take a lot of memory for large ranges. Instead use:

___FCKpd___7

### How can I output Roman numerals?

Get the http://www.perl.com/CPAN/modules/by-module/Roman module.

### Why aren't my random numbers random?

You should also check out the Math::TrulyRandom module from CPAN.

) } @array;

For example:

___FCKpd___4

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

___FCKpd___5

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

___FCKpd___6

`..`

operator creates an array of all integers in the range. This can take a lot of memory for large ranges. Instead use:

___FCKpd___7

### How can I output Roman numerals?

Get the http://www.perl.com/CPAN/modules/by-module/Roman module.

### Why aren't my random numbers random?

You should also check out the Math::TrulyRandom module from CPAN.