From 6d732527895ad1e83f1a37548a0c71e1a39388d5 Mon Sep 17 00:00:00 2001 From: Jan Kadel Date: Sat, 24 May 2025 19:48:16 +0200 Subject: [PATCH] add new constant one --- fixed.go | 23 ++++++++++++++--------- fixed_test.go | 8 ++++++++ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/fixed.go b/fixed.go index 5f9c301..1e9835f 100644 --- a/fixed.go +++ b/fixed.go @@ -20,18 +20,23 @@ type Fixed struct { // the following constants can be changed to configure a different number of decimal places - these are // the only required changes. only 18 significant digits are supported due to NaN -const nPlaces = 8 -const scale = int64(10 * 10 * 10 * 10 * 10 * 10 * 10 * 10) -const zeros = "00000000" -const MAX = float64(9999999999.99999999) +const ( + nPlaces = 8 + scale = int64(10 * 10 * 10 * 10 * 10 * 10 * 10 * 10) + zeros = "00000000" + MAX = float64(9999999999.99999999) -const nan = int64(1<<63 - 1) + nan = int64(1<<63 - 1) +) -var NaN = Fixed{fp: nan} -var ZERO = Fixed{fp: 0} +var ( + NaN = Fixed{fp: nan} + ZERO = Fixed{fp: 0} + ONE = NewI(1, 0) -var errTooLarge = errors.New("significand too large") -var errFormat = errors.New("invalid encoding") + errTooLarge = errors.New("significand too large") + errFormat = errors.New("invalid encoding") +) // NewS creates a new Fixed from a string, returning NaN if the string could not be parsed func NewS(s string) Fixed { diff --git a/fixed_test.go b/fixed_test.go index b9b6336..af3e5dc 100644 --- a/fixed_test.go +++ b/fixed_test.go @@ -608,6 +608,10 @@ func TestFloor(t *testing.T) { f0 = NewS("0.0000008") f1 = f0.Floor(6) require.Equal(t, "0", f1.String()) + + f0 = NewS("0.00000679") + f1 = f0.Floor(6) + require.Equal(t, "0.000006", f1.String()) } func TestCeil(t *testing.T) { @@ -692,6 +696,10 @@ func TestJSON(t *testing.T) { } } +func TestOne(t *testing.T) { + require.True(t, ONE.Equal(NewS("1"))) +} + func TestJSON_NaN(t *testing.T) { j := JStruct{}