Fixed special convertion case from decimal 'x.y()' to fraction

This commit is contained in:
2024-05-15 06:45:40 +02:00
parent c9db4b84e3
commit aa1338cd51
2 changed files with 7 additions and 3 deletions
+6 -3
View File
@@ -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] {