delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2003/08/29/17:42:51

From: Kbwms AT aol DOT com
Message-ID: <18.348fb69c.2c8122ce@aol.com>
Date: Fri, 29 Aug 2003 17:42:38 EDT
Subject: Re: Arithmetic Exceptions in C99
To: djgpp-workers AT delorie DOT com
MIME-Version: 1.0
X-Mailer: 8.0 for Windows sub 6015
Reply-To: djgpp-workers AT delorie DOT com

--part1_18.348fb69c.2c8122ce_boundary
Content-Type: text/plain; charset="US-ASCII"
Content-Transfer-Encoding: 7bit

In a message dated 8/29/2003 5:10:01 PM Eastern Standard Time, 
ams AT ludd DOT luth DOT se writes:

> >In this case, the affected math function sets errno to ERANGE and 
> (possibly) 
> >raises an appropriate exception.  In no case should a math function issue 
> >SIGFPE.
> 
> What signal should it generate if not SIGFPE?
> 
> For me raise an exception == generate a signal. Both Eli and you
> seems to say that raising an exception is something else than
> generating a signal.
> 
> Can somebody explain this to me?
> 

You mentioned acos(2) somewhere in this thread.  Appended to this email as a 
postscript is acosl.c.  In the first few steps, if the absolute value of the 
argument exceeds 1, the function takes actions prescribed in C99, pages 229 
(7.12.4.1) and 446 (F.9.11).  The actions include raising the invalid floating 
point exception.


KB Williams

PS

/* ------- */
/* acosl.c */
/* ------- */
#include <errno.h>
#include <math.h>
#include <fdlibml.h>
#include <fenv.h>
/*
Written for DJGPP/GCC by KB Williams, kbwms AT aol DOT com,
December 2001
*/
# ifdef __STDC__
long double
acosl(long double Arg)
# else
long double
acosl(Arg)
long double Arg;
# endif
//      1. If |Arg| > 1
//         Set errno to EDOM, set Retval to NaN,
//         raise invalid exception
//      2. If |Arg| == 1
//         Retval = (Arg > 0) ? 0 : Pi
//      3. If Arg == 0
//         Set Retval to Pi/2
//      4. Calculate acos(Arg) for
//         x in (-1, -.5), or (+.5, +1) or [-.5, +.5]
{
    long double AbsArg, Retval;

    AbsArg = fabsl(Arg);

    if (AbsArg >= 1)                    // Steps 1 & 2
    {
        if (AbsArg > 1)
        {
            errno = EDOM;
            Retval = NANL;              // Return NaN
            feraiseexcept(FE_INVALID);  // Raise invalid exception
        }
        else                            // Arg == +- 1
        {
            if (Arg > 0)                // acosl(1) = 0
                Retval = 0;
            else                        // acosl(-1) = pi
                Retval = PIL;
        }
    }
    else if (Arg == 0)                  // Step 3
    {
        Retval = PIO2L;
    }
    else if (Arg < -0.5L)               // Step 4
    {
        Retval = (PIL - 2.0L * asinl(sqrtl(0.5L * (1.0L + Arg))));
    }
    else if (Arg > 0.5L)
    {
        Retval = (2.0L * asinl(sqrtl(0.5L * (1.0L - Arg))));
    }
    else
    {
        Retval = (PIO2L - asinl(Arg));
    }
    return Retval;
}

--part1_18.348fb69c.2c8122ce_boundary
Content-Type: text/html; charset="US-ASCII"
Content-Transfer-Encoding: quoted-printable

<HTML><FONT FACE=3Darial,helvetica><FONT  SIZE=3D3 FAMILY=3D"SERIF" FACE=3D"=
Georgia" LANG=3D"0">In a message dated 8/29/2003 5:10:01 PM Eastern Standard=
 Time, ams AT ludd DOT luth DOT se writes:<BR>
<BR>
<BLOCKQUOTE TYPE=3DCITE style=3D"BORDER-LEFT: #0000ff 2px solid; MARGIN-LEFT=
: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px"></FONT><FONT  COLOR=3D"#000000"=
 style=3D"BACKGROUND-COLOR: #ffffff" SIZE=3D2 FAMILY=3D"SANSSERIF" FACE=3D"A=
rial" LANG=3D"0">&gt;In this case, the affected math function sets errno to=20=
ERANGE and (possibly) <BR>
&gt;raises an appropriate exception.&nbsp; In no case should a math function=
 issue <BR>
&gt;SIGFPE.<BR>
<BR>
What signal should it generate if not SIGFPE?<BR>
<BR>
For me raise an exception =3D=3D generate a signal. Both Eli and you<BR>
seems to say that raising an exception is something else than<BR>
generating a signal.<BR>
<BR>
Can somebody explain this to me?<BR>
</BLOCKQUOTE><BR>
</FONT><FONT  COLOR=3D"#000000" style=3D"BACKGROUND-COLOR: #ffffff" SIZE=3D3=
 FAMILY=3D"SERIF" FACE=3D"Georgia" LANG=3D"0"><BR>
You mentioned acos(2) somewhere in this thread.&nbsp; Appended to this email=
 as a postscript is acosl.c.&nbsp; In the first few steps, if the absolute v=
alue of the argument exceeds 1, the function takes actions prescribed in C99=
, pages 229 (7.12.4.1) and 446 (F.9.11).&nbsp; The actions include raising t=
he invalid floating point exception.<BR>
<BR>
<BR>
KB Williams<BR>
<BR>
PS<BR>
<BR>
/* ------- */<BR>
/* acosl.c */<BR>
/* ------- */<BR>
#include &lt;errno.h&gt;<BR>
#include &lt;math.h&gt;<BR>
#include &lt;fdlibml.h&gt;<BR>
#include &lt;fenv.h&gt;<BR>
/*<BR>
Written for DJGPP/GCC by KB Williams, kbwms AT aol DOT com,<BR>
December 2001<BR>
*/<BR>
# ifdef __STDC__<BR>
long double<BR>
acosl(long double Arg)<BR>
# else<BR>
long double<BR>
acosl(Arg)<BR>
long double Arg;<BR>
# endif<BR>
//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1. If |Arg| &gt; 1<BR>
//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set errno to EDOM, set Re=
tval to NaN,<BR>
//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; raise invalid exception<B=
R>
//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2. If |Arg| =3D=3D 1<BR>
//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Retval =3D (Arg &gt; 0) ?=
 0 : Pi<BR>
//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3. If Arg =3D=3D 0<BR>
//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set Retval to Pi/2<BR>
//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4. Calculate acos(Arg) for<BR>
//&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x in (-1, -.5), or (+.5,=20=
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nbsp; }<BR>
&nbsp;&nbsp;&nbsp; else if (Arg &lt; -0.5L)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Step 4<BR>
&nbsp;&nbsp;&nbsp; {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Retval =3D (PIL - 2.0L * asinl(sq=
rtl(0.5L * (1.0L + Arg))));<BR>
&nbsp;&nbsp;&nbsp; }<BR>
&nbsp;&nbsp;&nbsp; else if (Arg &gt; 0.5L)<BR>
&nbsp;&nbsp;&nbsp; {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Retval =3D (2.0L * asinl(sqrtl(0.=
5L * (1.0L - Arg))));<BR>
&nbsp;&nbsp;&nbsp; }<BR>
&nbsp;&nbsp;&nbsp; else<BR>
&nbsp;&nbsp;&nbsp; {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Retval =3D (PIO2L - asinl(Arg));<=
BR>
&nbsp;&nbsp;&nbsp; }<BR>
&nbsp;&nbsp;&nbsp; return Retval;<BR>
}<BR>
</FONT></HTML>
--part1_18.348fb69c.2c8122ce_boundary--

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019