From dd61d7eb5b1ce934cbdeffd3527246247c514245 Mon Sep 17 00:00:00 2001 From: Joey574 Date: Fri, 10 Apr 2026 14:39:12 -0700 Subject: [PATCH 1/4] json support --- float16.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/float16.go b/float16.go index c315091..e751e54 100644 --- a/float16.go +++ b/float16.go @@ -222,6 +222,11 @@ func (f Float16) String() string { return strconv.FormatFloat(float64(f.Float32()), 'f', -1, 32) } +// MarshalJSON satisfies json marshaller +func (f Float16) MarshalJSON() ([]byte, error) { + return []byte(f.String()), nil +} + // f16bitsToF32bits returns uint32 (float32 bits) converted from specified uint16. func f16bitsToF32bits(in uint16) uint32 { // All 65536 conversions with this were confirmed to be correct From d91e3502e4c3d83a59a02f4d61d4d577655a6f1d Mon Sep 17 00:00:00 2001 From: Joey574 Date: Fri, 10 Apr 2026 14:42:04 -0700 Subject: [PATCH 2/4] s --- float16_bench_test.go | 2 +- float16_test.go | 2 +- go.mod | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/float16_bench_test.go b/float16_bench_test.go index bd9fbfa..7767b4a 100644 --- a/float16_bench_test.go +++ b/float16_bench_test.go @@ -6,7 +6,7 @@ import ( "math" "testing" - "github.com/x448/float16" + "github.com/Joey574/float16" ) // prevent compiler optimizing out code by assigning to these diff --git a/float16_test.go b/float16_test.go index 104a8de..eeb43ad 100644 --- a/float16_test.go +++ b/float16_test.go @@ -11,7 +11,7 @@ import ( "math" "testing" - "github.com/x448/float16" + "github.com/Joey574/float16" ) // wantF32toF16bits is a tiny subset of expected values diff --git a/go.mod b/go.mod index 0afee11..2c3412b 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/x448/float16 +module github.com/Joey574/float16 go 1.17 From 8687f8941c2f9e0f141c3514487f8812f9b4ad0f Mon Sep 17 00:00:00 2001 From: Joey574 Date: Fri, 10 Apr 2026 14:57:51 -0700 Subject: [PATCH 3/4] complete --- float16_bench_test.go | 14 +++++++++++++- float16_test.go | 16 +++++++++++++++- go.mod | 2 +- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/float16_bench_test.go b/float16_bench_test.go index 7767b4a..28c08b2 100644 --- a/float16_bench_test.go +++ b/float16_bench_test.go @@ -6,7 +6,7 @@ import ( "math" "testing" - "github.com/Joey574/float16" + "github.com/x448/float16" ) // prevent compiler optimizing out code by assigning to these @@ -86,3 +86,15 @@ func BenchmarkString(b *testing.B) { } resultStr = result } + +func BenchmarkMarshalJson(b *testing.B) { + var result []byte + + pi32 := float32(math.Pi) + pi16 := float16.Fromfloat32(pi32) + + for i := 0; i < b.N; i++ { + result, _ = pi16.MarshalJSON() + } + resultStr = string(result) +} diff --git a/float16_test.go b/float16_test.go index eeb43ad..6072ea9 100644 --- a/float16_test.go +++ b/float16_test.go @@ -11,7 +11,7 @@ import ( "math" "testing" - "github.com/Joey574/float16" + "github.com/x448/float16" ) // wantF32toF16bits is a tiny subset of expected values @@ -616,6 +616,20 @@ func TestString(t *testing.T) { } +func TestMarshalJson(t *testing.T) { + f16 := float16.Fromfloat32(1.5) + s := f16.String() + if s != "1.5" { + t.Errorf("Float16(1.5).MarshalJson() returned %s, wanted 1.5", s) + } + + f16 = float16.Fromfloat32(3.14159) + s = f16.String() + if s != "3.140625" { + t.Errorf("Float16(3.141593).MarshalJson() returned %s, wanted 3.140625", s) + } +} + func TestIsInf(t *testing.T) { f16 := float16.Float16(0) diff --git a/go.mod b/go.mod index 2c3412b..0afee11 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module github.com/Joey574/float16 +module github.com/x448/float16 go 1.17 From cb549aacf46545c3e572e5dda97b1a1b49add65a Mon Sep 17 00:00:00 2001 From: Joey574 Date: Sat, 11 Apr 2026 08:05:00 -0700 Subject: [PATCH 4/4] fixed the marshal json test --- float16_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/float16_test.go b/float16_test.go index 6072ea9..6b1a614 100644 --- a/float16_test.go +++ b/float16_test.go @@ -618,14 +618,14 @@ func TestString(t *testing.T) { func TestMarshalJson(t *testing.T) { f16 := float16.Fromfloat32(1.5) - s := f16.String() - if s != "1.5" { + s, _ := f16.MarshalJSON() + if string(s) != "1.5" { t.Errorf("Float16(1.5).MarshalJson() returned %s, wanted 1.5", s) } f16 = float16.Fromfloat32(3.14159) - s = f16.String() - if s != "3.140625" { + s, _ = f16.MarshalJSON() + if string(s) != "3.140625" { t.Errorf("Float16(3.141593).MarshalJson() returned %s, wanted 3.140625", s) } }