Int

A library to convert unsigned integers to signed integers using two's complement. It contains basic arithmetic operations for signed integers.

Uses arithmetic shr and shl for negative numbers

Structs

Int

struct Int has copy, drop, store {
    value: u256
 }
  • value - The number.

const EQUAL: u8 = 0;

const LESS_THAN: u8 = 1;

const GREATER_THAN: u8 = 2;

Interface

value

It returns the inner value inside self.

public fun value(self: Int): u256
  • @param self: The Int struct.

  • @return u256.

zero

It creates a zero Int.

public fun zero(): Int
  • @return Int. The wrapped value.

one

It creates a one Int.

public fun one(): Int
  • @return Int. The wrapped value.

max

It creates the largest possible Int.

Maximum number is : 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

public fun max(): Int
  • @return Int.

from_u8

It wraps a u8 value into an Int.

public fun from_u8(value: u8): Int
  • @param value: The u8 value to wrap

  • @return Int. The wrapped value

from_u16

It wraps a u16 value into an Int.

public fun from_u16(value: u16): Int
  • @param value: The u16 value to wrap

  • @return Int. The wrapped value

from_u32

It wraps a u32 value into an Int.

public fun from_u32(value: u32): Int
  • @param value: The u32 value to wrap

  • @return Int. The wrapped value

from_u64

It wraps a u64 value into an Int.

public fun from_u64(value: u64): Int
  • @param value: The u64 value to wrap

  • @return Int. The wrapped value

from_u128

It wraps a u128 value into an Int.

public fun from_u128(value: u128): Int
  • @param value: The u128 value to wrap

  • @return Int. The wrapped value

from_u256

It wraps a u128 value into an Int.

public fun from_u256(value: u256): Int
  • @param value: The u256 value to wrap

  • @return Int. The wrapped value

Abort

  • if value is larger than 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF.

neg_from_u8

It wraps a u8 value into an Int and negates it.

public fun neg_from_u8(value: u8): Int
  • @param value: The u16 value to wrap

  • @return Int. The wrapped value

neg_from_u16

It wraps a u16 value into an Int and negates it.

public fun neg_from_u16(value: u16): Int
  • @param value: The u16 value to wrap

  • @return Int. The wrapped value

neg_from_u32

It wraps a u32 value into an Int and negates it.

public fun neg_from_u32(value: u32): Int
  • @param value: The u32 value to wrap

  • @return Int. The wrapped value

neg_from_u64

It wraps a u64 value into an Int and negates it.

public fun neg_from_u64(value: u64): Int
  • @param value: The u64 value to wrap

  • @return Int. The wrapped value

neg_from_u128

It wraps a u128 value into an Int and negates it.

public fun neg_from_u128(value: u128): Int
  • @param value: The u128 value to wrap

  • @return Int. The wrapped value

neg_from_u256

It wraps a u256 value into an Int and negates it.

public fun neg_from_u256(value: u256): Int
  • @param value: The u256 value to wrap

  • @return Int. The wrapped value

to_u8

It unwraps the value inside self and casts it to u8.

public fun to_u8(self: Int): u8
  • @param self: The Int struct.

  • @return u8. The inner value cast to u8.

Abort

  • self.value is negative

to_u16

It unwraps the value inside self and casts it to u16.

public fun to_u16(self: Int): u16
  • @param self: The Int struct.

  • @return u16. The inner value cast to u16.

Abort

  • self.value is negative

to_u32

It unwraps the value inside self and casts it to u32.

public fun to_u32(self: Int): u32
  • @param self: The Int struct.

  • @return u32. The inner value cast to u32.

Abort

  • self.value is negative

to_u64

It unwraps the value inside self and casts it to u64.

public fun to_u64(self: Int): u64
  • @param self: The Int struct.

  • @return u64. The inner value cast to u64.

Abort

  • self.value is negative

to_u128

It unwraps the value inside self and casts it to u128.

public fun to_u128(self: Int): u128
  • @param self: The Int struct.

  • @return u128. The inner value cast to u128.

Abort

  • self.value is negative

to_u256

It unwraps the value inside self and casts it to u256.

public fun to_u256(self: Int): u256
  • @param self: The Int struct.

  • @return u128. The inner value cast to u256.

Abort

  • self.value is negative

truncate_to_u8

It unwraps the value inside self and truncates it to u8.

public fun truncate_to_u8(self: Int): u8
  • @param self: The Int struct.

  • @return u8. The inner value is truncated to u8.

truncate_to_u16

It unwraps the value inside self and truncates it to u16.

public fun truncate_to_u16(self: Int): u16
  • @param self: The Int struct.

  • @return u8. The inner value is truncated to u16.

truncate_to_u32

It unwraps the value inside self and truncates it to u16.

public fun truncate_to_u32(self: Int): u32
  • @param self: The Int struct.

  • @return u8. The inner value is truncated to u32.

truncate_to_u64

It unwraps the value inside self and truncates it to u64.

public fun truncate_to_u64(self: Int): u64
  • @param self: The Int struct.

  • @return u8. The inner value is truncated to u64.

truncate_to_u128

It unwraps the value inside self and truncates it to u128.

public fun truncate_to_u128(self: Int): u128
  • @param self: The Int struct.

  • @return u8. The inner value is truncated to u128.

flip

It flips the sign of self.

public fun flip(self: Int): Int
  • @param self: The Int struct.

  • @return Int. The returned Int will have its signed flipped.

abs

It returns the absolute of an Int.

public fun abs(self: Int): Int
  • @param self: The Int struct.

  • @return Int. The absolute.

is_neg

It checks if self is negative.

public fun is_neg(self: Int): bool
  • @param self: The Int struct.

  • @return bool.

is_zero

It checks if self is zero.

public fun is_zero(self: Int): bool
  • @param self: The Int struct.

  • @return bool.

is_positive

It checks if self is positive.

public fun is_positive(self: Int): bool
  • @param self: The Int struct.

  • @return bool.

compare

It compares a and b.

public fun compare(a: Int, b: Int): u8
  • @param a: An Int struct.

  • @param b: An Int struct.

  • @return 0. a == b.

  • @return 1. a < b.

  • @return 2. a > b.

eq

It checks if a and b are equal.

public fun eq(a: Int, b: Int): bool
  • @param a: An Int struct.

  • @param b: An Int struct.

  • @return bool.

lt

It checks if a < b.

public fun lt(a: Int, b: Int): bool
  • @param a: An Int struct.

  • @param b: An Int struct.

  • @return bool.

lte

It checks if a <= b.

public fun lte(a: Int, b: Int): bool
  • @param a: An Int struct.

  • @param b: An Int struct.

  • @return bool.

gt

It checks if a > b.

public fun gt(a: Int, b: Int): bool
  • @param a: An Int struct.

  • @param b: An Int struct.

  • @return bool.

gte

It checks if a >= b.

public fun gte(a: Int, b: Int): bool
  • @param a: An Int struct.

  • @param b: An Int struct.

  • @return bool.

add

It checks if a >= b.

public fun add(a: Int, b: Int): Int
  • @param a: An Int struct.

  • @param b: An Int struct.

  • @return Int. The result of a + b.

sub

It performs a - b.

public fun sub(a: Int, b: Int): Int
  • @param a: An Int struct.

  • @param b: An Int struct.

  • @return Int. The result of a - b.

mul

It performs a * b.

public fun mul(a: Int, b: Int): Int
  • @param a: An Int struct.

  • @param b: An Int struct.

  • @return Int. The result of a * b.

div_down

It performs a / b rounding down.

public fun div_down(a: Int, b: Int): Int
  • @param a: An Int struct.

  • @param b: An Int struct.

  • @return Int. The result of a / b rounding down.

div_up

It performs a / b rounding up.

public fun div_up(a: Int, b: Int): Int
  • @param a: An Int struct.

  • @param b: An Int struct.

  • @return Int. The result of a / b rounding up.

mod

It performs a % b.

public fun mod(a: Int, b: Int): Int
  • @param a: An Int struct.

  • @param b: An Int struct.

  • @return Int. The result of a % b.

pow

It performs base ** exponent.

public fun pow(base: Int, exponent: u256): Int
  • @param base: An Int struct.

  • @param exponent: The exponent.

  • @return Int. The result of base ** exponent.

shr

It performs self >> rhs.

public fun shr(self: Int, rhs: u8): Int
  • @param self: An Int struct.

  • @param rhs: The value to right-hand shift.

  • @return Int. The result of self >> rhs.

shl

It performs self << lhs.

public fun shl(self: Int, lhs: u8): Int
  • @param self: An Int struct.

  • @param lhs: The value to right-hand shift.

  • @return Int. The result of self << lhs.

or

It performs a | b.

public fun or(a: Int, b: Int): Int
  • @param a: The first operand.

  • @param b: The second operand.

  • @return Int. The result of a | b.

and

It performs a & b.

public fun and(a: Int, b: Int): Int
  • @param a: The first operand.

  • @param b: The second operand.

  • @return Int. The result of a & b.

Last updated