From: Rich Felker <dalias@libc.org>
To: Paul Zimmermann <Paul.Zimmermann@inria.fr>
Cc: musl@lists.openwall.com, maxence.ponsardin@inria.fr
Subject: Re: sqrt does not emit errno=EDOM
Date: Thu, 3 Jul 2025 16:05:17 -0400 [thread overview]
Message-ID: <20250703200516.GK1827@brightrain.aerifal.cx> (raw)
In-Reply-To: <p9u01pqx90s3.fsf@araignee.loria.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
next prev parent reply other threads:[~2025-07-03 20:05 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
2025-07-04 9:00 ` Paul Zimmermann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250703200516.GK1827@brightrain.aerifal.cx \
--to=dalias@libc.org \
--cc=Paul.Zimmermann@inria.fr \
--cc=maxence.ponsardin@inria.fr \
--cc=musl@lists.openwall.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).