- Prison Code Breaker Diary -

=> aka: Nhật Kí Code Tù

Categories

In Perl, the floating-point number can be written in the form like this: 1E+01, 23.45e-02, 8.567E+32 ...
The letter 'E' or 'e' denotes the exponent.

Here the case:

my $var_1 = 11E+30;
my $var_2 = 11e30;
my $small_number = 0.00001; # or 1e-05

print "First case: $var_1 - $var_2 + $small_number = ".($var_1 - $var_2 + $small_number)."\n";
print "Second case: $var_1 + $small_number - $var_2 = ".($var_1 + $small_number - $var_2)."\n";
Here the result:
First case: 1.1e+031 - 1.1e+031 + 1e-005 = 1e-005
Second case: 1.1e+031 + 1e-005 - 1.1e+031 = 0
So how does it actually work?
I can explain it like this.
At line 5, $var_1 and $var_2 is eliminated down to 0 since they're equal in value, thus, adding a small value will be kept.
At line 6, $var_1 will be adding the $small_number, but because the value is too small compared to the other side, then it is considered as 0; so, the next substract to $var_2 will be 0 in result.

Check another example to see if the value is too small
my $too_small = 1e-999;
print "value is $too_small\n";
The result is 0 certainly, as I explained above.

0 comments

Post a Comment