From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 101deb,704fdaec147f48c X-Google-Attributes: gid101deb,public X-Google-Thread: 103376,183ebe04e93f0506 X-Google-Attributes: gid103376,public X-Google-Thread: 107079,183ebe04e93f0506 X-Google-Attributes: gid107079,public From: rav@goanna.cs.rmit.edu.au (robin) Subject: Re: fixed point vs floating point Date: 1997/12/03 Message-ID: <66271b$r67$1@goanna.cs.rmit.edu.au>#1/1 X-Deja-AN: 294683485 Expires: 3 March 1998 00:00:00 GMT References: Distribution: inet Organization: Comp Sci, RMIT University, Melbourne, Australia. NNTP-Posting-User: rav Newsgroups: comp.lang.pl1,comp.lang.ada,sci.math.num-analysis Date: 1997-12-03T00:00:00+00:00 List-Id: >Matthew Heaney wrote: >Anyway, I'll accept that fixed point operations are slower, but the >question remains: Why do most programmers insist on using floating point >when a fixed point better models the abstraction? Is the reason solely >based on efficiency? >By the way, are there off-the-shelf subprograms (in Ada preferably, but I'd >take any language) for computing the elementary functions (sin, cos, etc) >of a fixed point angle? The following evaluates sine using fixed point, for a limited range of angles. _____________________________________________________ /* Copyright (c) 1996, 1997 by R. A. Vowels. */ /* This subroutine computes the sine of an angle using fixed-point binary arithmetic. */ SINE: PROCEDURE(XX) OPTIONS (REORDER) RETURNS (FIXED BINARY(31,28)); DECLARE XX FIXED BINARY(31,24); DECLARE X FIXED BINARY (31,28); DECLARE Sum FIXED BINARY (31,28); DECLARE Term FIXED BINARY (31,28); DECLARE (J, K) FIXED BINARY; DECLARE Neg BIT(1); DECLARE (HiX, HiY) FIXED BINARY (31,14); DECLARE (LoX, LoY) FIXED BINARY (31,28); DECLARE (ProdHiXLoY, ProdHiYLoX, Prod) FIXED BINARY (31,28); /* This macro procedure forms the product of the fixed-point arguments X1 and Y1. */ %MULT: PROCEDURE(LHS, X1, Y1); DECLARE (LHS, X1, Y1) CHARACTER; ANSWER ('HiX = ' || X1 || ';') SKIP; /* Extract the upper 14 bits of X. */ ANSWER ('HiY = ' || Y1 || ';') SKIP; /* Extract the upper 14 bits of X. */ ANSWER ('LoX = ' || X1 || ' - HiX;' ) SKIP; /* Extract the lower 14 bits of X. */ ANSWER ('LoY = ' || Y1 || ' - HiY;' ) SKIP; /* Extract the lower 14 bits of Y. */ ANSWER ('Prod = HiX * HiY;') SKIP; /* the product of the high bits. */ ANSWER ('ProdHiXLoY = HiX * LoY;') SKIP; /* Product of the upper bits of X, lower bits of Y. */ ANSWER ('ProdHiYLoX = HiY * LoX;') SKIP; /* Product of the upper bits of Y, lower bits of X. */ ANSWER (LHS || ' = Prod + ProdHiXLoY + ProdHiYLoX') SKIP; /* Sum them all. */ %END MULT; %ACTIVATE MULT NORESCAN; /* The following code segment evaluates the sine of an angle x using the */ /* first 6 terms of the Taylor series: */ /* sine x = x - x**3/3! + x**5/5! - x**7/7! + x**9/9! - x**11/11! */ /* Range: -pi <= x <= pi */ /* Accuracy: 8 decimal places (typically better than 7 significant digits).*/ X = XX; Sum = X; Neg = X < 0; IF Neg THEN X = -X; IF X > 1.5707963 THEN /*sin(x) = sin(180-x). */ X = 3.1415927 - X; /* For small angles, the radian angle is closer to the sine than this approximation. */ /* Therefore, we only execute the following DO group for larger angles. */ IF X > 0.002 THEN DO; Sum = X; Term = X / 6; MULT(Term, Term, X); MULT(Term, Term, X); /* To give x**3/3! */ Sum = Sum - Term; Term = Term / 20; MULT(Term, Term, X); MULT(Term, Term, X); /* To give x**5/5! */ Sum = Sum + Term; Term = Term / 42; MULT(Term, Term, X); MULT(Term, Term, X); /* To give x**7/7! */ Sum = Sum - Term; Term = Term / 72; MULT(Term, Term, X); MULT(Term, Term, X); /* To give x**9/9! */ Sum = Sum + Term; Term = Term / 110; HiX = X; Term = Term * HiX; Term = Term * HiX; /* To give x**11/11! */ Sum = Sum - Term; IF Neg THEN Sum = -Sum; END; RETURN (Sum); END SINE;