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

Add new function that multiply u128 and return fixpoint64 #142

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions aptos-move/framework/aptos-stdlib/sources/fixed_point64.move
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ module aptos_std::fixed_point64 {
(val * multiplier.value) >> 64
}

public fun multiply_u128_return_fixpoint64(val: u128, multiplier: FixedPoint64): FixedPoint64 {
// The product of two 128 bit values has 256 bits, so perform the
// multiplication with u256 types and keep the full 256 bit product
// to avoid losing accuracy.
let unscaled_product = (val as u256) * (multiplier.value as u256);
// Check whether the value is too large.
assert!(unscaled_product <= MAX_U128, EMULTIPLICATION);
create_from_raw_value((unscaled_product as u128))
}

/// Divide a u128 integer by a fixed-point number, truncating any
/// fractional part of the quotient. This will abort if the divisor
/// is zero or if the quotient overflows.
Expand Down