![]() When we use them in a multiplication, we get "inf" again: $ perl -le 'print 1 * "inf"' Similarly, Perl recognizes the special value Inf, Inf, -Inf, Infinity. In the case where a perl does not support this, we’re back to the original string-to-number conversion rule that returns 0. This means, then, that to check if a perl supports "NaN", we can test: die "This perl does not support NaN!\n" if "NaN" = "NaN" In fact, "NaN" is never equal to anything, so "NaN" != "NaN" if true. $ perl -le 'print "Not the same!" unless undef = undef' We didn’t tell you that could do that.Īlso, "NaN" isn’t numerically equal to itself, which is curious since undef is: $ perl -le 'print "Not the same!" unless "NaN" = "NaN"' If one of the operands is "NaN", the operator returns undef. The spaceship operator normally returns 1, 0, or -1. Perl will allow ignore a leading or - sign as well: $ perl -le 'print " NaN"' Any string that starts with "NaN" also turns into “not a number”: $ perl -le 'print "Nandor" 1' It’s more than just the string "NaN" that turns into this special value. It won’t accidentally turn into 0, making you think that your calculation is accurate or destroying all values in multiplication.īut, it gets trickier. Now here’s some stuff you might not need: $ perl -le 'print "NaN" 1'Īny calculations we try to do with this value turns the rest of the results into "NaN". It’s one of the white lies we tell so we don’t fill your head with stuff you probably won’t need. If we applied Perl’s basic rule to the string "NaN", we’d expect to get 0, because we didn’t tell you about this special value in Learning Perl. ![]() For instance, "1234fred" becomes 1234 while "fred" becomes the empty string which becomes 0. Whatever it grabbed so far becomes the number. When it encounters something that doesn’t look like it belongs in a decimal number, it stops. As we explain in the “Scalars” chapter, Perl grabs decimal number characters from the start of the string. This value, which isn’t a number, returns itself in any mathematical operation.īefore we look at that, though, remember what Perl does with strings that might be numbers. The standard for floating-point numbers, IEEE 754. The first, the “not a number”, is the string “NaN”, in any case. Or, it has them if your underlying C library supports them. ![]() Perl has some “numbers” that aren’t really numbers.
0 Comments
Leave a Reply. |