Skip to content

Commit 12f98cf

Browse files
authored
Add Float32::MIN_SUBNORMAL and Float64::MIN_SUBNORMAL (#13961)
1 parent 31762cf commit 12f98cf

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

spec/std/float_printer_spec.cr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ describe "Float32#to_s" do
6363
it_converts_to_s 0xFFC0_0000_u32.unsafe_as(Float32), "NaN"
6464
it_converts_to_s Float32::MIN_POSITIVE, "1.1754944e-38"
6565
it_converts_to_s Float32::MAX, "3.4028235e+38"
66-
it_converts_to_s hexfloat("0x1.ffffffp-127_f32"), "1.1754942e-38" # largest denormal
67-
it_converts_to_s 1.0e-45_f32 # smallest denormal
66+
it_converts_to_s Float32::MIN_POSITIVE.prev_float, "1.1754942e-38" # largest subnormal
67+
it_converts_to_s Float32::MIN_SUBNORMAL, "1.0e-45"
6868
end
6969

7070
context "Ryu f2s_test.cc BoundaryRoundEven" do
@@ -347,8 +347,8 @@ describe "Float64#to_s" do
347347
it_converts_to_s 0xFFF8_0000_0000_0000_u64.unsafe_as(Float64), "NaN"
348348
it_converts_to_s Float64::MIN_POSITIVE, "2.2250738585072014e-308"
349349
it_converts_to_s Float64::MAX, "1.7976931348623157e+308"
350-
it_converts_to_s hexfloat("0x1.fffffffffffffp-1023"), "2.225073858507201e-308" # largest denormal
351-
it_converts_to_s 5.0e-324 # smallest denormal
350+
it_converts_to_s Float64::MIN_POSITIVE.prev_float, "2.225073858507201e-308" # largest subnormal
351+
it_converts_to_s Float64::MIN_SUBNORMAL, "5.0e-324"
352352
end
353353

354354
context "Ryu d2s_test.cc LotsOfTrailingZeros" do

spec/std/float_spec.cr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ describe "Float" do
225225

226226
describe "#next_float" do
227227
it "does for f64" do
228-
0.0.next_float.should eq(5.0e-324) # smallest denormal (not MIN_POSITIVE)
228+
0.0.next_float.should eq(Float64::MIN_SUBNORMAL)
229229
1.0.next_float.should eq(1.0000000000000002)
230230
(-1.0).next_float.should eq(-0.9999999999999999)
231231
Float64::MAX.next_float.should eq(Float64::INFINITY)
@@ -235,7 +235,7 @@ describe "Float" do
235235
end
236236

237237
it "does for f32" do
238-
0.0_f32.next_float.should eq(1.0e-45_f32) # smallest denormal (not MIN_POSITIVE)
238+
0.0_f32.next_float.should eq(Float32::MIN_SUBNORMAL)
239239
1.0_f32.next_float.should eq(1.0000001_f32)
240240
(-1.0_f32).next_float.should eq(-0.99999994_f32)
241241
Float32::MAX.next_float.should eq(Float32::INFINITY)
@@ -247,7 +247,7 @@ describe "Float" do
247247

248248
describe "#prev_float" do
249249
it "does for f64" do
250-
0.0.prev_float.should eq(-5.0e-324) # smallest denormal (not MIN_POSITIVE)
250+
0.0.prev_float.should eq(-Float64::MIN_SUBNORMAL)
251251
1.0.prev_float.should eq(0.9999999999999999)
252252
(-1.0).prev_float.should eq(-1.0000000000000002)
253253
Float64::MIN.prev_float.should eq(-Float64::INFINITY)
@@ -257,7 +257,7 @@ describe "Float" do
257257
end
258258

259259
it "does for f32" do
260-
0.0_f32.prev_float.should eq(-1.0e-45_f32) # smallest denormal (not MIN_POSITIVE)
260+
0.0_f32.prev_float.should eq(-Float32::MIN_SUBNORMAL)
261261
1.0_f32.prev_float.should eq(0.99999994_f32)
262262
(-1.0_f32).prev_float.should eq(-1.0000001_f32)
263263
Float32::MIN.prev_float.should eq(-Float32::INFINITY)

src/float.cr

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,10 @@ struct Float32
129129
MIN_10_EXP = -37
130130
# The maximum possible power of 10 exponent (such that 10**MAX_10_EXP is representable)
131131
MAX_10_EXP = 38
132-
# Smallest representable positive value
132+
# Smallest normal positive value, whose previous representable value is subnormal
133133
MIN_POSITIVE = 1.17549435e-38_f32
134+
# Smallest representable positive value, whose previous representable value is zero
135+
MIN_SUBNORMAL = 1.0e-45_f32
134136

135137
# Returns a `Float32` by invoking `String#to_f32` on *value*.
136138
#
@@ -249,8 +251,10 @@ struct Float64
249251
MIN_10_EXP = -307
250252
# The maximum possible power of 10 exponent (such that 10**MAX_10_EXP is representable)
251253
MAX_10_EXP = 308
252-
# Smallest representable positive value
254+
# Smallest normal positive value, whose previous representable value is subnormal
253255
MIN_POSITIVE = 2.2250738585072014e-308_f64
256+
# Smallest representable positive value, whose previous representable value is zero
257+
MIN_SUBNORMAL = 5.0e-324_f64
254258

255259
# Returns a `Float64` by invoking `String#to_f64` on *value*.
256260
#

0 commit comments

Comments
 (0)