Skip to content

Commit cd22166

Browse files
committed
Merge pull request #11 from mlubin/ml/sqrt
implement sqrt
2 parents 969151c + 27d61b1 commit cd22166

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

src/NaNMath.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ for f in (:sin, :cos, :tan, :asin, :acos, :acosh, :atanh, :log, :log2, :log10,
1111
end
1212
end
1313

14+
# Would be more efficient to remove the domain check in Base.sqrt(),
15+
# but this doesn't seem easy to do.
16+
sqrt(x::Real) = x < 0.0 ? NaN : Base.sqrt(x)
17+
1418
# Don't override built-in ^ operator
1519
pow(x::Float64, y::Float64) = ccall((:pow,Base.Math.libm), Float64, (Float64,Float64), x, y)
1620
pow(x::Float32, y::Float32) = ccall((:powf,Base.Math.libm), Float32, (Float32,Float32), x, y)

test/runtests.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ using Base.Test
44
@test isnan(NaNMath.log(-10))
55
@test isnan(NaNMath.log1p(-100))
66
@test isnan(NaNMath.pow(-1.5,2.3))
7+
@test isnan(NaNMath.sqrt(-5))
8+
@test NaNMath.sqrt(5) == Base.sqrt(5)
79
@test NaNMath.sum([1., 2., NaN]) == 3.0
810
@test isnan(NaNMath.sum([NaN, NaN]))
911
@test NaNMath.sum(Float64[]) == 0.0

0 commit comments

Comments
 (0)