Skip to content

Commit 3dd0414

Browse files
committed
Don't promote to float in norm(::Number). Fixes #36071.
1 parent 7301dc6 commit 3dd0414

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

stdlib/LinearAlgebra/src/generic.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -621,31 +621,31 @@ end
621621
"""
622622
norm(x::Number, p::Real=2)
623623
624-
For numbers, return ``\\left( |x|^p \\right)^{1/p}``.
624+
For numbers, return ``abs(x)`` when ``p>0``. When `p==0`, return one if ``x!=0`` otherwise return zero.
625625
626626
# Examples
627627
```jldoctest
628628
julia> norm(2, 1)
629-
2.0
629+
2
630630
631631
julia> norm(-2, 1)
632-
2.0
632+
2
633633
634634
julia> norm(2, 2)
635-
2.0
635+
2
636636
637637
julia> norm(-2, 2)
638-
2.0
638+
2
639639
640640
julia> norm(2, Inf)
641-
2.0
641+
2
642642
643643
julia> norm(-2, Inf)
644-
2.0
644+
2
645645
```
646646
"""
647647
@inline function norm(x::Number, p::Real=2)
648-
afx = abs(float(x))
648+
afx = abs(x)
649649
if p == 0
650650
if x == 0
651651
return zero(afx)

stdlib/LinearAlgebra/test/generic.jl

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,8 @@ end
334334
@test [[1,2, [3,4]], 5.0, [6im, [7.0, 8.0]]] [[1,2, [3,4]], 5.0, [6im, [7.0, 8.0]]]
335335
end
336336

337-
# Minimal modulo number type - but not subtyping Number
338-
struct ModInt{n}
337+
# Minimal modulo number type
338+
struct ModInt{n} <: Number
339339
k
340340
ModInt{n}(k) where {n} = new(mod(k,n))
341341
ModInt{n}(k::ModInt{n}) where {n} = k
@@ -356,20 +356,16 @@ Base.adjoint(a::ModInt{n}) where {n} = ModInt{n}(conj(a))
356356
Base.transpose(a::ModInt{n}) where {n} = a # see Issue 20978
357357
LinearAlgebra.Adjoint(a::ModInt{n}) where {n} = adjoint(a)
358358
LinearAlgebra.Transpose(a::ModInt{n}) where {n} = transpose(a)
359+
# Needed for pivoting:
360+
Base.abs(a::ModInt{n}) where {n} = a
361+
Base.:<(a::ModInt{n}, b::ModInt{n}) where {n} = a.k < b.k
359362

360363
@testset "Issue 22042" begin
361364
A = [ModInt{2}(1) ModInt{2}(0); ModInt{2}(1) ModInt{2}(1)]
362365
b = [ModInt{2}(1), ModInt{2}(0)]
363366

364367
@test A*(lu(A, Val(false))\b) == b
365-
366-
# Needed for pivoting:
367-
Base.abs(a::ModInt{n}) where {n} = a
368-
LinearAlgebra.norm(a::ModInt{n}) where {n} = a
369-
370-
Base.:<(a::ModInt{n}, b::ModInt{n}) where {n} = a.k < b.k
371-
372-
@test A*(lu(A, Val(true))\b) == b
368+
@test A*(lu(A, Val(true)) \b) == b
373369
end
374370

375371
@testset "Issue 18742" begin

0 commit comments

Comments
 (0)