From: rav@goanna.cs.rmit.edu.au (robin)
Subject: Re: fixed point vs floating point
Date: 1997/12/03
Date: 1997-12-03T00:00:00+00:00 [thread overview]
Message-ID: <66271b$r67$1@goanna.cs.rmit.edu.au> (raw)
In-Reply-To: mheaney-ya023680002211970739180001@news.ni.net
>Matthew Heaney <mheaney@ni.net> 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;
next prev parent reply other threads:[~1997-12-03 0:00 UTC|newest]
Thread overview: 64+ messages / expand[flat|nested] mbox.gz Atom feed top
1997-11-22 0:00 fixed point vs floating point Matthew Heaney
1997-11-22 0:00 ` Tucker Taft
1997-11-22 0:00 ` Robert Dewar
1997-11-22 0:00 ` Matthew Heaney
1997-11-23 0:00 ` Geert Bosch
1997-11-23 0:00 ` Matthew Heaney
1997-11-23 0:00 ` Robert Dewar
1997-11-24 0:00 ` Herman Rubin
1997-11-24 0:00 ` Robert Dewar
1997-11-25 0:00 ` Joe Gwinn
1997-11-25 0:00 ` Robert Dewar
1997-11-25 0:00 ` Joe Gwinn
1997-11-25 0:00 ` Robert Dewar
1997-11-26 0:00 ` Joe Gwinn
1997-11-26 0:00 ` Robert Dewar
1997-12-01 0:00 ` Joe Gwinn
1997-12-01 0:00 ` Robert Dewar
1997-12-01 0:00 ` Joe Gwinn
1997-12-03 0:00 ` robin
1997-11-25 0:00 ` Matthew Heaney
1997-11-26 0:00 ` William A Whitaker
1997-11-24 0:00 ` Geert Bosch
1997-11-23 0:00 ` Tom Moran
1997-11-25 0:00 ` John A. Limpert
1997-11-25 0:00 ` Robert Dewar
1997-11-25 0:00 ` Robert Dewar
1997-11-24 0:00 ` Vince Del Vecchio
1997-11-24 0:00 ` Vince Del Vecchio
1997-12-03 0:00 ` robin [this message]
-- strict thread matches above, loose matches on Subject: below --
2011-09-29 10:25 RasikaSrinivasan@gmail.com
2011-09-29 10:49 ` AdaMagica
2011-09-29 13:38 ` Martin
2011-09-30 10:17 ` Stephen Leake
2011-09-30 16:25 ` tmoran
2011-09-30 16:52 ` Dmitry A. Kazakov
2011-10-01 11:09 ` Stephen Leake
2011-09-30 19:26 ` tmoran
2011-09-30 22:31 ` tmoran
2011-10-01 13:37 ` RasikaSrinivasan@gmail.com
2011-10-02 14:19 ` Stephen Leake
1997-12-02 0:00 Robert Dewar
1997-12-02 0:00 ` Joe Gwinn
1997-12-02 0:00 ` Robert Dewar
1997-12-02 0:00 ` Matthew Heaney
1997-12-03 0:00 ` Robert Dewar
1997-12-03 0:00 ` Shmuel (Seymour J.) Metz
1997-12-03 0:00 ` Robert Dewar
1997-12-03 0:00 ` Matthew Heaney
1997-12-04 0:00 ` Shmuel (Seymour J.) Metz
1997-12-04 0:00 ` Robert Dewar
1997-12-03 0:00 ` Robert Dewar
1997-12-03 0:00 ` robin
1997-12-03 0:00 ` Robert Dewar
1997-12-02 0:00 ` Ken Garlington
1997-12-03 0:00 ` Joe Gwinn
1997-12-04 0:00 ` Robert Dewar
1997-12-04 0:00 ` Shmuel (Seymour J.) Metz
1997-12-03 0:00 ` robin
1997-11-28 0:00 tmoran
1997-11-28 0:00 ` Robert Dewar
1997-11-27 0:00 tmoran
1997-11-27 0:00 ` Robert Dewar
1997-11-29 0:00 ` Tarjei T. Jensen
[not found] <9711221603.AA03295@nile.gnat.com>
1997-11-22 0:00 ` Ken Garlington
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox