Hi Monks,
I was trying the code from
Okay! What!?!?!?
my $value1 = 14.4;
print "value1 = $value1\n";
my $value2 = 10 + 14.4 - 10;
print "value2 = $value2\n";
if ( $value1 == $value2 ) {
print "value 1 equals value 2\n";
} else {
print "value 1 does not equal value2\n";
}
Output:
value1 = 14.4
value2 = 14.4
value 1 does not equal value2
and from
My floating point comparison does not work. Why ?
$number = 1.80;
$premium = $number * ( 1 + 10/100 );
$expected = 1.98;
print "Number 1 : $premium \n";
print "Number 2 : $expected \n";
print "Not" if $expected != $premium;
print "Equal !! ";
Output:
Number 1 : 1.98
Number 2 : 1.98
NotEqual !!
when I came up with the following behavior (tested on Perl 5.10.1 and on 5.18.2):
Swapping operands on the first sample code:
my $value2 = 10 + 14.4 - 10; # wrong evaluated
my $value2 = 14.4 + 10 - 10; # wrong evaluated
my $value2 = 10 - 10 + 14.4; # right evaluated
Using
eval:
my $value2 = eval (10 + 14.4 - 10); # right evaluated
Using
eval on the second sample code:
$number = 1.80;
$premium = eval ($number * ( 1 + 10/100 ));
$expected = 1.98;
print "Number 1 : $premium \n";
print "Number 2 : $expected \n";
print "Not" if $expected != $premium;
print "Equal !! ";
Output:
Number 1 : 1.98
Number 2 : 1.98
Equal !!
So, I got odd results depending on the order of the operands and right results on
evaluating.
How can that be?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.