Skip to content

Commit

Permalink
Make ErrInvalidNaNValue a const and fix docs
Browse files Browse the repository at this point in the history
* make ErrInvalidNaNValue a const to prevent changes
* mention FromNaN32ps can return ErrInvalidNaNValue with sNaN (0x7c01)
  • Loading branch information
x448 committed Jan 17, 2020
1 parent 1dace37 commit cb9afec
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Fromfloat32(f32 float32) Float16 // Float16 number converted from f32 using IE
FromNaN32ps(nan float32) (Float16, error) // Float16 NaN without modifying quiet bit.
// The "ps" suffix means "preserve signaling".
// Returns ErrInvalidNaNValue if nan isn't a NaN.
// Returns sNaN and ErrInvalidNaNValue if nan isn't a NaN.
Frombits(b16 uint16) Float16 // Float16 number corresponding to b16 (IEEE 754 binary16 rep.)
NaN() Float16 // Float16 of IEEE 754 binary16 not-a-number
Expand Down
1 change: 1 addition & 0 deletions float16.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func (e float16Error) Error() string { return string(e) }
// qNaN because it sets quiet bit = 1, this can return both sNaN and qNaN.
// If the result is infinity (sNaN with empty payload), then the
// lowest bit of payload is set to make the result a NaN.
// Returns ErrInvalidNaNValue and 0x7c01 (sNaN) if nan isn't IEEE 754 NaN.
// This function was kept simple to be able to inline.
func FromNaN32ps(nan float32) (Float16, error) {
const SNAN = Float16(uint16(0x7c01)) // signalling NaN
Expand Down
3 changes: 3 additions & 0 deletions float16_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ func TestFromNaN32ps(t *testing.T) {
if err != float16.ErrInvalidNaNValue {
t.Errorf("FromNaN32ps: in float32(math.Pi) wanted err float16.ErrInvalidNaNValue, got err = %q", err)
}
if err.Error() != "float16: invalid NaN value, expected IEEE 754 NaN" {
t.Errorf("unexpected string value returned by err.Error() for ErrInvalidNaNValue: %s", err.Error())
}
if uint16(nan) != 0x7c01 { // signalling NaN
t.Errorf("FromNaN32ps: in float32(math.Pi) wanted nan = 0x7c01, got nan = 0x%04x", uint16(nan))
}
Expand Down

0 comments on commit cb9afec

Please sign in to comment.