Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Float Objects
Why does,


| x y |

x := 1.0*1.0.
y := 1.0.

x == y. true

evaluate to true, whereas


#(0.0 1.0 2.0 3.0) select: [:i | i == (i*i)] ()

returns an empty set ?

Thanks in Advance,
Pranab Mehta

Report this thread to moderator Post Follow-up to this message
Old Post
Pranab Mehta
09-23-04 08:56 AM


Re: Float Objects
Pranab Mehta wrote(-ish):

> Why does,
> | x y |
>
> x := 1.0*1.0.
> y := 1.0.
> x == y.
> evaluate to true, whereas [...]

Which Smalltalk are you using ?  That expression comes out false on all the
implementations I've tried.

-- chris



Report this thread to moderator Post Follow-up to this message
Old Post
Chris Uppal
09-23-04 08:56 AM


Re: Float Objects
"Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> wrote in
news:tNadna5z8c9q8c_cRVn-gw@nildram.net:

> Pranab Mehta wrote(-ish):
> 
>
> Which Smalltalk are you using ?  That expression comes out false on
> all the implementations I've tried.
>
>     -- chris
>
>

IBM VisualAge SmallTalk.

Also, I noticed something different from what my reference book
suggested, the following also evaluates to true -

| x y |

x := 'hello'.
y := 'hello'.

x == y. true

Any clue, why ?

Report this thread to moderator Post Follow-up to this message
Old Post
Pranab Mehta
09-23-04 08:56 AM


Re: Float Objects
Pranab Mehta wrote:
 

> IBM VisualAge SmallTalk.

Funny ?  It comes out false for me.  This is pasted from a workspace in IBM
Smalltalk 6.0.1

| x y |

x := 1.0*1.0.
y := 1.0.

x == y.  false

I'm afraid that I have no idea at all why you are getting a different answer
.


> Also, I noticed something different from what my reference book
> suggested, the following also evaluates to true -
>
> | x y |
>
> x := 'hello'.
> y := 'hello'.
>
> x == y. true

Now that I can tell you.  (And the answer may even be correct ;-)  IBM St
merges String literal objects that are found in one compilation.  So if you
evaluate the above in one go, the two occurrences of 'hello' will be compile
d
into references to the /same/ instance of String.  This optimisation is, I
think, misguided since it saves very little and causes unnecessary confusion
.
Under "normal" circumstances the String(s) wouldn't have come from the same
compilation, and so they would only be =, not ==.  (At least, I /think/ it's
done on a per-compilation basis, rather than globally (as in Java).  That's 
how
Dolphin St implements the same idea anyway.)

Perhaps your version of IBM St is doing something similar with Float literal
s
and constant-valued expressions ??

-- chris




Report this thread to moderator Post Follow-up to this message
Old Post
Chris Uppal
09-23-04 08:56 AM


Re: Float Objects
On Thu, 23 Sep 2004 08:21:21 +0100, Chris Uppal wrote:

FWIW: it does evaluate to true in VA 5.5.2

> Pranab Mehta wrote:
> 
> 
>
> Funny ?  It comes out false for me.  This is pasted from a workspace in IB
M
> Smalltalk 6.0.1
>
> | x y |
>
> x := 1.0*1.0.
> y := 1.0.
>
> x == y.  false
>
<...>

Report this thread to moderator Post Follow-up to this message
Old Post
Bob Nemec
09-23-04 08:59 PM


Re: Float Objects
Bob Nemec wrote:
> On Thu, 23 Sep 2004 08:21:21 +0100, Chris Uppal wrote:
>
> FWIW: it does evaluate to true in VA 5.5.2

I remember that at some stage VAST parser/compiler was converting equal
literals to references to the same single instance.
I am not sure whether it was intentional or just a side effect of some
equality based lookup somewhere deep in the guts.



Regards, Jaroslaw.
 
>
> <...>

Report this thread to moderator Post Follow-up to this message
Old Post
Jaroslaw Podgajny
09-24-04 02:00 AM


Re: Float Objects
Pranab,

> Why does,
>
>
> | x y |
>
> x := 1.0*1.0.
> y := 1.0.
>
> x == y. true
>
> evaluate to true, whereas
>
>
> #(0.0 1.0 2.0 3.0) select: [:i | i == (i*i)] ()
>
> returns an empty set ?

The basic problem here is one of representing floating-point numbers on the
computer.  The computer uses some number of significant digits with an
exponent.  Multiplying (and performing other arithmetic operations on)
floating-point numbers can lead to rounding errors in some circumstances,
for example 1.0 is expressed as 0.999999999999999999 * 10^0.  The exponent
is 10^0.  Naturally, 1.0 is not equal to 0.999999999999999999 * 10^0, but it
should be because it is within the precision of the computer to distinguish
between one floating-point number and another.

Because of this, and no matter whether there is a bug in the particular
Smalltalk environment you are using, you should not use an "=" or "=="
message to compare floating-point numbers.  You should write some code to
find out what that resolution is on your computer and then use that
precision to check to see if you can distinguish between the floating-point
numbers.  If they can not be distinguished, they are equal as far as the
computer is concerned.

Hope that makes sense.

Regards,

Randy



Report this thread to moderator Post Follow-up to this message
Old Post
Randy A. Ynchausti
09-27-04 09:02 PM


Re: Float Objects
The == operator compares the object addresses (handles.)

The result will return true if the addresses are the same regardless
of what its an address of (or to.)

It depends on whether your parser evaluates two literal strings in the
parsing process to one instance (in which case the answer to == will
be true,) or to two instances (in which case the answer to == will be
false.)

-Charles-A.

"Randy A. Ynchausti" <randy.ynchausti@oocl.com> wrote in message news:<415824df@news.totall
yobjects.com>...
> Pranab,
> 
>
> The basic problem here is one of representing floating-point numbers on th
e
> computer.  The computer uses some number of significant digits with an
> exponent.  Multiplying (and performing other arithmetic operations on)
> floating-point numbers can lead to rounding errors in some circumstances,
> for example 1.0 is expressed as 0.999999999999999999 * 10^0.  The exponent
> is 10^0.  Naturally, 1.0 is not equal to 0.999999999999999999 * 10^0, but 
it
> should be because it is within the precision of the computer to distinguis
h
> between one floating-point number and another.
>
> Because of this, and no matter whether there is a bug in the particular
> Smalltalk environment you are using, you should not use an "=" or "=="
> message to compare floating-point numbers.  You should write some code to
> find out what that resolution is on your computer and then use that
> precision to check to see if you can distinguish between the floating-poin
t
> numbers.  If they can not be distinguished, they are equal as far as the
> computer is concerned.
>
> Hope that makes sense.
>
> Regards,
>
> Randy

Report this thread to moderator Post Follow-up to this message
Old Post
Charles-A. Rovira
10-01-04 08:58 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Smalltalk archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:14 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.