1 |
On Saturday, August 22, 2015 3:26:56 PM hw wrote: |
2 |
> |
3 |
> Hi, |
4 |
> |
5 |
> I have the following in a perl script: |
6 |
> |
7 |
> |
8 |
> if ($a != $b) { |
9 |
> print "e: '$a', t: '$b'\n"; |
10 |
> } |
11 |
> |
12 |
> |
13 |
> That will print: |
14 |
> |
15 |
> e: '69.99', t: '69.99' |
16 |
> |
17 |
> |
18 |
> When I replace != with ne (if ($a ne $a) {), it doesn't print. |
19 |
> |
20 |
> |
21 |
> Is that a bug or a feature? And if it's a feature, what's the explanation? |
22 |
> |
23 |
> And how do you deal with comparisions of variables when you get randomly |
24 |
> either correct results or wrong ones? It's randomly because this |
25 |
> statement checks multiple values in the script, and 69.99 is the only |
26 |
> number showing up yet which isn't numerically equal to itself (but equal |
27 |
> to itself when compared as strings). |
28 |
> |
29 |
|
30 |
Most languages have a decimal type that you should use when you need exact |
31 |
math. I think for perl this is what you want: |
32 |
|
33 |
http://search.cpan.org/~zefram/Math-Decimal-0.003/lib/Math/Decimal.pm |
34 |
|
35 |
-- |
36 |
Fernando Rodriguez |