On Sat, 22 Aug 2015 16:57:41 hw wrote:
> Am 22.08.2015 um 15:43 schrieb Alan McKinnon:
> > On 22/08/2015 15:26, hw wrote:
> >> Hi,
> >> 
> >> I have the following in a perl script:
> >>        if ($a != $b) {
> >>        
> >>          print "e: '$a', t: '$b'\n";
> >>        
> >>        }
> >> 
> >> That will print:
> >> 
> >> e: '69.99', t: '69.99'
> >> 
> >> 
> >> When I replace != with ne (if ($a ne $a) {), it doesn't print.
> >> 
> >> 
> >> Is that a bug or a feature?  And if it's a feature, what's the
> >> explanation?


> > != does a numerical comparison. More on this below
> > ne does a string comparison. When viewed as a bunch of 
characters, 69.99
> > and 69.99 are identical.

> > Now, your comparisons are NOT random. They are entirely 
predictable, as
> > long as you know what is going on; you are running into floating 
point
> > numbers. And as it turns out, computers never represent these 
things
> > exactly (they are NOT integers). Even though they look identical
> > on-screen, in RAM they will not be (this must be so for perl to do the
> > print). Maybe they actually resolve to 69.990000001 and 
69.99000000. You
> > see them as close-as-dammit equal, perl sees them as entirely 
different.
> 
> Why can't it print the number as it is, or at least as it is compared,
> like it should?  If it would, one could see at once what the problem is.


Your print values are coming from the original variables. The numeric 
comparison does an internal conversion to two temporary variables, 
and compares those, WITHOUT affecting the original data.

This string to float conversion appears not to be deterministic, if it 
converts two identical strings into two different float values, but I don't 
know enough about the internals to comment any further.


-- 
Reverend Paul Colquhoun, ULC.     http://andor.dropbear.id.au/
  Asking for technical help in newsgroups?  Read this first:
     http://catb.org/~esr/faqs/smart-questions.html#intro

Reply via email to