Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Float calculation error #162

Open
siguorui opened this issue Nov 1, 2022 · 0 comments
Open

Float calculation error #162

siguorui opened this issue Nov 1, 2022 · 0 comments

Comments

@siguorui
Copy link

siguorui commented Nov 1, 2022

Floats cannot be added or subtracted directly. Because of the accuracy problem, calculation errors may occur. eg:

func TestIncrementFloat64(t *testing.T) { tc := New(DefaultExpiration, 0) tc.Set("float64", float64(0.6), DefaultExpiration) n, err := tc.IncrementFloat64("float64", 0.7) if err != nil { t.Error("Error incrementing:", err) } if n != 1.3 { t.Error("Returned number is not 1.3:", n) } x, found := tc.Get("float64") if !found { t.Error("float64 was not found") } if x.(float64) != 1.3 { t.Error("float64 is not 1.3:", x) } }

the result will be 1.2999999999999998.

func TestDecrementFloat64(t *testing.T) { tc := New(DefaultExpiration, 0) tc.Set("float64", float64(74.96), DefaultExpiration) n, err := tc.DecrementFloat64("float64", 20.48) if err != nil { t.Error("Error decrementing:", err) } if n != 54.48 { t.Error("Returned number is not 54.48:", n) } x, found := tc.Get("float64") if !found { t.Error("float64 was not found") } if x.(float64) != 54.48 { t.Error("float64 is not 54.48:", x) } }

the result will be 54.47999999999999

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant