float equality

"Jérôme M. Berger" jeberger at free.fr
Sun Feb 20 14:25:08 PST 2011


Kevin Bealer wrote:
> == Quote from Jonathan M Davis (jmdavisProg at gmx.com)'s article
> ...
>> It may be that you would still end up with situations where two values that you
>> would think would be the same aren't due to rounding error or whatnot. However,
>> with a fixed point value, you wouldn't have the problem where a particular value
>> could not be held in it even if it's within its range of precision. As I
>> understand it, there are a number of values which cannot be held in a floating
>> point and therefore end up being rounded up or down simply because of how
>> floating points work and not because there the precision isn't high enough.
>>
>> It's definitely true however, that using fractions would be much more accurate
>> for a lot of stuff. That wouldn't be particulary efficient though. Still, if you're
>> doing a lot of math that needs to be accurate, that may be the way to go.
>>
>> - Jonathan M Davis
> 
> Floating point numbers are a good compromise for a lot of purposes, but yeah they
> are limited.  Here are some ways to open up the limits a bit...
> 
> (First some math for those who haven't gone down this path...)
> 
> The reason that fixed point does better than floating is that fixed point classes
> use base ten and floating point uses base 2.  In base two, all fractions that have
> a denominator of a power of 2 can be represented (e.g. 3/16) exactly, and all others
> can't.
> 
> Fixed point solves this for numbers like .1 or .0003 because the denominator is a
> power of ten.  Ten is 5*2 and the way it works is that any denominator that is a power
> of two times a power of ten can be represented exactly.
> 
	Wrong. There is nothing that says fixed-point uses a power of ten.
Actually, I do a lot of work on embedded platforms without a FPU and
with slow integer division (and to a lesser extent multiplication).
Non-integer numbers are represented in fixed point (to work around
the lack of FPU) using base 2 (so that adjustments only require a
bitwise shift). Of course, this means that 0.1 cannot be represented
exactly...

		Jerome
-- 
mailto:jeberger at free.fr
http://jeberger.free.fr
Jabber: jeberger at jabber.fr

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: OpenPGP digital signature
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20110220/62405e01/attachment.pgp>


More information about the Digitalmars-d mailing list