[R] BUG: atan(1i) / 5 = NaN+Infi ?

Richard O'Keefe r@oknz @end|ng |rom gm@||@com
Fri Sep 6 07:24:07 CEST 2024


The thing is that real*complex, complex*real, and complex/real are not
"complex arithmetic"
in the requisite sense.  The complex numbers are a vector space over
the reals, and
complex*real and real*complex are vector*scalar and scalar*vector.
For example, in the Ada programming language, we have
function "*" (Left, Right : Complex) return Complex;
function "*" (Left : Complex;   Right : Real'Base) return Complex;
function "*" (Left : Real'Base; Right : Complex)   return Complex;
showing that Z*R and Z*W involve *different* functions.

It's worth noting that complex*real and real*complex just require two
real multiplications,
no other arithmetic operations, while complex*complex requires four
real multiplications,
an addition, and a subtraction.  So implementing complex*real by
conventing the real
to complex is inefficient (as well as getting the finer points of IEEE
arithmetic wrong).
As for comple division, getting that *right* in floating-point is
fiendishly difficult (there are
lots of algorithms out there and the majority of them have serious
flaws) and woefully costly.
It's not unfair to characterise implementing complex/real by
conversion to complex and
doing complex/complex as a beginner's bungle.

There are good reasons why "double", "_Imaginary double", and "_Complex double"
are distinct types in standard C (as they are in Ada), and the
definition of multiplication
in G.5.1 para 2 is *direct* (not via complex*complex).

Now R has its own way of doing things, and if the judgement of the R
maintainers is
that keeping the "convert to a common type and then operate" model is
more important
than getting good answers, well, it's THEIR language, not mine.  But
let's not pretend
that the answers are *right* in any other sense.

On Fri, 6 Sept 2024 at 11:07, Jeff Newmiller via R-help
<r-help using r-project.org> wrote:
>
> atan(1i) -> 0 + Inf i
> complex(1/5) -> 0.2 + 0i
> atan(1i) -> (0 + Inf i) * (0.2 + 0i)
> -> 0*0.2 + 0*0i + Inf i * 0.2 + Inf i * 0i
> infinity times zero is undefined
> -> 0 + 0i + Inf i + NaN * i^2
> -> 0 + 0i + Inf i - NaN
> -> NaN + Inf i
>
> I am not sure how complex arithmetic could arrive at another answer.
>
> I advise against messing with infinities... use atan2() if you don't actually need complex arithmetic.
>
> On September 5, 2024 3:38:33 PM PDT, Bert Gunter <bgunter.4567 using gmail.com> wrote:
> >> complex(real = 0, imaginary = Inf)
> >[1] 0+Infi
> >
> >> Inf*1i
> >[1] NaN+Infi
> >
> >>> complex(real = 0, imaginary = Inf)/5
> >[1] NaN+Infi
> >
> >See the Note in ?complex for the explanation, I think.  Duncan can correct
> >if I'm wrong.
> >
> >-- Bert
> >
> >On Thu, Sep 5, 2024 at 3:20 PM Leo Mada <leo.mada using syonic.eu> wrote:
> >
> >> Dear Bert,
> >>
> >> These behave like real divisions/multiplications:
> >> complex(re=Inf, im = Inf) * 5
> >> # Inf+Infi
> >> complex(re=-Inf, im = Inf) * 5
> >> # -Inf+Infi
> >>
> >> The real division / multiplication should be faster and also is well
> >> behaved. I was expecting R to do the real division/multiplication on a
> >> complex number. Which R actually does for these very particular cases; but
> >> not when only Im(x) is Inf.
> >>
> >> Sincerely,
> >>
> >> Leonard
> >>
> >> ------------------------------
> >> *From:* Bert Gunter <bgunter.4567 using gmail.com>
> >> *Sent:* Friday, September 6, 2024 1:12 AM
> >> *To:* Duncan Murdoch <murdoch.duncan using gmail.com>
> >> *Cc:* Leo Mada <leo.mada using syonic.eu>; r-help using r-project.org <
> >> r-help using r-project.org>
> >> *Subject:* Re: [R] BUG: atan(1i) / 5 = NaN+Infi ?
> >>
> >> Perhaps
> >>
> >> > Inf*1i
> >> [1] NaN+Infi
> >>
> >> clarifies why it is *not* a bug.
> >> (Boy, did that jog some long dusty math memories :-)  )
> >>
> >> -- Bert
> >>
> >> On Thu, Sep 5, 2024 at 2:48 PM Duncan Murdoch <murdoch.duncan using gmail.com>
> >> wrote:
> >>
> >> On 2024-09-05 4:23 p.m., Leo Mada via R-help wrote:
> >> > Dear R Users,
> >> >
> >> > Is this desired behaviour?
> >> > I presume it's a bug.
> >> >
> >> > atan(1i)
> >> > # 0+Infi
> >> >
> >> > tan(atan(1i))
> >> > # 0+1i
> >> >
> >> > atan(1i) / 5
> >> > # NaN+Infi
> >>
> >> There's no need to involve atan() and tan() in this:
> >>
> >>  > (0+Inf*1i)/5
> >> [1] NaN+Infi
> >>
> >> Why do you think this is a bug?
> >>
> >> Duncan Murdoch
> >>
> >> ______________________________________________
> >> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide
> >> https://www.R-project.org/posting-guide.html
> >> and provide commented, minimal, self-contained, reproducible code.
> >>
> >>
> >
> >       [[alternative HTML version deleted]]
> >
> >______________________________________________
> >R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >https://stat.ethz.ch/mailman/listinfo/r-help
> >PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
> >and provide commented, minimal, self-contained, reproducible code.
>
> --
> Sent from my phone. Please excuse my brevity.
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list