[Glass] 0.01 copy == 0.01

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[Glass] 0.01 copy == 0.01

Mariano Martinez Peck
Hi guys,

I was checking some differences from sixx exported files from Pharo and GemStone and I noticed in GemStone equal floats (but "different" instances) were serialized like if they were the same instance. Then I did some experiments: 

| anObject |
anObject := OrderedCollection new.
anObject add: 0.01.
anObject add: (Float readFrom: '0.01').
anObject first == anObject second  
-> true

or 

0.01 copy == 0.01    
-> true

So...is this a compiler optimization? Is it something related to the fact that SmallDouble are immediate objects? 

Thanks,

--
Mariano
http://marianopeck.wordpress.com

_______________________________________________
Glass mailing list
[hidden email]
http://lists.gemtalksystems.com/mailman/listinfo/glass
Reply | Threaded
Open this post in threaded view
|

Re: [Glass] 0.01 copy == 0.01

James Foster-9
On Feb 27, 2014, at 4:58 AM, Mariano Martinez Peck <[hidden email]> wrote:

> 0.01 copy == 0.01    
> -> true
>
> So...is this a compiler optimization? Is it something related to the fact that SmallDouble are immediate objects?

It is not a compiler optimization but a side-effect of the fact that (as you note) SmallDouble are immediate objects so there are never any distinct copies. The behavior is similar to the following:

42 copy == 42. “true”
$a copy == $a. “true”
nil copy == nil. “true"
true copy == true. “true”


James
_______________________________________________
Glass mailing list
[hidden email]
http://lists.gemtalksystems.com/mailman/listinfo/glass
Reply | Threaded
Open this post in threaded view
|

Re: [Glass] 0.01 copy == 0.01

Martin McClure-5
On 02/27/2014 06:56 AM, James Foster wrote:

> On Feb 27, 2014, at 4:58 AM, Mariano Martinez Peck <[hidden email]> wrote:
>
>> 0.01 copy == 0.01
>> -> true
>>
>> So...is this a compiler optimization? Is it something related to the fact that SmallDouble are immediate objects?
>
> It is not a compiler optimization but a side-effect of the fact that (as you note) SmallDouble are immediate objects so there are never any distinct copies. The behavior is similar to the following:
>
> 42 copy == 42. “true”
> $a copy == $a. “true”
> nil copy == nil. “true"
> true copy == true. “true”

SmallDoubles, like SmallIntegers, have a limited (though fairly large)
range. Integer operations on SmallIntegers can produce a result that
cannot fit in a SmallInteger, so the result is a LargeInteger.
Similarly, operations on SmallDoubles can produce a result that is
either too close to zero (but not exactly zero) or too far from zero to
be represented as a SmallDouble, so the result will in those cases be a
Float.

Regards,

-Martin
_______________________________________________
Glass mailing list
[hidden email]
http://lists.gemtalksystems.com/mailman/listinfo/glass