-
Notifications
You must be signed in to change notification settings - Fork 855
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
Comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
The text was updated successfully, but these errors were encountered: