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.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: "Jeffrey R. Carter" Newsgroups: comp.lang.ada Subject: Re: Ada.Numerics, Accuracy of trigonometric functions Date: Fri, 7 Oct 2016 09:38:00 -0700 Organization: Also freenews.netfront.net; news.tornevall.net; news.eternal-september.org Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Fri, 7 Oct 2016 16:37:52 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="6df4b173985f7c5c043cea362c370ff7"; logging-data="22501"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/jI55djZ1r6tSsCM+HIqxORcVGS86Bzrs=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 In-Reply-To: Cancel-Lock: sha1:0EVYVKG1j85qgt6T1fOmSjcPkOU= Xref: news.eternal-september.org comp.lang.ada:32037 Date: 2016-10-07T09:38:00-07:00 List-Id: On 10/07/2016 03:13 AM, Markus Schöpflin wrote: > > Size of SHORT_FLOAT = 32 > Maximum relative error of COS = 2.38419E-07 > Angle threshold = 12 > X = 4294967296.00000; COS(X) = 1.00000 <-- WHAT? For GNAT (I have 4.9.3) Ada.Numerics.Generic_Elementary_Functions is defined in terms of Ada.Numerics.Aux, which defines type Double is new Long_Long_Float; Cos, after some checks for zero, converts its argument to Double and calls Aux.Cos. Aux.Cos, for abs X > Pi/4, reduces abs X and then invokes the FPU instruction fcos or fsin on the reduced value, depending on the quadrant. Reduce is defined as procedure Reduce (X : in out Double; Q : out Natural); -- Implements reduction of X by Pi/2. Q is the quadrant of the final -- result in the range 0 .. 3. The absolute value of X is at most Pi. and contains the following K : Double := X * Two_Over_Pi; begin -- For X < 2.0**32, all products below are computed exactly. -- Due to cancellation effects all subtractions are exact as well. -- As no double extended floating-point number has more than 75 -- zeros after the binary point, the result will be the correctly -- rounded result of X - K * (Pi / 2.0). Since X = 2**32, this doesn't apply. It looks to me as if Reduce is converting X to zero, which is then passed to fcos, giving 1. This result is independent of the actual type used to instantiate Generic_Elementary_Functions. The C cosf function probably just passes its argument to fcos. Investigation of what GNAT does for Cos with Cycle is left as an exercise for the reader. -- Jeff Carter "It's symbolic of his struggle against reality." Monty Python's Life of Brian 78