FixedPoint asFloat -- is it safe?
Author Message
FixedPoint asFloat -- is it safe?

My understanding of floating point numbers is they aren't precise since many
numbers can't be represented as binary fractions.  In testing them in the
workspace they seem to convert OK, but is there a way to be sure?

123.45s asFloat (123.45)
44.99 asFloat (44.99)

--
.tom

Sat, 19 Apr 2003 13:31:04 GMT
FixedPoint asFloat -- is it safe?

Quote:

> My understanding of floating point numbers is they aren't precise since many
> numbers can't be represented as binary fractions.  In testing them in the
> workspace they seem to convert OK, but is there a way to be sure?

> 123.45s asFloat (123.45)
> 44.99 asFloat (44.99)

If this were really true, we wouldn't need classes such as FixedPoint
and Fraction.  Float (or Double) would suffice.

Since Float is less precise than FixedPoint, try converting to Float
and then see if that is equal to the original.  Unfortunately, Smalltalk
deals with the mixed comparison by converting the FixedPoint to a Float.
So try this:

(123.45s asFloat asFixedPoint: 2) = 123.45s
(123.50s asFloat asFixedPoint: 2) = 123.50s

The second case can be precisely represented as a binary fraction
and will compare as true.  The first case will be false.

Another technique is to convert to a more precise representation than
Float and see if you get the same converted answer:

44.99s asFloat = 44.99s asDouble

Be aware that your initial test was only really testing rounding
precision of the printOn: method.  And even my technique is testing
(to a certain extent)  the precision of the conversion code.  But
it's much better evidence than the printed strings.

Sun, 20 Apr 2003 05:59:32 GMT
FixedPoint asFloat -- is it safe?
If you want to see it{*filter*}up try using numbers with a large amount of decimal
places.  As far as I know they are safe to within a certain number of places.
Trying to limit yourself to that number of places is probably not worth it.  You
may also be seeing some old comments about how fixedpoints act.  They use to be
implemented differently.

Chris Lopeman

Quote:

> > My understanding of floating point numbers is they aren't precise since many
> > numbers can't be represented as binary fractions.  In testing them in the
> > workspace they seem to convert OK, but is there a way to be sure?

> > 123.45s asFloat (123.45)
> > 44.99 asFloat (44.99)

> If this were really true, we wouldn't need classes such as FixedPoint
> and Fraction.  Float (or Double) would suffice.

> Since Float is less precise than FixedPoint, try converting to Float
> and then see if that is equal to the original.  Unfortunately, Smalltalk
> deals with the mixed comparison by converting the FixedPoint to a Float.
> So try this:

> (123.45s asFloat asFixedPoint: 2) = 123.45s
> (123.50s asFloat asFixedPoint: 2) = 123.50s

> The second case can be precisely represented as a binary fraction
> and will compare as true.  The first case will be false.

> Another technique is to convert to a more precise representation than
> Float and see if you get the same converted answer:

> 44.99s asFloat = 44.99s asDouble

> Be aware that your initial test was only really testing rounding
> precision of the printOn: method.  And even my technique is testing
> (to a certain extent)  the precision of the conversion code.  But
> it's much better evidence than the printed strings.

Mon, 21 Apr 2003 03:16:34 GMT

 Page 1 of 1 [ 3 post ]

Relevant Pages
 11. FixedPoint