I’m trying to define a large number in a Sway unit test, but getting the following issue:
280 | let expected: u256 = 160149899619106589403934712464197979;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ int literal out of range
How can I define large integers?
diyahir
November 20, 2023, 9:31am
2
//! A 256-bit unsigned integer type.
library;
use ::assert::*;
use ::convert::From;
use ::result::Result::{self, *};
use ::u128::U128;
use ::math::Power;
/// Left shift a `u64` and preserve the overflow amount if any.
fn lsh_with_carry(word: u64, shift_amount: u64) -> (u64, u64) {
let right_shift_amount = 64 - shift_amount;
let carry = word >> right_shift_amount;
let shifted = word << shift_amount;
(shifted, carry)
}
/// Right shift a `u64` and preserve the overflow amount if any.
fn rsh_with_carry(word: u64, shift_amount: u64) -> (u64, u64) {
let left_shift_amount = 64 - shift_amount;
This file has been truncated. show original
pub struct U256 {
/// The most significant 64 bits of the `U256`.
a: u64,
/// The 65-128th most significant bits of the `U256`.
b: u64,
/// The 129-192nd most significant bits of the `U256`.
c: u64,
/// The 193-256th most significant bits of the `U256`.
d: u64,
}
I don’t think it can just take the raw number, I believe you need to decompose your number into 4 64 bit pieces and then reconstruct it
I’m trying to use the native u256
, not the U256
library.
I worded around it by converting to hex and using the 0x1234u256
notation. But would still be nice to have decimal literals as well.
FossilFrank:
u256
Yes, @FossilFrank , that is the correct way to do it. I will bring this up with the team to discuss implementing decimal literals.