![]() ![]() If this ends up being a problem, the Math::BigInt and Math::BigFloat object modules provide infinite-precision arithmetic for integers and floating-point numbers at the cost of somewhat slower execution. ![]() # 2.699999999999999733546474089962430298328399658203125ĭon't worry too much about this: the print function's default format for printing floating-point numbers usually hides such minor representational inaccuracies. But unless you are doing something extreme, you'll usually see the results you expect to see.Ĭomparing the following statements, you'll see what the computer really got as the result of the subtraction (the printf function is described in Chapter 6, Basic I/O): printf("%.51f\n", 5.1 - 2.4) Computers don't store numbers the same way a mathematician thinks of them. Instead, you get an approximation that's only accurate to a certain number of decimal places. Don't you get exactly 2.7 when subtracting 2.4 from 5.1? In math class you do, but on computers you usually don't. You may be wondering about the word "approximately" in the code comments at the start of this section. (These operators are revisited in Table 2.2.) The definitions of true and false are covered later, but for now, think of the return values as one for true, and zero for false. ![]() For example, 3 > 2 returns true because three is greater than two, while 5 != 5 returns false because it's not true that 5 is not equal to 5. The logical comparison operators are = > !=, these compare two values numerically, returning a true or false value. Both values are first reduced to their integer values, so 10.5 % 3.2 is computed as 10 % 3. The value of the expression 10 % 3 is the remainder when 10 is divided by 3, which is 1. (If the result can't fit into a double-precision floating-point number, such as a negative number to a noninteger exponent, or a large number to a large exponent, you'll get a fatal error.) The operator is represented by the double asterisk, such as 2**3, which is two to the third power, or eight. For example: 2 3 # 2 plus 3, or 5ĥ.1 - 2.4 # 5.1 minus 2.4, or approximately 2.7ġ0.2 / 0.3 # 10.2 divided by 0.3, or approximately 34ġ0 / 3 # always floating point divide, so approximately 3.3333333.Īdditionally, Perl provides the FORTRAN-like exponentiation operator, which many have yearned for in Pascal and C. I think this format is a little easier to read, and as a final note, you'll see this syntax used more commonly in Perl programs than the first syntax I showed.Perl provides the typical ordinary addition, subtraction, multiplication, and division operators, and so on. When you're performing a simple pattern match like this you can use this format, without the leading m character or the parentheses. Return unless $string =~ /print|allow|okay/ Here's how you can simplify that line of code: There's actually a shorter way to write that return unless line of code, but I didn't want to throw it at you all at once. The only lines that are printed are those that match the three patterns I'm performing a comparison against: print, allow, and okay. Return unless ($string =~ m/(print|allow|okay)/) Īs you can see I call the print_filter method four times, passing in different strings. These patterns can be anywhere in the string that is passed in, but they must be in lower case. ![]() In this case I only print the strings that contain the patterns print, allow, or okay. In this sample program I have a method named print_filter that prints lines that only match specific, simple patterns. The Perl source code below shows a simple example of the pattern matching that I'm doing in my Perl script. This is pretty easy in this case, in part because it's easy to match a string against multiple patterns in Perl, and also because my patterns are very simple - no regular expressions involved. For a Perl program that I'm working on right now, I need to match the elements of an array of strings against several patterns, and then take an action if the current string matches one of my patterns. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |