From 54dd983a0dd3c7973611a98e6d022b51a6aff6b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=BCller?= Date: Wed, 27 Aug 2025 10:36:03 -0700 Subject: [PATCH] use big.Int.Quo() instead of Div() to ensure Go-like rounding behaviour --- interpreter/value_fix64.go | 4 ++-- interpreter/value_int128.go | 4 ++-- interpreter/value_int256.go | 4 ++-- values/value_int.go | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/interpreter/value_fix64.go b/interpreter/value_fix64.go index 8d5702c6ad..feda8a54d8 100644 --- a/interpreter/value_fix64.go +++ b/interpreter/value_fix64.go @@ -328,7 +328,7 @@ func (v Fix64Value) Div(context NumberValueArithmeticContext, other NumberValue, valueGetter := func() int64 { result := new(big.Int).Mul(a, sema.Fix64FactorBig) - result.Div(result, b) + result.Quo(result, b) if result.Cmp(minInt64Big) < 0 { panic(&UnderflowError{ @@ -362,7 +362,7 @@ func (v Fix64Value) SaturatingDiv(context NumberValueArithmeticContext, other Nu valueGetter := func() int64 { result := new(big.Int).Mul(a, sema.Fix64FactorBig) - result.Div(result, b) + result.Quo(result, b) if result.Cmp(minInt64Big) < 0 { return math.MinInt64 diff --git a/interpreter/value_int128.go b/interpreter/value_int128.go index 3bede23132..8b2300c13c 100644 --- a/interpreter/value_int128.go +++ b/interpreter/value_int128.go @@ -459,7 +459,7 @@ func (v Int128Value) Div(context NumberValueArithmeticContext, other NumberValue LocationRange: locationRange, }) } - res.Div(v.BigInt, o.BigInt) + res.Quo(v.BigInt, o.BigInt) return res } @@ -495,7 +495,7 @@ func (v Int128Value) SaturatingDiv(context NumberValueArithmeticContext, other N if (v.BigInt.Cmp(sema.Int128TypeMinIntBig) == 0) && (o.BigInt.Cmp(res) == 0) { return sema.Int128TypeMaxIntBig } - res.Div(v.BigInt, o.BigInt) + res.Quo(v.BigInt, o.BigInt) return res } diff --git a/interpreter/value_int256.go b/interpreter/value_int256.go index 7797de68b5..695a1f44cf 100644 --- a/interpreter/value_int256.go +++ b/interpreter/value_int256.go @@ -428,7 +428,7 @@ func (v Int256Value) Div(context NumberValueArithmeticContext, other NumberValue LocationRange: locationRange, }) } - res.Div(v.BigInt, o.BigInt) + res.Quo(v.BigInt, o.BigInt) return res } @@ -463,7 +463,7 @@ func (v Int256Value) SaturatingDiv(context NumberValueArithmeticContext, other N if (v.BigInt.Cmp(sema.Int256TypeMinIntBig) == 0) && (o.BigInt.Cmp(res) == 0) { return sema.Int256TypeMaxIntBig } - res.Div(v.BigInt, o.BigInt) + res.Quo(v.BigInt, o.BigInt) return res } diff --git a/values/value_int.go b/values/value_int.go index 8adeb193ca..e5979f3355 100644 --- a/values/value_int.go +++ b/values/value_int.go @@ -162,7 +162,7 @@ func (v IntValue) Div(gauge common.MemoryGauge, other IntValue) (IntValue, error common.NewDivBigIntMemoryUsage(v.BigInt, other.BigInt), func() *big.Int { res := new(big.Int) - return res.Div(v.BigInt, other.BigInt) + return res.Quo(v.BigInt, other.BigInt) }, ), nil }