|
|
 | | From: | Krishna Myneni | | Subject: | FMOD in Forth | | Date: | Fri, 21 Jan 2005 21:43:30 -0800 |
|
|
 | [I tried to reply to Anton's original message, but it was apparently deleted from my newsgroup server. It is copied below, and belongs as part of the FMOD in Forth thread].
Anton Ertl wrote:
> Krishna Myneni writes: > >> After re-reading DPANS94, Floating Point Extensions, >> I find there is no equivalent of the C library function >> fmod() which returns the remainder (fractional part) of >> the result of dividing two fp numbers. Needing this, I >> had to resort to the clumsy and inefficient source level >> definition > > >> : FMOD ( f1 f2 -- f | remainder of f1/f2 ) >> FOVER FOVER F/ F>D D>F F* F- ; > > > It divides flags?-) Floats are indicated by an r prefix. >
Ok. This is an overdue reply. As some of you already know, we have had two new additions to our family: our twin daughters. So I've had my hands full, literally, day and night. [Mother and daughters are doing fine]. Well, onto the reply.
stack notation: f to represent a floating pt number.
I don't believe I'm the first to use this notation. In fact I probably picked it up from my days working with LMI Forth.
> This is an explicitly round-towards-zero-symmetric operation, whereas > MOD in Forth does not define division rounding and FM/MOD is > explicitly floored. Maybe it should be called FREM in analogy with > SM/REM (OTOH, there is the C name fmod() as precedent). OTOH, there > is a C function drem(), which works with round-to-nearest division. >
IMO, the Forth FMOD should mimick the C fmod() function. I haven't actually tested the C fmod() to see what the division rounding is, but I suspect it is truncation towards zero, as provided by F>D in my Forth definition.
> Is there any reason why you find the round-to-0 variant preferable > over the other two? >
Yes. Round to nearest or flooring is not as useful, because when working with positive real numbers for the two arguments, we would like a positive modulus, rather than a negative one. As an example, suppose we want to take the fp modulus of 270e and 360e. If we round 270e/360e to the nearest integer, rather than truncate towards zero, we would get a rounded quotient of 1e, rather than 0e. Then FMOD would return -90e rather than 270e. The latter is consistent with what we expect from a modulus function (and consistent with MOD, e.g. 270 360 MOD).
>> FMOD is particularly useful for calculations involving >> angles, which are quite common. > > > Hmm, a round-towards-zero 2pi FMOD produces results between 2pi and > -2pi, whereas a round-to-nearest 2pi FMOD produces results between pi > and -pi, and a floored 2pi FMOD produces results between 2pi and 0. >
Results in the range 0 to 2pi or 0 to -2pi are preferred, because they are easy to visualize in terms of a phase angle for a wave. When one visualizes a vector spinning on the unit circle in a counterclockwise fashion, it is easier to think of the angle as changing continuously from 0 to 2pi, rather than suffering a discontinuity in sign at pi.This is standard in the physical sciences, although mathematically it is equivalent to using the range -pi to pi.
> - anton > -- > M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html > comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html >
Krishna
|
|
|