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

Context.Rem fails for Rem(100E100, 10) #134

Open
NoamTD opened this issue Apr 23, 2024 · 1 comment
Open

Context.Rem fails for Rem(100E100, 10) #134

NoamTD opened this issue Apr 23, 2024 · 1 comment

Comments

@NoamTD
Copy link

NoamTD commented Apr 23, 2024

I attempted to run the following test:

func TestRem(t *testing.T) {
	a := apd.New(100, 100)
	b := apd.New(10, 1)
	var rem apd.Decimal
	_, err := apd.BaseContext.Rem(&rem, a, b)
	if err != nil {
		t.Fatal(err)
	}

	if !rem.IsZero() {
		t.Fatal("expected zero")
	}
}

I would expect this test to pass, but it fails:

=== RUN   TestRem
    rem_test.go:72: division impossible
--- FAIL: TestRem (0.00s)

FAIL

apd.BaseContext.Rem returns division impossible, which (I believe) is an incorrect result in this case.

@mvdan
Copy link

mvdan commented Apr 25, 2024

For some added context, we wanted to use Rem to be able to precisely tell if one apd.Decimal number was a multiple of another. We instead ended up with the following code, requiring two apd operations (Quo + Modf) instead of one (Rem):

_, err := apdCtx.Quo(&d, x, y)
if err != nil {
	return false, err
}
var frac apd.Decimal
d.Modf(nil, &frac)
return frac.IsZero(), nil

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

2 participants