9/20/2023 0 Comments Perl string comparison![]() The operators compare the ASCII values of the characters of the strings in the usual fashion. These operators are FORTRAN-like, as in lt for less-than, and so on. You can't just stick the two values close to each other.Īnother set of operators for strings are the string comparison operators. Note that the concatenation must be explicitly called for with the ". The resulting (longer) string is then available for further computation or to be stored into a variable. (Yes, that's a single period.) This does not alter either string, any more than 2+3 alters either 2 or 3. String values can be concatenated with the ". For details, see Chapter 7 of Programming Perl or the online documentation on these modules. 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. Perl provides the typical ordinary addition, subtraction, multiplication, and division operators, and so on. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |