mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] sqrt does not emit errno=EDOM
@ 2025-07-03  7:44 Paul Zimmermann
  2025-07-03  8:00 ` Damian McGuckin
  2025-07-03 20:05 ` Rich Felker
  0 siblings, 2 replies; 4+ messages in thread
From: Paul Zimmermann @ 2025-07-03  7:44 UTC (permalink / raw)
  To: musl; +Cc: maxence.ponsardin

       Hi,

it seems musl does not set errno=EDOM for sqrt(x) when x is negative.
Here on cfarm27 (Alpine Linux):

$ cat /tmp/e.c
#include <stdio.h>
#include <math.h>
#include <errno.h>
 
int
main()
{
  errno = 0;
  float x = -1.0f;
  float y = sqrtf (x);
  printf ("y=%a errno=%d\n", y, errno);
}

$ gcc /tmp/e.c -lm
$ ./a.out 
y=-nan errno=0

Is this a known issue?

Best regards,
Paul


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: sqrt does not emit errno=EDOM
  2025-07-03  7:44 [musl] sqrt does not emit errno=EDOM Paul Zimmermann
@ 2025-07-03  8:00 ` Damian McGuckin
  2025-07-03 20:05 ` Rich Felker
  1 sibling, 0 replies; 4+ messages in thread
From: Damian McGuckin @ 2025-07-03  8:00 UTC (permalink / raw)
  To: musl


Hi Paul,

On Thu, 3 Jul 2025, Paul Zimmermann wrote:

> it seems musl does not set errno=EDOM for sqrt(x) when x is negative.
> Here on cfarm27 (Alpine Linux):
>
> $ cat /tmp/e.c
> #include <stdio.h>
> #include <math.h>
> #include <errno.h>
>
> int
> main()
> {
>  errno = 0;
>  float x = -1.0f;
>  float y = sqrtf (x);
>  printf ("y=%a errno=%d\n", y, errno);
> }
>
> $ gcc /tmp/e.c -lm
> $ ./a.out
> y=-nan errno=0
>
> Is this a known issue?

I think it is a policy issue:

 	Errors are reported by raising floating-point status flags ie.
 	math_errhandling is set to MATH_ERREXCEPT on all platforms (errno
 	is not set by math functions and on platforms without IEEE 754
 	exception semantics there is no error reporting).

Later - Damian


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: sqrt does not emit errno=EDOM
  2025-07-03  7:44 [musl] sqrt does not emit errno=EDOM Paul Zimmermann
  2025-07-03  8:00 ` Damian McGuckin
@ 2025-07-03 20:05 ` Rich Felker
  2025-07-04  9:00   ` Paul Zimmermann
  1 sibling, 1 reply; 4+ messages in thread
From: Rich Felker @ 2025-07-03 20:05 UTC (permalink / raw)
  To: Paul Zimmermann; +Cc: musl, maxence.ponsardin

On Thu, Jul 03, 2025 at 09:44:12AM +0200, Paul Zimmermann wrote:
>        Hi,
> 
> it seems musl does not set errno=EDOM for sqrt(x) when x is negative.
> Here on cfarm27 (Alpine Linux):
> 
> $ cat /tmp/e.c
> #include <stdio.h>
> #include <math.h>
> #include <errno.h>
>  
> int
> main()
> {
>   errno = 0;
>   float x = -1.0f;
>   float y = sqrtf (x);
>   printf ("y=%a errno=%d\n", y, errno);
> }
> 
> $ gcc /tmp/e.c -lm
> $ ./a.out 
> y=-nan errno=0
> 
> Is this a known issue?

It's intentional that none of musl's math library sets errno.
Implementations have the option to do either or both of setting errno
or raising fenv exception flags as long as this is reflected in the
value of math_errhandling.

Generally, the modern view seems to be that setting errno is bad
practice. It makes it so that the math functions can never be treated
as pure functions by the compiler (note: fenv also breaks purity but
the application gets to signal whether it wants FENV_ACCESS or not,
and if not, they're pure).

Note that on soft float archs without fenv, omitting errno is actually
nonconforming. It was originally my hope that we'd eventually get soft
fenv working on these, but they seem to be of waning relevance these
days anyway. I don't see adding errno as an appropriate fix for this.
Applications generally don't want it, and any such effort to add it
would be better spent on workout out a way to do soft fenv.

Rich


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: sqrt does not emit errno=EDOM
  2025-07-03 20:05 ` Rich Felker
@ 2025-07-04  9:00   ` Paul Zimmermann
  0 siblings, 0 replies; 4+ messages in thread
From: Paul Zimmermann @ 2025-07-04  9:00 UTC (permalink / raw)
  To: Rich Felker; +Cc: musl, maxence.ponsardin

thank you Rich for clarifying the Musl position with respect to errno.

Paul

> Date: Thu, 3 Jul 2025 16:05:17 -0400
> From: Rich Felker <dalias@libc.org>
> Cc: musl@lists.openwall.com, maxence.ponsardin@inria.fr
> 
> On Thu, Jul 03, 2025 at 09:44:12AM +0200, Paul Zimmermann wrote:
> >        Hi,
> > 
> > it seems musl does not set errno=EDOM for sqrt(x) when x is negative.
> > Here on cfarm27 (Alpine Linux):
> > 
> > $ cat /tmp/e.c
> > #include <stdio.h>
> > #include <math.h>
> > #include <errno.h>
> >  
> > int
> > main()
> > {
> >   errno = 0;
> >   float x = -1.0f;
> >   float y = sqrtf (x);
> >   printf ("y=%a errno=%d\n", y, errno);
> > }
> > 
> > $ gcc /tmp/e.c -lm
> > $ ./a.out 
> > y=-nan errno=0
> > 
> > Is this a known issue?
> 
> It's intentional that none of musl's math library sets errno.
> Implementations have the option to do either or both of setting errno
> or raising fenv exception flags as long as this is reflected in the
> value of math_errhandling.
> 
> Generally, the modern view seems to be that setting errno is bad
> practice. It makes it so that the math functions can never be treated
> as pure functions by the compiler (note: fenv also breaks purity but
> the application gets to signal whether it wants FENV_ACCESS or not,
> and if not, they're pure).
> 
> Note that on soft float archs without fenv, omitting errno is actually
> nonconforming. It was originally my hope that we'd eventually get soft
> fenv working on these, but they seem to be of waning relevance these
> days anyway. I don't see adding errno as an appropriate fix for this.
> Applications generally don't want it, and any such effort to add it
> would be better spent on workout out a way to do soft fenv.
> 
> Rich
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-07-04  9:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-07-03  7:44 [musl] sqrt does not emit errno=EDOM Paul Zimmermann
2025-07-03  8:00 ` Damian McGuckin
2025-07-03 20:05 ` Rich Felker
2025-07-04  9:00   ` Paul Zimmermann

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).