Gentoo Archives: gentoo-user

From: Alexander Kapshuk <alexander.kapshuk@×××××.com>
To: Gentoo mailing list <gentoo-user@l.g.o>
Subject: Re: [gentoo-user] 69.99 != 69.99
Date: Sat, 22 Aug 2015 15:39:51
Message-Id: CAJ1xhMW2o_jKa__jEJCJNaUmROEGuetsz993NPiuPf2yOn0PYw@mail.gmail.com
In Reply to: [gentoo-user] 69.99 != 69.99 by hw
1 On Sat, Aug 22, 2015 at 4:26 PM, hw <hw@×××××××××××××××××××××.de> 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 statement
25 > checks multiple values in the script, and 69.99 is the only number showing
26 > up yet which isn't numerically equal to itself (but equal to itself when
27 > compared as strings).
28 >
29
30 Perl Cookbook, 2nd edition, suggests these two approaches to comparing
31 floats for equality.
32 (1). Use sprintf to format the numbers to a certain number of decimal
33 places, then compare the resulting strings.
34 (2). Alternatively, store the numbers as integers by assuming the decimal place.

Replies

Subject Author
Re: [gentoo-user] 69.99 != 69.99 Alan McKinnon <alan.mckinnon@×××××.com>