Skip to content

Commit

Permalink
Implement isinf and isfinite for Point and Vec (#215)
Browse files Browse the repository at this point in the history
* Implement `isinf` and `isfinite` for `Point` and `Vec`

Also cleans up the implementation to use the functions directly and avoid anonymous functions.

* Add tests

* Fix `isfinite`
  • Loading branch information
asinghvi17 authored Apr 8, 2024
1 parent 4885d07 commit bc1ce58
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/fixed_arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ const Mat = SMatrix
const VecTypes{N,T} = Union{StaticVector{N,T},NTuple{N,T}}
const Vecf{N} = Vec{N,Float32}
const Pointf{N} = Point{N,Float32}
Base.isnan(p::Union{AbstractPoint,Vec}) = any(x -> isnan(x), p)
Base.isnan(p::Union{AbstractPoint,Vec}) = any(isnan, p)
Base.isinf(p::Union{AbstractPoint,Vec}) = any(isinf, p)
Base.isfinite(p::Union{AbstractPoint,Vec}) = all(isfinite, p)

for i in 1:4
for T in [:Point, :Vec]
Expand Down
17 changes: 17 additions & 0 deletions test/fixed_arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,20 @@ end
@test [T(0, 0), T(5, 0)] == x .- T(2, 3)
end
end

@testset "finite, nan, inf tests" begin
for T in (Vec, Point)
@testset "$T" begin
nan_point = T(Float64.((1.0, 2.0, 3.0, NaN)))
inf_point = T(Float64.((1.0, 2.0, Inf, 4.0)))
@test isinf(inf_point)
@test !isinf(nan_point)
@test isnan(nan_point)
@test !isnan(inf_point)
@test !isfinite(nan_point)
@test !isfinite(inf_point)
@test !isfinite(nan_point + inf_point)
@test isfinite(T(1, 2, 3))
end
end
end

0 comments on commit bc1ce58

Please sign in to comment.