[R] BUG: atan(1i) / 5 = NaN+Infi ?
Duncan Murdoch
murdoch@dunc@n @end|ng |rom gm@||@com
Fri Sep 6 01:40:35 CEST 2024
On 2024-09-05 6:12 p.m., Leo Mada wrote:
> Dear Duncan,
>
> Here is also the missing information:
> R version 4.4.1 (2024-06-14 ucrt)
> Platform: x86_64-w64-mingw32/x64
> Running under: Windows 10 x64 (build 19045)
>
> Regarding the results:
> atan(1i)
> # 0+Infi
> Re(atan(1i))
> # 0
> Im(atan(1i))
> # Inf
>
> 0 + Inf i is a valid complex number:
> tan(atan(1i))
> # 0+1i
>
> Inf / 5
> # Inf
>
> Note: atan(1i) / 5 should have generated 0 + Inf * 1i; even the explicit
> complex number fails:
> complex(re=0, im = Inf) / 5
> # NaN+Infi
> complex(re=Inf, im = Inf) / 5
> # Inf+Infi
>
> I presume that R tries to do the complex division, although the real
> division is well defined.
I imagine that what happens is that when one operand is complex, both
are coerced to complex and the operation is carried out. I had assumed
this was documented in ?complex, but I don't see it there. Maybe it
should be.
If you want z/5 to be carried out using the correct mathematical
approach, you'll probably have to define it yourself. For example,
CxByReal <- function(num, denom) {
if (is.complex(denom)) stop("this is for a real denominator!")
complex(real = Re(num)/denom, imaginary = Im(num)/denom)
}
CxByReal(complex(real=0, imaginary=Inf), 5)
# [1] 0+Infi
Duncan Murdoch
>
> Sincerely,
>
> Leonard
>
> ------------------------------------------------------------------------
> *From:* Duncan Murdoch <murdoch.duncan using gmail.com>
> *Sent:* Friday, September 6, 2024 12:40 AM
> *To:* 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 ?
> 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
>
More information about the R-help
mailing list