Floating Point Numbers and Smalltalk Behavior

VA Smalltalk is a "100% VisualAge compatible" IDE that includes the original VisualAge technology and the popular VA Assist and WidgetKit add-ons.

Moderators: Eric Clayberg, wembley, tc, Diane Engles, solveig

Floating Point Numbers and Smalltalk Behavior

Postby sharma.narender » Fri Jan 21, 2011 4:06 am

I am a bit surprised by how smalltalk behaves the way it does for the Floating point numbers for the follwoing scenearios...

#1 When I inspect 644444.4444444449 the Float Inspector window shows me value as 644444.444444445.
Is this correct ?
However when I inspect 64444.4444444449 the Float Inspector window shows me value as 64444.4444444449
#2 When I inspect 644444444444444.9 the Float Inspector window shows me value as 644444444444445.0
Is this correct ?
However when I inspect 64444444444444.9 the Float Inspector window shows me value as 64444444444444.9

I tried various code snippents and found that if floating point number has total number of digits(count the digits on the left and right side of the decimal point) greater than 15, samlltalk starts rounding the digits
Can someone from Instantiations look at this ? and explain the reason behind such a behavior.

I am runnning the code on Visula Age Smalltalk ver 7.5


Thanks in advance!
-- Narender Sharma
Senior Software Engineer
aonhewitt.com
sharma.narender
 
Posts: 16
Joined: Wed Mar 31, 2010 11:13 pm
Location: GURGAON - INDIA

Re: Floating Point Numbers and Smalltalk Behavior

Postby TriSebastian » Fri Jan 21, 2011 6:38 am

Hi!

Try to inspect this: (1.2 - 1.1) asString (Expect happy Questions from your USER while this might happen on the GUI having just a TextBox on your View)

Have a look here:
Search for "Binary vs. decimal"
http://en.wikipedia.org/wiki/Fixed-point_arithmetic

This will lead you to the usage of:

(1.2 asDecimal - 1.1 asDecimal) asFloat

In your case you should search for IEEE 754. There's a restriction on Floats.

Hope this helps

Sebastian
TriSebastian
 
Posts: 76
Joined: Sun Jul 20, 2008 9:40 pm
Location: Nanaimo, BC, Canada

Re: Floating Point Numbers and Smalltalk Behavior

Postby marten » Fri Jan 21, 2011 7:25 am

floats again ...

* double precision is around 15 digits. Therefore the stuff works, when you have 15 digits (see your third example) - but you are at the limit of the possibilities there.
* all other examples need number with a precision of 16 digits and this precision is simply not guaranteed.

Whenever you have to handle double values you should be very carefully about comparing considering the machine precision - you may have a look at the source code located at http://vastgoodies.com/ and there the package DhbNumerics.

Marten

sharma.narender wrote:I am a bit surprised by how smalltalk behaves the way it does for the Floating point numbers for the follwoing scenearios...

#1 When I inspect 644444.4444444449 the Float Inspector window shows me value as 644444.444444445.
Is this correct ?
However when I inspect 64444.4444444449 the Float Inspector window shows me value as 64444.4444444449
#2 When I inspect 644444444444444.9 the Float Inspector window shows me value as 644444444444445.0
Is this correct ?
However when I inspect 64444444444444.9 the Float Inspector window shows me value as 64444444444444.9

I tried various code snippents and found that if floating point number has total number of digits(count the digits on the left and right side of the decimal point) greater than 15, samlltalk starts rounding the digits
Can someone from Instantiations look at this ? and explain the reason behind such a behavior.

I am runnning the code on Visula Age Smalltalk ver 7.5


Thanks in advance!
Marten Feldtmann, Principal Smalltalk User, Private
SkypeMe callto://marten.feldtmann
marten
[|]
 
Posts: 641
Joined: Sat Oct 14, 2006 7:10 am
Location: Hamburg - Germany

Re: Floating Point Numbers and Smalltalk Behavior

Postby a62754 » Mon Jan 24, 2011 2:39 am

From some examples its clear that limits set by IEEE 32 bit format are different than VAST's float object - smalltalk's float object has more precision than IEEE 32 bit format, but less than its 64 bit variant.
I think what the original question demands is how the IEEE 32 bit format maps to VAST 7.5. How is Float different from IEEE 32 bit floating point - what is the difference in precision and behavior; what are the limits; etc.
a62754
 
Posts: 18
Joined: Thu Jul 01, 2010 2:03 am

Re: Floating Point Numbers and Smalltalk Behavior

Postby sharma.narender » Mon Jan 24, 2011 6:24 am

It seems that smalltalk makes use of the double precision i.e. 64 bits for Floats.

Try this
64444.4444444449 exponent gives 1038
64444.4444444449 mantissa gives 4353577374158455

Now open the site... http://babbage.cs.qc.edu/IEEE-754/Decimal.html and enter 64444.4444444449 in the "Decimal Floating-Point:"and then click on Not rounded.

You will get exponent as 10000001110 which in decimal is 1038 which is equal to the exponent in smalltalk.
and significand as 1 .1111011101111000111000111000111000111000111001110111.
1111011101111000111000111000111000111000111001110111 in decimal is 4353577374158455 which is equal to mantissa in smalltalk.
The only question remaining is why we have 1. and that seems to be explained by http://support.microsoft.com/kb/42980.

However Smalltalk does not support denormalized Floats.
-- Narender Sharma
Senior Software Engineer
aonhewitt.com
sharma.narender
 
Posts: 16
Joined: Wed Mar 31, 2010 11:13 pm
Location: GURGAON - INDIA

Re: Floating Point Numbers and Smalltalk Behavior

Postby marten » Mon Jan 24, 2011 10:10 am

That's right.

In contrast to e.g. Visualworks, the Float class in VASmalltalk is a IEEE64 Bit data type (Binary64 or double precision) - there is no support for doing calcs with IEEE32 Bit floats (Binary32 or single precision).

But there is support for calling API calls with IEEE32 (Binary32) parameters.

Marten

sharma.narender wrote:It seems that smalltalk makes use of the double precision i.e. 64 bits for Floats.
Marten Feldtmann, Principal Smalltalk User, Private
SkypeMe callto://marten.feldtmann
marten
[|]
 
Posts: 641
Joined: Sat Oct 14, 2006 7:10 am
Location: Hamburg - Germany

Re: Floating Point Numbers and Smalltalk Behavior

Postby sharma.narender » Tue Apr 19, 2011 12:10 am

Thanks Everyone.
-- Narender Sharma
Senior Software Engineer
aonhewitt.com
sharma.narender
 
Posts: 16
Joined: Wed Mar 31, 2010 11:13 pm
Location: GURGAON - INDIA


Return to VA Smalltalk 7.0, 7.5 & 8.0

Who is online

Users browsing this forum: No registered users and 1 guest

cron