Skip to content

Commit

Permalink
Added test of SmallestNonzero comparing it to the same binary represe…
Browse files Browse the repository at this point in the history
…ntation in float32.
  • Loading branch information
janpfeifer committed Apr 29, 2024
1 parent 2480cf3 commit db1ee2c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions float16.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const (
// the smallest denormal value for float32 (1.401298464324817070923729583289916131280e-45).
// The equivalent formula for float16 is 1 / 2**(15 - 1 + 10). We use Float16(0x0001) to compile as const.
const SmallestNonzero = Float16(0x0001) // 5.9604645e-08 (effectively 0x1p-14 * 0x1p-10)

// PrecisionFromfloat32 returns Precision without performing
// the conversion. Conversions from both Infinity and NaN
// values will always report PrecisionExact even if NaN payload
Expand Down
6 changes: 6 additions & 0 deletions float16_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -794,5 +794,11 @@ func checkRoundTrippedPrecision(t *testing.T, u32 uint32, u16 uint16, u32bis uin
t.Errorf("PrecisionFromfloat32 in f32bits=0x%08x (%032b) (%f), out f16bits=0x%04x (%v), back=0x%08x (%f), got %v, wanted PrecisionExact, exp=%d, coef=%d, drpd=%d", u32, u32, f32, u16, f16, u32bis, f32bis, pre, exp32, coef32, dropped32)
}
}
}

func TestSmallestNonzero(t *testing.T) {
want := float32(0x1p-24)
if float16.SmallestNonzero.Float32() != want {
t.Errorf("Invalid SmallestNonzero to float32 conversion: Float16=%s, wanted %g", float16.SmallestNonzero, want)
}
}

0 comments on commit db1ee2c

Please sign in to comment.