Skip to content

Commit

Permalink
Merge branch 'fix-u128-log-rounding-error' of https://github.com/Fuel…
Browse files Browse the repository at this point in the history
…Labs/sway into fix-u128-log-rounding-error
  • Loading branch information
SwayStar123 committed Oct 16, 2024
2 parents c42c383 + d917b46 commit 0076865
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
8 changes: 8 additions & 0 deletions sway-lib-std/src/math.sw
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,18 @@ impl Logarithm for u256 {
assert(base >= 2);
// Logarithm is undefined for 0
assert(self != 0);
} else {
// Logarithm is undefined for bases less than 2
// Logarithm is undefined for 0
if (base < 2) || (self == 0) {
set_flags(flags);
return 0x00u256;
}
}

// Decimals rounded to 0
if self < base {
set_flags(flags);
return 0x00u256;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ fn bytes_append() {

assert(bytes2.len() == second_length);
assert(!bytes2.is_empty());

assert(bytes2.get(0).unwrap() == d);
assert(bytes2.get(1).unwrap() == e);
assert(bytes2.get(2).unwrap() == f);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library;

use std::flags::disable_panic_on_overflow;
use std::{flags::{disable_panic_on_overflow, disable_panic_on_unsafe_math}, registers::flags};

#[test]
fn math_root_u256() {
Expand Down Expand Up @@ -272,13 +272,21 @@ fn math_log_u8() {
#[test]
fn math_log_u256() {
let max_u256 = u256::max();
let prior_flags = flags();
assert(0x2u256.log(0x2u256) == 0x1u256);
assert(0x1u256.log(0x3u256) == 0);
assert(0x8u256.log(0x2u256) == 0x3u256);
assert(0x64u256.log(0xau256) == 0x2u256);
assert(0x64u256.log(0x2u256) == 0x6u256);
assert(0x64u256.log(0x9u256) == 0x2u256);
assert(max_u256.log(0x2u256) == 0xffu256);
assert(prior_flags == flags());

let _ = disable_panic_on_unsafe_math();
let prior_flags = flags();
assert(0x1u256.log(0x1u256) == 0);
assert(0x0u256.log(0x3u256) == 0);
assert(prior_flags == flags());
}

#[test]
Expand Down

0 comments on commit 0076865

Please sign in to comment.