Skip to content
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 ispositive, etc. #53677

Open
wants to merge 45 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
d0323ac
add ispositive, etc.
putianyi889 Mar 9, 2024
9b47c02
Update number.jl
putianyi889 Mar 9, 2024
b774f97
specialize Integer and IEEEFloat
putianyi889 Mar 17, 2024
5f76224
add docs
putianyi889 Mar 17, 2024
0cddd58
add tests
putianyi889 Mar 17, 2024
84ac2f8
Update number.jl
putianyi889 Mar 17, 2024
b1b86a5
Update floatfuncs.jl
putianyi889 Mar 17, 2024
f09a032
export methods
putianyi889 Mar 17, 2024
a2bbe5e
update tests
putianyi889 Mar 17, 2024
ff0704f
whitespace
putianyi889 Mar 18, 2024
a2aee14
fix tests
putianyi889 Mar 18, 2024
9276fb4
Update test/numbers.jl
putianyi889 Mar 18, 2024
c63b3ce
Update gmp.jl
putianyi889 Mar 19, 2024
7c03d55
Update mpfr.jl
putianyi889 Mar 19, 2024
bb1201a
Update number.jl
putianyi889 Mar 19, 2024
049133a
Update int.jl
putianyi889 Mar 19, 2024
645ceb6
Update floatfuncs.jl
putianyi889 Mar 19, 2024
2e5941f
Update numbers.jl
putianyi889 Mar 19, 2024
4540780
Update bool.jl
putianyi889 Mar 19, 2024
bfd28c5
Update numbers.jl
putianyi889 Mar 19, 2024
1b71527
Update numbers.jl
putianyi889 Mar 19, 2024
e09fec9
Update base/number.jl
putianyi889 Mar 19, 2024
86c7224
Update gmp.jl
putianyi889 Mar 19, 2024
7a1e53f
Update gmp.jl
putianyi889 Mar 19, 2024
24dd8be
Update mpfr.jl
putianyi889 Mar 19, 2024
f303313
Update gmp.jl
putianyi889 Mar 19, 2024
d8d1baa
Update base/number.jl
putianyi889 Mar 19, 2024
099680e
Update base/number.jl
putianyi889 Mar 19, 2024
5ed79da
add compat note
putianyi889 Mar 20, 2024
bd2dd5f
Update test/numbers.jl
putianyi889 Mar 21, 2024
54dd7c7
Update base/number.jl
putianyi889 Mar 21, 2024
a66321a
Update base/number.jl
putianyi889 Mar 21, 2024
761ed61
Merge branch 'master' into patch-6
putianyi889 Oct 21, 2024
b6c1d88
fix
putianyi889 Oct 21, 2024
d6b9be6
Merge branch 'master' into patch-6
putianyi889 Oct 21, 2024
9ebc0a7
Update NEWS.md
putianyi889 Oct 21, 2024
dfa057e
deprecate `ispos` and `isneg`
putianyi889 Oct 21, 2024
e922bf5
Update bool.jl
putianyi889 Oct 21, 2024
02dec43
remove generic fallback
putianyi889 Oct 21, 2024
7db7a2b
Update NEWS.md
putianyi889 Oct 21, 2024
fc3a0de
Update gmp.jl
putianyi889 Oct 21, 2024
c372cb2
Update gmp.jl
putianyi889 Oct 21, 2024
9e26047
Update gmp.jl
putianyi889 Oct 21, 2024
bfbb69c
Merge branch 'master' into patch-6
putianyi889 Oct 21, 2024
85b3e56
Update missing.jl
putianyi889 Oct 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,9 @@ export
isinf,
isinteger,
isnan,
isnegative,
isodd,
ispositive,
ispow2,
isqrt,
isreal,
Expand Down
3 changes: 3 additions & 0 deletions base/floatfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ signbit(x::Float64) = signbit(bitcast(Int64, x))
signbit(x::Float32) = signbit(bitcast(Int32, x))
signbit(x::Float16) = signbit(bitcast(Int16, x))

ispositive(x::IEEEFloat) = x>0
isnegative(x::IEEEFloat) = x<0
putianyi889 marked this conversation as resolved.
Show resolved Hide resolved

"""
maxintfloat(T=Float64)

Expand Down
39 changes: 39 additions & 0 deletions base/number.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,45 @@ true
"""
signbit(x::Real) = x < 0

"""
ispositive(x)

Test whether `x` is positive. Falls back to [`isnan`](@ref), [`signbit`](@ref) and [`iszero`](@ref). See also [`isnegative`](@ref).
putianyi889 marked this conversation as resolved.
Show resolved Hide resolved

# Examples
```jldoctest
julia> ispositive(-4.0)
false

julia> ispositive(99)
true

julia> ispositive(0.0)
false
```
"""
ispositive(x) = !isnan(x) && !signbit(x) && !iszero(x)

"""
isnegative(x)

Test whether `x` is positive. Falls back to [`isnan`](@ref), [`signbit`](@ref) and [`iszero`](@ref). See also [`ispositive`](@ref).
putianyi889 marked this conversation as resolved.
Show resolved Hide resolved

# Examples
```jldoctest
julia> isnegative(-4.0)
true

julia> isnegative(99)
false

julia> isnegative(-0.0)
false
```
"""
isnegative(x) = !isnan(x) && signbit(x) && !iszero(x)
isnegative(x::Integer) = signbit(x) # skip iszero
putianyi889 marked this conversation as resolved.
Show resolved Hide resolved

"""
sign(x)

Expand Down
48 changes: 48 additions & 0 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,54 @@ end
@test cmp(isless, 1, NaN) == -1
@test cmp(isless, NaN, NaN) == 0
end
@testset "ispositive/isnegative" begin
putianyi889 marked this conversation as resolved.
Show resolved Hide resolved
for T in Base.uniontypes(Base.IEEEFloat)
putianyi889 marked this conversation as resolved.
Show resolved Hide resolved
@test ispositive(one(T))
@test !ispositive(zero(T))
@test !ispositive(-zero(T))
@test !ispositive(-one(T))
@test !isnegative(one(T))
@test !isnegative(zero(T))
@test !isnegative(-zero(T))
@test isnegative(-one(T))
@test ispositive(T(Inf))
@test !ispositive(T(-Inf))
@test !ispositive(T(NaN))
@test !ispositive(T(-NaN))
end

for T in Base.BitSigned_types
@test ispositive(one(T))
@test !ispositive(zero(T))
@test !ispositive(-one(T))
@test !isnegative(one(T))
@test !isnegative(zero(T))
@test isnegative(-one(T))
end

for T in Base.BitUnsigned_types
@test ispositive(one(T))
@test !ispositive(zero(T))
@test ispositive(-one(T))
@test !isnegative(one(T))
@test !isnegative(zero(T))
@test !isnegative(-one(T))
end

@test ispositive(2//3)
@test !ispositive(-2//3)
@test !ispositive(0//1)
@test !ispositive(-0//1)
@test ispositive(1//0)
@test !ispositive(-1//0)

@test !isnegative(2//3)
@test isnegative(-2//3)
@test !isnegative(0//1)
@test !isnegative(-0//1)
@test !isnegative(1//0)
@test isnegative(-1//0)
end
@testset "Float vs Integer comparison" begin
for x=-5:5, y=-5:5
@test (x==y)==(Float64(x)==Int64(y))
Expand Down
Loading