public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "kargl at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug fortran/107753] gfortran returns NaN in complex divisions (x+x*I)/(x+x*I) and (x+x*I)/(x-x*I)
Date: Wed, 07 Dec 2022 21:50:14 +0000	[thread overview]
Message-ID: <bug-107753-4-4QBykbplCW@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-107753-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107753

--- Comment #14 from kargl at gcc dot gnu.org ---
(In reply to anlauf from comment #13)
> (In reply to Steve Kargl from comment #12)
> > The optimization level is irrelevant.  gfortran unilaterally
> > uses -fcx-fortran-rules, and there is no way to disable this
> > option to user the slower, but stricter, evaluation.  One
> > will always get complex division computed by
> > 
> > a+ib   a + b(d/c)     b - a(d/c) 
> > ---- = ---------- + i ------------  |c| > |d|
> > c+id   c + d(d/c)     c + d(d/c)
> > 
> > and similar for |d| > |c|.
> > 
> > There are a few problems with this. d/c can trigger an invalid underflow
> > exception.  If d == c, you then have numerators of a + b and b - a, you
> > can get a invalid overflow for a = huge() and b > 1e291_8.
> 
> I am wondering how slow an algorithm would be that scales numerator
> and denominator by respective factors that are powers of 2, e.g.
> 
> e_num = 2. ** -max (exponent (a), exponent (b))
> e_den = 2. ** -max (exponent (c), exponent (d))
> 
> The modulus of scaled values would be <= 1, even for any of a,... being
> huge().
> Of course this does not address underflows that could occur during scaling,
> or denormalized numbers, which are numerically irrelevant for the result.
> 
> Is there anything else wrong with this approach?

Comment #10 contains a simple timing measurement in from my Intel Core2 Duo
based system.  gfortran with its current method (ie., -fcx-fortran-rules) takes
44.5 clock ticks for a complex division.  If I sidestep the option and force it
to use the C language method of evaluation, it takes 62 clock ticks.  I haven't
looked at what algorithm C uses, but I suspect its along the lines you suggest.
 The question is likely do we break backwards compatibility and remove
-fcx-fortran-rules or change when/how -fcx-fortran-rules applies (e.g., add it
to -ffast-math?)

  parent reply	other threads:[~2022-12-07 21:50 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-18 19:32 [Bug fortran/107753] New: " weslley.pereira at ucdenver dot edu
2022-11-18 19:50 ` [Bug fortran/107753] " anlauf at gcc dot gnu.org
2022-11-18 20:50 ` kargl at gcc dot gnu.org
2022-11-18 21:26 ` anlauf at gcc dot gnu.org
2022-11-18 22:05 ` kargl at gcc dot gnu.org
2022-11-18 23:24 ` sgk at troutmask dot apl.washington.edu
2022-11-18 23:32 ` sgk at troutmask dot apl.washington.edu
2022-11-18 23:45 ` weslley.pereira at ucdenver dot edu
2022-11-18 23:47 ` weslley.pereira at ucdenver dot edu
2022-11-19  0:25 ` sgk at troutmask dot apl.washington.edu
2022-11-19 19:11 ` kargl at gcc dot gnu.org
2022-11-19 20:14 ` anlauf at gcc dot gnu.org
2022-11-20  0:54 ` sgk at troutmask dot apl.washington.edu
2022-12-07 21:16 ` anlauf at gcc dot gnu.org
2022-12-07 21:50 ` kargl at gcc dot gnu.org [this message]
2022-12-07 22:31 ` anlauf at gcc dot gnu.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-107753-4-4QBykbplCW@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.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).