@@ -393,7 +393,7 @@ function (:)(start::T, step::T, stop::T) where T<:Union{Float16,Float32,Float64}
393
393
stop_n, stop_d = rat (stop)
394
394
if start_d != 0 && stop_d != 0 &&
395
395
T (start_n/ start_d) == start && T (stop_n/ stop_d) == stop
396
- den = lcm (start_d, step_d) # use same denominator for start and step
396
+ den = lcm_unchecked (start_d, step_d) # use same denominator for start and step
397
397
m = maxintfloat (T, Int)
398
398
if den != 0 && abs (start* den) <= m && abs (step* den) <= m && # will round succeed?
399
399
rem (den, start_d) == 0 && rem (den, step_d) == 0 # check lcm overflow
@@ -429,7 +429,7 @@ function _range(a::T, st::T, ::Nothing, len::Integer) where T<:Union{Float16,Flo
429
429
step_n, step_d = rat (st)
430
430
if start_d != 0 && step_d != 0 &&
431
431
T (start_n/ start_d) == a && T (step_n/ step_d) == st
432
- den = lcm (start_d, step_d)
432
+ den = lcm_unchecked (start_d, step_d)
433
433
m = maxintfloat (T, Int)
434
434
if abs (den* a) <= m && abs (den* st) <= m &&
435
435
rem (den, start_d) == 0 && rem (den, step_d) == 0
@@ -513,7 +513,7 @@ function _convertSRL(::Type{StepRangeLen{T,R,S}}, r::AbstractRange{U}) where {T,
513
513
step_n, step_d = rat (s)
514
514
if start_d != 0 && step_d != 0 &&
515
515
U (start_n/ start_d) == f && U (step_n/ step_d) == s
516
- den = lcm (start_d, step_d)
516
+ den = lcm_unchecked (start_d, step_d)
517
517
m = maxintfloat (T, Int)
518
518
if den != 0 && abs (f* den) <= m && abs (s* den) <= m &&
519
519
rem (den, start_d) == 0 && rem (den, step_d) == 0
@@ -582,7 +582,7 @@ function _range(start::T, ::Nothing, stop::T, len::Integer) where {T<:IEEEFloat}
582
582
start_n, start_d = rat (start)
583
583
stop_n, stop_d = rat (stop)
584
584
if start_d != 0 && stop_d != 0
585
- den = lcm (start_d, stop_d)
585
+ den = lcm_unchecked (start_d, stop_d)
586
586
m = maxintfloat (T, Int)
587
587
if den != 0 && abs (den* start) <= m && abs (den* stop) <= m
588
588
start_n = round (Int, den* start)
@@ -691,6 +691,9 @@ function rat(x)
691
691
return a, b
692
692
end
693
693
694
+ # This version of lcm does not check for overflows
695
+ lcm_unchecked (a:: T , b:: T ) where T<: Integer = a * div (b, gcd (a, b))
696
+
694
697
narrow (:: Type{T} ) where {T<: AbstractFloat } = Float64
695
698
narrow (:: Type{Float64} ) = Float32
696
699
narrow (:: Type{Float32} ) = Float16
0 commit comments