Trigonometric functions at infinity

238 views
Skip to first unread message

isa...@gmail.com

unread,
Nov 4, 2014, 12:22:13 PM11/4/14
to julia...@googlegroups.com
I have recently become aware of Julia and have been impressed with its ease of use and speed.  While I was converting my previous code to Julia, I noticed that trigonometric functions at infinity yield DomainError and abort the program. Try sin(Inf), sin(-Inf), cos(Inf), tan(Inf), etc.  I checked the behavior of Numpy and it returns a NaN and a warning of invalid value to the function. I remember Matlab was yielding a NaN too. But they both wouldn't abort the program.

Returning a NaN instead of aborting the program might be useful when the following computations don't depend on only this result of NaN.  For example, consider finding the smallest element of x =cos( [1.0, 2.0, Inf]) which would have given the number I am interested in if cos(Inf) gives a NaN.  Here I cos(2.0) < NaN would be false nevertheless findmin cleverly finds the correct answer 2.0 ( so does Numpy). Note that Inf is usually a result of an intermediate step of an algorithm.

If the following computations involve NaN and yield compilation error than checking elements of x being not NaN is necessary. But since it does occur rarely, avoiding this check might be useful for speed.

What would be your thoughts?  Thanks.

John Myles White

unread,
Nov 4, 2014, 12:58:59 PM11/4/14
to julia...@googlegroups.com
My personal preference is for code to never raise warnings if you might ever use it in a system that has more than 10 lines of code. So I'm personally a believer in either returning NaN without a warning (which seems a little risky) or maintaining the current behavior, which seems wisest to me.

-- John

Milan Bouchet-Valat

unread,
Nov 4, 2014, 1:58:40 PM11/4/14
to julia...@googlegroups.com
Le mardi 04 novembre 2014 à 09:22 -0800, isa...@gmail.com a écrit :
> I have recently become aware of Julia and have been impressed with its
> ease of use and speed. While I was converting my previous code to
> Julia, I noticed that trigonometric functions at infinity yield
> DomainError and abort the program. Try sin(Inf), sin(-Inf), cos(Inf),
> tan(Inf), etc. I checked the behavior of Numpy and it returns a NaN
> and a warning of invalid value to the function. I remember Matlab was
> yielding a NaN too. But they both wouldn't abort the program.
>
> Returning a NaN instead of aborting the program might be useful when
> the following computations don't depend on only this result of NaN.
> For example, consider finding the smallest element of x =cos( [1.0,
> 2.0, Inf]) which would have given the number I am interested in if
> cos(Inf) gives a NaN. Here I cos(2.0) < NaN would be false
> nevertheless findmin cleverly finds the correct answer 2.0 ( so does
> Numpy). Note that Inf is usually a result of an intermediate step of
> an algorithm.
You'll probably be interested in this discussion:
https://github.com/JuliaLang/julia/issues/7866
Message has been deleted

isa...@gmail.com

unread,
Nov 5, 2014, 8:08:59 AM11/5/14
to julia...@googlegroups.com
In order to avoid exiting the program, I am checking for Inf and setting the result to Nan manually for each trigonometric function.  I agree with John's comments and hope that something like this would become default behavior.
@Milan: Thanks for the link, it was a good one.

Simon Byrne

unread,
Nov 5, 2014, 5:11:01 PM11/5/14
to julia...@googlegroups.com
Alternatively, you could just redefine sin without the nan_dom_err call:

Something like the following should work:

import Base.sin
sin(x::Float64) = ccall((:sin,Base.Math.libm), Float64, (Float64,), x)

isa...@gmail.com

unread,
Nov 6, 2014, 7:46:01 AM11/6/14
to julia...@googlegroups.com
Nice solution! Thanks.

Reply all
Reply to author
Forward
0 new messages