Gentoo Archives: gentoo-user

From: Fernando Rodriguez <frodriguez.developer@×××××××.com>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] 69.99 != 69.99
Date: Sat, 22 Aug 2015 21:26:52
Message-Id: BLU436-SMTP15D55154A92FE6903F57F8D640@phx.gbl
In Reply to: [gentoo-user] 69.99 != 69.99 by hw
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