PERL常见问题解答--FAQ(4)--Data: Numbers

转载 2006年06月19日 17:19:00
  • 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?

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:

 

___FCKpd___3

For example:

 

    @triple = map { 3 * 
  • 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?

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:

 

___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 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?

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:

 

___FCKpd___3

For example:

 

    @triple = map { 3 * 
  • 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?

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:

 

___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

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.

 

 


) } (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:

 

___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

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

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.

 

 


相关文章推荐

mtk faq MTKandroid平台常见问题解答

  • 2013年08月05日 12:20
  • 152KB
  • 下载

Cortex-M3 FAQ 常见问题解答2012

Q1: Cortex-M3是什么?       它是ARM在重视MCU市场后,设计的一个用于32位单片机的处理器内核。Cortex-M3一经推出就得到整个业界的追捧,其先进的架构和很多突破性的特性更...

subversion FAQ(常见问题解答)

  • 2009年04月29日 11:19
  • 493KB
  • 下载

FAQ-SL900A-SL900A常见问题解答

  • 2015年12月22日 10:47
  • 183KB
  • 下载

套磁常见问题解答FAQ(转)

http://www.sharewithu.com/thread-486173-1-1.html原文地址 正文: 又是一年申请季。很多版友即将结束各种考试开始全面的申请事宜。这其中,选校和套磁...

PERL常见问题解答(HTML)

  • 2005年06月24日 02:01
  • 0B
  • 下载

jbpm4——常见问题解答

转载地址:http://inotgaoshou.iteye.com/blog/679091 转自http://www.family168.com/jbpm4/faq.html...

欧陆590常见问题解答

  • 2015年08月13日 01:19
  • 389KB
  • 下载

PLL设计中常见问题解答ADI

  • 2017年03月24日 16:31
  • 6.33MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章: PERL常见问题解答--FAQ(4)--Data: Numbers
举报原因:
原因补充:

(最多只允许输入30个字)