Skip to content

Commit de31094

Browse files
oschulzdevmotion
andauthored
Restrict domain of square (#9)
Co-authored-by: David Widmann <[email protected]>
1 parent 03a956d commit de31094

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/functions.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# This file is a part of InverseFunctions.jl, licensed under the MIT License (MIT).
22

33
"""
4-
square(x)
4+
square(x::Real)
55
66
Inverse of `sqrt(x)` for non-negative `x`.
77
"""
88
square(x) = x^2
9+
function square(x::Real)
10+
x < zero(x) && throw(DomainError(x, "`square` is defined as the inverse of `sqrt` and can only be evaluated for non-negative values"))
11+
return x^2
12+
end

test/test_functions.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ using InverseFunctions
55

66

77
@testset "square" begin
8-
for x in (-0.72, 0.73, randn(3, 3))
8+
for x in (0.0, 0.73)
99
@test InverseFunctions.square(x) x * x
1010
end
11+
12+
@test_throws DomainError InverseFunctions.square(-1)
1113
end

0 commit comments

Comments
 (0)