POV-Ray : Newsgroups : povray.pov4.discussion.general : floating point precision : Re: floating point precision Server Time
19 Apr 2024 04:11:59 EDT (-0400)
  Re: floating point precision  
From: clipka
Date: 12 Oct 2009 21:19:50
Message: <4ad3d5b6@news.povray.org>
Woody schrieb:
> 
> I realize now that it was coming up short since inc never actually equals 1, but
> somethine closer to 0.9999999999.

> I'm assuming there is a perfectly legit reason as to why this was happening
> (having to do whith floating point precision) but Is there anyway to get around
> this?

The underlying problem is the same as if you'd try to compute 1/3 + 1/3 
+ 1/3 + ... using decimal numbers with a certain limit in precision; as 
you can't exactly represent 1/3 this way, you'd get e.g.:

   0.33 + 0.33 = 0.66
   0.66 + 0.33 = 0.99
   0.99 + 0.33 = 1.32
   ...

In practice, if you have a fixed value to add, you can usually 
"re-normalize" the whole problem to use integers for your loop variable; 
e.g. instead of:

   #local Foo = 0.0;
   #while (Foo <= 1.0)
     ...
     #local Foo = Foo + MySmallValue
   #end

you can use:

   #local Bar = 0.0;
   #while (Bar * MySmallValue <= 1.0)
     #local Foo = Bar * MySmallValue;
     ...
     #local Bar = Bar + 1.0;
   #end

Somewhat more generally, any integer divided by any power of 2 is safe 
to add (unless it's extremely small compared to the "target value").


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.