Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!unmvax!indri!polyslo!usc!randvax!celia!peter From: peter@celia.UUCP (Peter Farson) Newsgroups: comp.graphics Subject: Re: Pixar's noise function Keywords: random numbers, Byte, elephants Message-ID: <465@celia.UUCP> Date: 22 Apr 89 00:57:10 GMT References: <2553@ssc-vax.UUCP> <3599@pixar.UUCP> <97699@sun.Eng.Sun.COM> <1592@ccncsu.ColoState.EDU> <43327@tut.cis.ohio-state.edu> <1648@ccncsu.ColoState.EDU> <1771@skinner.nprdc.arpa> <1669@ccncsu.ColoState.EDU> Reply-To: celia!peter@tis.llnl.gov (Peter Farson) Organization: Rhythm & Hues, Inc., Hollywood Lines: 46 In article <1669@ccncsu.ColoState.EDU> bullerj@handel.colostate.edu.UUCP (Jon Buller) writes: >In article <1771@skinner.nprdc.arpa> malloy@nprdc.arpa (Sean Malloy) writes: >>If I understand the problem correctly, writing a floor() function is >>extremely simple: > >>int floor(x) >>double x; >>{ >> if (x >= 0.0) return(trunc(x)); >> else return(trunc(x)+1); >>} > > >Well, that almost works, if x = -1.0, floor = -1.0, your_func = 0.0 >(you meant return(trunc(x)-1); didn't you?) in this case, your_func = -2.0, >this ----> ^^^^^^^^ will work if x is NOT an integer, will break if it is, >and it is admittedly a simple thing to fix, but as I said before, I've been >LAZY. I can't use the thing in a render quite yet, so I haven't bothered. > How about: int floor(x) double x; { if (x >= 0.0) return(trunc(x)); else return(trunc(x-.999999)); } The only problem with this is with the accuracy of the .999999 constant. Ideally you would want a floating point binary constant with all bits set to 1 in the mantissa, to the precision of the binary floating point format of the machine. Any error in this value should not be too big a deal though in most cases - it just causes values that are very close to an integer to be treated as though they are an integer (-1.00000001 being mistaken for -1.0000000000, for example). But there is one pitfall: if there are too many decimal digits in this constant for the machine's precision, it might get rounded up to 1.00, which would make the function return incorrect results for negative integers. -- Can a bee be said to be Peter Farson An entire bee if celia!peter@tis.llnl.gov Half the bee is not a bee ...{ihnp4,ames}!lll-tis!celia!peter Due to some ancient injury?