[Ns-developers] looking for solution for floating point differences
Olivier Dalle
Olivier.Dalle at sophia.inria.fr
Fri Jan 19 01:43:21 PST 2007
Be careful, adding epsilon in any case does not solve the problem but
only translate it Epsilon backward :
Let's call:
- Alpha(n) the smallest FP result in range [(n-1); n] that int-cast to
(n-1) on some arch and to (n) on some other.
Now let's consider what happen to another FP result Beta(n) such that
(unfortunately) Beta(n)=Alpha(n)-Epsilon
With fuzzy_cast(f), provided that we choosed Epsilon s.t. Alpha(n) is in
[(n-Epsilon); n], then we have :
- fuzzy_cast(Alpha(n)) -> n on any platform
- fuzzy_cast(Beta(n)) -> (n-1) on any platform
( because Beta(n) < (n-Epsilon) <= Alpha(n) the *smallest* FP
result ... )
While with (int)(f+Epsilon) we have :
- (int)(Alpha(n)+Epsilon) -> n on any platform
- (int)(Beta(n)+Epsilon) = (int)Alpha(n) -> ? (depends on the platform)
Olivier.
On Thu, 2007-01-18 at 22:39 -0800, Tom Henderson wrote:
> > // Choose Epsilon s.t. any FP in range [(n - Epsilon); n]
> > // will be converted to int value n instead of n-1.
> > #define Epsilon 0.00001
> >
> > int fuzzy_cast(double f) {
> > int try = (int)f;
> > if ( (f- (float)try) >= (1.0-Epsilon) )
> > // Adding a small value such as 2*Epsilon may be risky
> > // if Epsilon is very small and f is big, while 0.5 should
> > // work in any case
> > return (int)(f+0.5);
> > else
> > return try;
> > }
> >
> > and then
> >
> > int bpstorate = fuzzy_cast(log(Bps / 5000) / log(2));
> >
> > Regards,
> >
> > Olivier.
>
> That works, provided that you avoid the reserved word "try". So does
> simply adding Epsilon before doing an int cast.
>
> I've asked Sally which she prefers and we'll fix it soon after.
>
> Thanks,
> Tom
More information about the Ns-developers
mailing list