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

Fixed norm #30481

Merged
merged 8 commits into from
Jan 7, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
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
14 changes: 7 additions & 7 deletions stdlib/LinearAlgebra/src/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -469,25 +469,25 @@ For numbers, return ``\\left( |x|^p \\right)^{1/p}``.
# Examples
```jldoctest
julia> norm(2, 1)
2
2.0

julia> norm(-2, 1)
2
2.0

julia> norm(2, 2)
2
2.0

julia> norm(-2, 2)
2
2.0

julia> norm(2, Inf)
2
2.0

julia> norm(-2, Inf)
2
2.0
```
"""
@inline norm(x::Number, p::Real=2) = p == 0 ? (x==0 ? zero(abs(x)) : oneunit(abs(x))) : abs(x)
@inline norm(x::Number, p::Real=2) = p == 0 ? (x==0 ? zero(abs(float(x))) : oneunit(abs(float(x)))) : abs(float(x))
norm(::Missing, p::Real=2) = missing

# special cases of opnorm
Expand Down
5 changes: 5 additions & 0 deletions stdlib/LinearAlgebra/test/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ end
end
end

@testset "Issue #30466" begin
@test norm([typemin(Int), typemin(Int)], Inf) == 9.223372036854776e18
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Int is Int32 on 32 bit systems so make the rhs here float(typemax(Int)) and 2float(typemax(Int)) in the line below.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean -float(typemin(Int)), I guess (since typemin and typemax are not equal in magnitude).

@test norm([typemin(Int), typemin(Int)], 1) == 1.8446744073709552e19
end

@testset "potential overflow in normalize!" begin
δ = inv(prevfloat(typemax(Float64)))
v = [δ, -δ]
Expand Down
1 change: 1 addition & 0 deletions test/testhelpers/Quaternions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct Quaternion{T<:Real} <: Number
end
Quaternion(s::Real, v1::Real, v2::Real, v3::Real) = Quaternion(promote(s, v1, v2, v3)...)
Base.abs2(q::Quaternion) = q.s*q.s + q.v1*q.v1 + q.v2*q.v2 + q.v3*q.v3
Base.float(z::Quaternion{T}) where T = Quaternion(float(z.s), float(z.v1), float(z.v2), float(z.v3))
Base.abs(q::Quaternion) = sqrt(abs2(q))
Base.real(::Type{Quaternion{T}}) where {T} = T
Base.conj(q::Quaternion) = Quaternion(q.s, -q.v1, -q.v2, -q.v3)
Expand Down