public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "sebastian-glibc at sipsolutions dot net" <sourceware-bugzilla@sourceware.org>
To: glibc-bugs@sourceware.org
Subject: [Bug math/27875] New: Complex power `0^1` gives good result but sets division error flag
Date: Mon, 17 May 2021 02:17:02 +0000	[thread overview]
Message-ID: <bug-27875-131@http.sourceware.org/bugzilla/> (raw)

https://sourceware.org/bugzilla/show_bug.cgi?id=27875

            Bug ID: 27875
           Summary: Complex power `0^1` gives good result but sets
                    division error flag
           Product: glibc
           Version: 2.31
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: math
          Assignee: unassigned at sourceware dot org
          Reporter: sebastian-glibc at sipsolutions dot net
  Target Milestone: ---

We had a request in NumPy to return a non NaN value for `0j^(1 + Xj)`. 
Exploring that, I think that glibc gives perfectly good values, including the
correct sign of the 0.

However, the divide by zero floating error flag is set.  Since the return value
is finite, I would not expect it to get set?

This is the result of a test code run (note the first two examples):

```
Calculating for (0 + 0j)^(1 + 1j):
    Result is: 0+-0j
    Divide by zero flag set.

Calculating for (0 + 0j)^(1 + -1j):
    Result is: 0+0j
    Divide by zero flag set.

Calculating for (0 + 0j)^(-1 + 1j):
    Result is: inf+-nanj
    Divide by zero flag set.
    Invalid flag set.

Calculating for (0 + 0j)^(-1 + -1j):
    Result is: inf+-nanj
    Divide by zero flag set.
    Invalid flag set.
```

Generated with the following program:

```
#include <stdio.h>
#include <fenv.h>
#include <complex.h>


static void
print_cpow(complex mantissa, complex exponent)
{
   printf("\nCalculating for (%g + %gj)^(%g + %gj):\n",
          creal(mantissa), cimag(mantissa), creal(exponent), cimag(exponent));
   fetestexcept(FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW | FE_INVALID);

   complex res = cpow(mantissa, exponent);

   int except = fetestexcept(FE_DIVBYZERO | FE_OVERFLOW | FE_UNDERFLOW |
FE_INVALID);

   printf("    Result is: %g+%gj\n", creal(res), cimag(res));
   if (except & FE_DIVBYZERO) {
        printf("    Divide by zero flag set.\n");
   }
   if (except & FE_OVERFLOW) {
        printf("    Overflow flag set.\n");
   }
   if (except & FE_UNDERFLOW) {
       printf("    Underflow flag set.\n");
   }
   if (except & FE_INVALID) {
       printf("    Invalid flag set.\n");
   }
}


int
main(void)
{
    complex exponents[] = {1.0+1.0*I, 1.0-1.0*I, -1.0+1.0*I, -1.0-1.0*I};

    for(int i = 0; i < 4; i++){
        print_cpow(-0, exponents[i]);
    }
    return 0;
}
```

And for completeness:

* gcc (Debian 10.2.1-6) 10.2.1 20210110
* ldd (Debian GLIBC 2.31-11) 2.31
* Compiled without any optimization flags: gcc cexp.c -lm

-- 
You are receiving this mail because:
You are on the CC list for the bug.

             reply	other threads:[~2021-05-17  2:17 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-17  2:17 sebastian-glibc at sipsolutions dot net [this message]
2022-09-15 14:24 ` [Bug math/27875] " schwab@linux-m68k.org
2022-09-15 14:29 ` schwab@linux-m68k.org

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=bug-27875-131@http.sourceware.org/bugzilla/ \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=glibc-bugs@sourceware.org \
    /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.
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 read-only IMAP folder(s) and NNTP newsgroup(s).