ЭЛЕКТРОННАЯ БИБЛИОТЕКА КОАПП
Сборники Художественной, Технической, Справочной, Английской, Нормативной, Исторической, и др. литературы.



Perl Cookbook

Perl CookbookSearch this book
Previous: 1.18. Program: psgrepChapter 2Next: 2.1. Checking Whether a String Is a Valid Number
 

2. Numbers

Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin.

- John von Neumann (1951)

2.0. Introduction

Numbers, the most basic data type of almost any programming language, can be surprisingly tricky. Random numbers, numbers with decimal points, series of numbers, and the conversion of strings to numbers all pose trouble.

Perl works hard to make life easy for you, and the facilities it provides for manipulating numbers are no exception to that rule. If you treat a scalar value as a number, Perl converts it to one. This means that when you read ages from a file, extract digits from a string, or acquire numbers from any of the other myriad textual sources that Real Life pushes your way, you don't need to jump through the hoops created by other languages' cumbersome requirements to turn an ASCII string into a number.

Perl tries its best to interpret a string as a number when you use it as one (such as in a mathematical expression), but it has no direct way of reporting that a string doesn't represent a valid number. Perl quietly converts non-numeric strings to zero, and it will stop converting the string once it reaches a non-numeric character  - so "A7" is still 0, and "7A" is just 7. (Note, however, that the -w flag will warn of such improper conversions.) Sometimes (such as when validating input) you need to know if a string represents a valid number. We show you how in Recipe 2.1.

Recipe 2.16 shows how to get a number from strings containing hexadecimal or octal representations of numbers like "0xff". Perl automatically converts literals in your program code (so $a = 3 + 0xff will set $a to 258) but not data read by that program (you can't read "0xff" into $b and then say $a = 3 + $b to make $a become 258).

As if integers weren't giving us enough grief, floating-point numbers can cause even more headaches. Internally, a computer represents numbers with decimal points as floating-point numbers in binary format. Floating-point numbers are not the same as real numbers; they are an approximation of real numbers, with limited precision. Although infinitely many real numbers exist, you only have finite space to represent them, usually about 64 bits or so. You have to cut corners to fit them all in.

When numbers are read from a file or appear as literals in your program, they are converted from decimal representation (e.g., 0.1) to internal representation. 0.1 can't be precisely represented as a binary floating-point number, just as 1/3 can't be exactly represented as a non-repeating decimal number. The computer's binary representation of 0.1, therefore, isn't exactly 0.1. To 20 decimal places, it is 0.10000000000000000555.

Performing arithmetic on binary representations of floating-point numbers can accumulate errors in the representations. In the preceding example, 3 * 0.1 is not stored with the same bit pattern as 0.3. This means you can't blindly test equality with == when you use Perl's floating-point numbers. Working with floating-point numbers is the subject of Recipes Recipe 2.2 and Recipe 2.3.

Recipe 2.4 shows how to convert an ASCII string representing a binary number (e.g., "1001") into an integer (e.g., 9) and back again. Recipe 2.5 gives three ways to perform one operation on each element of a set of consecutive integers. We show how to convert to and from Roman numerals in Recipe 2.6.

Random numbers are the topic of several recipes. Perl's rand function returns a floating-point value between 0 and 1 or between 0 and its argument. We show how to get random numbers in a given range, how to make random numbers more randomly, and how to make rand give a different set of random numbers each time you run your program.

We round out the chapter with recipes on trigonometry, logarithms, matrix multiplication, complex numbers, and the often-asked question: "How do you put commas in numbers?"


Previous: 1.18. Program: psgrepPerl CookbookNext: 2.1. Checking Whether a String Is a Valid Number
1.18. Program: psgrepBook Index2.1. Checking Whether a String Is a Valid Number