From aa1338cd51c88f36897ed51907223d74d2879c13 Mon Sep 17 00:00:00 2001 From: Celestino Amoroso <celestino.amoroso@gmail.com> Date: Wed, 15 May 2024 06:45:40 +0200 Subject: [PATCH] Fixed special convertion case from decimal 'x.y()' to fraction --- operator-fraction.go | 9 ++++++--- parser_test.go | 1 + 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/operator-fraction.go b/operator-fraction.go index 2e11c0e..23363c5 100644 --- a/operator-fraction.go +++ b/operator-fraction.go @@ -29,7 +29,7 @@ func float64ToFraction(f float64) (fract *fraction, err error) { var sign string intPart, decPart := math.Modf(f) if decPart < 0.0 { - sign="-" + sign = "-" intPart = -intPart decPart = -decPart } @@ -89,11 +89,14 @@ func makeGeneratingFraction(s string) (f *fraction, err error) { goto exit } if s[0] == '-' { - sign=int64(-1) + sign = int64(-1) s = s[1:] } else if s[0] == '+' { s = s[1:] } + if strings.HasSuffix(s, "()") { + s = s[0 : len(s)-2] + } parts = strings.SplitN(s, ".", 2) if num, err = strconv.ParseInt(parts[0], 10, 64); err != nil { return @@ -106,7 +109,7 @@ func makeGeneratingFraction(s string) (f *fraction, err error) { den = 1 dec := parts[1] lsd := len(dec) - for i:=lsd-1; i>= 0 && dec[i]=='0'; i-- { + for i := lsd - 1; i >= 0 && dec[i] == '0'; i-- { lsd-- } for _, c := range dec[0:lsd] { diff --git a/parser_test.go b/parser_test.go index bb4d8a8..9e2f4ee 100644 --- a/parser_test.go +++ b/parser_test.go @@ -158,6 +158,7 @@ func TestGeneralParser(t *testing.T) { /* 137 */ {`builtin "os.file"`, int64(1), nil}, /* 138 */ {`v=10; v++; v`, int64(11), nil}, /* 139 */ {`1+1|2+0.5`, float64(2), nil}, + /* 140 */ {`1.2()`, newFraction(6, 5), nil}, } // t.Setenv("EXPR_PATH", ".")