From 7a3c48c33617a1b756a4e0010e55498972226922 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Lindstr=C3=B8m?= Date: Tue, 24 Sep 2024 14:11:06 +0200 Subject: [PATCH] by value --- .../move-stdlib/sources/fixed_point32.move | 50 +++++++++++-------- .../move-stdlib/tests/fixedpoint32_tests.move | 48 +++++++++--------- 2 files changed, 53 insertions(+), 45 deletions(-) diff --git a/crates/sui-framework/packages/move-stdlib/sources/fixed_point32.move b/crates/sui-framework/packages/move-stdlib/sources/fixed_point32.move index 3386ce72ad496b..f70640e36150dc 100644 --- a/crates/sui-framework/packages/move-stdlib/sources/fixed_point32.move +++ b/crates/sui-framework/packages/move-stdlib/sources/fixed_point32.move @@ -53,8 +53,8 @@ module std::fixed_point32 { /// Multiply two fixed-point number s, truncating any fractional part of /// the product. This will abort if the product overflows. - public fun mul(a: &FixedPoint32, b: &FixedPoint32): FixedPoint32 { - from_raw(multiply_u64(a.value, *b)) + public fun mul(a: FixedPoint32, b: FixedPoint32): FixedPoint32 { + from_raw(multiply_u64(a.value, b)) } /// Divide a u64 integer by a fixed-point number, truncating any @@ -77,8 +77,8 @@ module std::fixed_point32 { /// Divide two fixed-point numbers, truncating any fractional part of /// the quotient. This will abort if the divisor is zero or if the /// quotient overflows. - public fun div(a: &FixedPoint32, b: &FixedPoint32): FixedPoint32 { - from_raw(divide_u64(a.value, *b)) + public fun div(a: FixedPoint32, b: FixedPoint32): FixedPoint32 { + from_raw(divide_u64(a.value, b)) } /// Create a fixed-point value from a rational number specified by its @@ -109,15 +109,13 @@ module std::fixed_point32 { } /// Create a fixed-point value from a rational number specified by its - /// numerator and denominator. Calling this function should be preferred - /// for using `Self::create_from_raw_value` which is also available. - /// This will abort if the denominator is zero. It will also - /// abort if the numerator is nonzero and the ratio is not in the range - /// 2^-32 .. 2^32-1. When specifying decimal fractions, be careful about - /// rounding errors: if you round to display N digits after the decimal - /// point, you can use a denominator of 10^N to avoid numbers where the - /// very small imprecision in the binary representation could change the - /// rounding, e.g., 0.0125 will round down to 0.012 instead of up to 0.013. + /// numerator and denominator. It will abort if the numerator is nonzero + /// and the ratio is not in the range 2^-32 .. 2^32-1. When specifying + /// decimal fractions, be careful about rounding errors: if you round to + /// display N digits after the decimal point, you can use a denominator of + /// 10^N to avoid numbers where the very small imprecision in the binary + /// representation could change the rounding, e.g., 0.0125 will round down + /// to 0.012 instead of up to 0.013. public fun from_rational(numerator: u64, denominator: u64): FixedPoint32 { // If the denominator is zero, this will abort. // Scale the numerator to have 64 fractional bits and the denominator @@ -169,33 +167,43 @@ module std::fixed_point32 { } /// Add two fixed-point numbers, `a + b`. - public fun add(a: &FixedPoint32, b: &FixedPoint32): FixedPoint32 { + public fun add(a: FixedPoint32, b: FixedPoint32): FixedPoint32 { let sum = (a.value as u128) + (b.value as u128); assert!(sum <= MAX_U64, EADDITION); from_raw(sum as u64) } /// Subtract two fixed-point numbers, `a - b`. - public fun sub(a: &FixedPoint32, b: &FixedPoint32): FixedPoint32 { + public fun sub(a: FixedPoint32, b: FixedPoint32): FixedPoint32 { assert!(a.value >= b.value, ESUBTRACTION); from_raw(a.value - b.value) } - /// Return `true` is and only if `x <= y`. - public fun le(a: &FixedPoint32, b: &FixedPoint32): bool { + /// Return `true` if and only if `x <= y`. + public fun le(a: FixedPoint32, b: FixedPoint32): bool { a.value <= b.value } - /// Return `true` is and only if `a < b`. - public fun lt(a: &FixedPoint32, b: &FixedPoint32): bool { + /// Return `true` if and only if `a < b`. + public fun lt(a: FixedPoint32, b: FixedPoint32): bool { a.value < b.value } - /// Return `true` is and only if `a == b`. - public fun eq(a: &FixedPoint32, b: &FixedPoint32): bool { + /// Return `true` if and only if `a == b`. + public fun eq(a: FixedPoint32, b: FixedPoint32): bool { a.value == b.value } + /// Return `true` if and only if `a >= b`. + public fun ge(a: FixedPoint32, b: FixedPoint32): bool { + a.value >= b.value + } + + /// Return `true` if and only if `a > b`. + public fun gt(a: FixedPoint32, b: FixedPoint32): bool { + a.value > b.value + } + /// Return a fixed-point representation of 1. public fun one(): FixedPoint32 { from_integer(1) diff --git a/crates/sui-framework/packages/move-stdlib/tests/fixedpoint32_tests.move b/crates/sui-framework/packages/move-stdlib/tests/fixedpoint32_tests.move index b3195554aef74f..7cad6642b4393e 100644 --- a/crates/sui-framework/packages/move-stdlib/tests/fixedpoint32_tests.move +++ b/crates/sui-framework/packages/move-stdlib/tests/fixedpoint32_tests.move @@ -118,16 +118,16 @@ module std::fixed_point32_tests { fun test_subtraction_underflow() { let a = from_integer(3); let b = from_integer(5); - let _ = a.sub(&b); + let _ = a.sub(b); } #[test] fun test_subtraction() { let a = from_integer(5); - assert!(a.sub(&zero()) == a); + assert!(a.sub(zero()) == a); let b = from_integer(4); - let c = a.sub(&b); + let c = a.sub(b); assert!(fixed_point32::one() == c); } @@ -136,19 +136,19 @@ module std::fixed_point32_tests { fun test_addition_overflow() { let a = from_integer(1 << 31); let b = from_integer(1 << 31); - let _ = a.add(&b); + let _ = a.add(b); } #[test] fun test_addition() { let a = from_rational(3, 4); - assert!(a.add(&zero()) == a); + assert!(a.add(zero()) == a); - let c = a.add(&one()); + let c = a.add(one()); assert!(from_rational(7, 4) == c); let b = from_rational(1, 4); - let c = a.add(&b); + let c = a.add(b); assert!(fixed_point32::one() == c); } @@ -157,21 +157,19 @@ module std::fixed_point32_tests { fun test_multiplication_overflow() { let a = from_integer(1 << 16); let b = from_integer(1 << 16); - let _ = a.mul(&b); + let _ = a.mul(b); } #[test] fun test_multiplication() { let a = from_rational(3, 4); - assert!(a.mul(&zero()) == zero()); - assert!(a.mul(&one()) == a); + assert!(a.mul(zero()) == zero()); + assert!(a.mul(one()) == a); let b = from_rational(3, 2); - let c = a.mul(&b); + let c = a.mul(b); let expected = from_rational(9, 8); - std::debug::print(&expected); - std::debug::print(&c); - assert!(c.eq(&expected)); + assert!(c.eq(expected)); } #[test] @@ -179,18 +177,18 @@ module std::fixed_point32_tests { fun test_division_by_zero() { let a = from_integer(7); let b = fixed_point32::zero(); - let _ = a.div(&b); + let _ = a.div(b); } #[test] fun test_division() { let a = from_rational(3, 4); - assert!(a.div(&one()) == a); + assert!(a.div(one()) == a); let b = from_integer(8); - let c = a.div(&b); + let c = a.div(b); let expected = from_rational(3, 32); - assert!(c.eq(&expected)); + assert!(c.eq(expected)); } #[test] @@ -198,7 +196,7 @@ module std::fixed_point32_tests { fun test_division_overflow() { let a = from_integer(1 << 31); let b = from_rational(1, 2); - let _ = a.div(&b); + let _ = a.div(b); } #[test] @@ -207,10 +205,12 @@ module std::fixed_point32_tests { let b = from_rational(5, 3); let c = from_rational(5, 2); - assert!(b.le(&a)); - assert!(b.lt(&a)); - assert!(c.le(&a)); - assert!(c.eq(&a)); - assert!(zero().le(&a)); + assert!(b.le(a)); + assert!(b.lt(a)); + assert!(c.le(a)); + assert!(c.eq(a)); + assert!(a.ge(b)); + assert!(a.gt(b)); + assert!(zero().le(a)); } }