-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add functions to return inf and nan of the specified type #37569
Conversation
The correct way to do this is to use
which accomplishes the same thing. |
That looks a bit awkward and inconsistent next to
What is the rationale for |
It's not about it looking awkward. Should we also add |
I am proposing this because Infinity and not a number are also special values - we even have ways to test for them without using the standard equality check (e.g.
Now, I decided not to do that in this code because I didn't see a large speed boost by specializing them completely and the |
You don't see a speed boost, because there isn't one, as it stands it is redundant with |
We used to have |
Because julia> using StaticArrays
julia> zero(SVector{3,Int})
3-element SArray{Tuple{3},Int64,1,3} with indices SOneTo(3):
0
0
0
julia> one(SMatrix{3,3,Float64,9})
3×3 SArray{Tuple{3,3},Float64,2,9} with indices SOneTo(3)×SOneTo(3):
1.0 0.0 0.0
0.0 1.0 0.0
0.0 0.0 1.0 or even julia> one(String)
"" (since In contrast, |
This PR adds two new functions
inf()
andnan()
that take a type and then will return the literal infinity or not a number of the specified type. These are useful when writing generic floating-point code, since the use ofNaN
orInf
will force the user ofFloat64
. This can be seen in the following function:Which will give the following output:
Note that the return value is then automatically
Float64
because the assignment toNaN
in the if statement makesx
intoFloat64
instead of keeping it asFloat32
like it was passed in as (sinceNaN
is simply an alias for the 64-bit NaN). While in this case you could simply force a return type ofT
on the function, if other processing is done before the return, then it is using theFloat64
type. When prototyping algorithms, it is useful to test with different precisions to see how they behave, so this behavior makes that difficult.This PR adds the functions
inf()
andnan()
, so the sample function then can be:Which will give the following output:
This now preserves the type of x during the assignment of
NaN
, so the entire contents oftestfunc()
can usex
as any arbitrary floating-point type and not convert it.