[Bf-committers] Floating point compares
Ton Roosendaal
bf-committers@blender.org
Wed, 29 Oct 2003 13:15:36 +0100
Hi,
Floats can guaranteed express values that are powers of 2. I mean a
range of:
... 0.25 - 0.5 - 1.0 - 2.0 - 4.0 - ..... including zero.
No reason to obsfucate the code for these cases.
Different is when you want to check on values that come back from float
math. A float can express a minus zero! I did a lot of fixing on this
when porting to linux, freebsd, solaris, beos, 64 bits alpha, and
windows. Just something to be aware of... it's the reason we have a
'safe acos' for example.
I also noticed someone adding an 'f' to float constants. I don't know
what this is good for... the compiler really can do that thinking for
you, or not?
Just always use a point precision to express it's a float constant.
-Ton-
(btw; inline is C++ only)
On Wednesday, Oct 29, 2003, at 12:25 Europe/Amsterdam, Kester Maddock
wrote:
> Hi guys,
>
> I've just noticed that there are a lot of floating point compares in
> the code, ie:
> if(tex->cropxmin==0.0) ret++;
> if(tex->cropymin==0.0) ret++;
> if(tex->cropxmax==1.0) ret++;
> if(tex->cropymax==1.0) ret++;
>
> Isn't this bad? (because of the finite floating point precision.)
>
> #define FPRECISION 1e-10
> static inline int fzero(float x)
> {
> return fabs(x) < FPRECISION;
> }
>
> static inline int fequal(float x, float y)
> {
> return fzero(x - y);
> }
>
> if(fzero(tex->cropxmin)) ret++;
> if(fzero(tex->cropymin)) ret++;
> if(fequal(tex->cropxmax, 1.0)) ret++;
> if(fequal(tex->cropymax, 1.0)) ret++;
>
> GCC will catch this with -Wfloat-equal.
>
> Should I start fixing this or is there a valid reason to do it this
> way?
>
> Kester
>
> _______________________________________________
> Bf-committers mailing list
> Bf-committers@blender.org
> http://www.blender.org/mailman/listinfo/bf-committers
>
>
------------------------------------------------------------------------
--
Ton Roosendaal Blender Foundation ton@blender.org
http://www.blender.org