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

Inconsistent (or buggy) Quantize behavior #122

Open
LasTshaMAN opened this issue Aug 15, 2022 · 2 comments
Open

Inconsistent (or buggy) Quantize behavior #122

LasTshaMAN opened this issue Aug 15, 2022 · 2 comments

Comments

@LasTshaMAN
Copy link

LasTshaMAN commented Aug 15, 2022

Looks like Quantize(result, amount, 0) func works differently for:

  • when amount is of the form N.0* (where N is integer number and N != 0)
  • when amount is of the form 0.0*

Here is a simple test to reproduce this (with v3.1.0):

// quantize is a simple wrapper around `apd` library.
func quantize(amount *apd.Decimal, decimalPlaces uint32, roundingRule apd.Rounder) (*apd.Decimal, error) {
	ctx := apd.BaseContext.WithPrecision(infinitePrecision)
	ctx.Rounding = roundingRule

	var amt apd.Decimal
	exp := -int32(decimalPlaces)
	_, err := ctx.Quantize(&amt, amount, exp)

	return &amt, err
}

func TestQuantizeRounding(t *testing.T) {
        // 1.1 -> 2 (works as expected)
	got, err = quantize(apd.New(11, -1), 0, apd.RoundUp)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	want = apd.New(2, 0)
	if want.CmpTotal(got) != 0 {
		t.Fatalf("want: %+v, got: %+v", want, got)
	}

        // 1.01 -> 2 (works as expected)
	got, err := quantize(apd.New(101, -2), 0, apd.RoundUp)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	want := apd.New(2, 0)
	if want.CmpTotal(got) != 0 {
		t.Fatalf("want: %+v, got: %+v", want, got)
	}

        // 0.1 -> 1 (works as expected)
	got, err = quantize(apd.New(1, -1), 0, apd.RoundUp)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	want = apd.New(1, 0)
	if want.CmpTotal(got) != 0 {
		t.Fatalf("want: %+v, got: %+v", want, got)
	}

        // 0.01 -> 1 (doesn't work as expected! instead it does 0.01 -> 0 for some reason)
	got, err = quantize(apd.New(1, -2), 0, apd.RoundUp)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	want = apd.New(1, 0)
	if want.CmpTotal(got) != 0 {
		t.Fatalf("want: %+v, got: %+v", want, got)
	}
}
@LasTshaMAN
Copy link
Author

Can observe similar discrepancy for negative values:

                 // -0.01 -> -1 (doesn't work as expected! instead it does -0.01 -> 0 for some reason)
		got, err = quantize(apd.New(-1, -2), 0, apd.RoundUp)
		if err != nil {
			t.Fatalf("unexpected error: %v", err)
		}
		want = apd.New(-1, 0)
		if want.CmpTotal(got) != 0 {
			t.Fatalf("want: %+v, got: %+v", want, got)
		}

@LasTshaMAN
Copy link
Author

LasTshaMAN commented Aug 15, 2022

And exp param (decimalPlaces in my notation above) doesn't have to be 0, I was also able to reproduce this with -1 (or decimalPlaces being 1 is the equivalent) for example.

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