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.

 

 


FAQ:CDN常见问题解答

1 、什么类型的网站最需要 CDN ?   答: 我们请第三方公司对我们的 CDN 做了性能测试, 测试结果表明, 从 ISP 分类 来看, 教育网的用户使用 CDN 后提速最为明显, 访问速度可以提...
  • orichisonic
  • orichisonic
  • 2016年01月18日 18:08
  • 264

TI ZigBee FAQ 常见问题解答

1,TI的ZigBee协议栈不同版本的区别,如何选择合适的协议栈进行产品开发 TI ZigBee 协议栈Z-Stack从最开始的Z-Stack 0.1到大家熟悉的Z-Stack 2.5....
  • amen315
  • amen315
  • 2015年09月29日 21:55
  • 139

TI ZigBee FAQ 常见问题解答

在TI的官方论坛中,看到一些知识点记录下,具体可以参考:  http://www.deyisupport.com/question_answer/wireless_connectivity/zigbe...
  • xingqingly
  • xingqingly
  • 2015年07月15日 17:26
  • 773

VC++中一些常见问题的解决方法

(转)VC++中一些常见问题的解决方法 (1)为什么某个类突然在工作区间里面突然看不见了? 只是类隐藏了,打开FILEVIEW,找到隐藏类的头文件,随便敲一下键盘的空格键,类就会在CLAS...
  • chidy
  • chidy
  • 2013年02月26日 13:36
  • 904

IE10和IE9兼容性常见问题解答(FAQ)

本文档介绍了诊断和解决与IE10和IE9的文档兼容性有关的具体问题的一系列过程。 1. 我们在本文档中提到“ IE10和IE9”时有什么意义? 以前版本的IE都不是符合标准的。从IE9发布...
  • xbw673009796
  • xbw673009796
  • 2014年01月15日 19:19
  • 955

perl 时间处理函数

1) Date::Calc这里是冠军模块: use strict; use warnings; use Date::Calc qw(Add_Delta_YMD); my $s...
  • frt007
  • frt007
  • 2015年05月02日 13:06
  • 1398

TI ZigBee FAQ 常见问题解答 CC2530低功耗完成

TI ZigBee FAQ 常见问题解答 作者 VV  发表于 2014-11-23 22:40 榜眼40451分 1,TI的ZigBee...
  • shjhuang
  • shjhuang
  • 2016年04月17日 23:08
  • 912

Python学习笔记 —— 数字类型【Numbers】及常用方法

Tips: 在Python中数据类型不允许改变的,如果改变了,则会重新分配内存空间。 pi: 数字常量pi(圆周率) e:自然常数 Numbers支持四种不同的数值类型:整型...
  • linfengwenyou
  • linfengwenyou
  • 2016年06月06日 15:05
  • 788

perl 打印复杂数据结构

方法1: #!/usr/bin/perl -w use strict; my %hash = ( a => 3, b => 1, c => 4); map{ print "$_ => $has...
  • Shao_You
  • Shao_You
  • 2017年01月24日 11:23
  • 506

Perl 的命令行参数(One-liner)

Perl命令行参数 执行控制 -e                     指定字符串以作为脚本(多个字符串迭加)执行 -M                    导入模块 -I       ...
  • lw370481
  • lw370481
  • 2013年12月18日 11:48
  • 4582
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章: PERL常见问题解答--FAQ(4)--Data: Numbers
举报原因:
原因补充:

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