githubEdit

Math Operations in Move

Move supports familiar math operations with integer-based precision, requiring typecasting for calculations between different unsigned integer types.

circle-info

For the latest RPC URL, please refer to the Supra Network Informationarrow-up-right page.

Math in Move

Doing math in Move is easy and very similar to other programming languages:

  • Add: x + y

  • Subtract: x - y

  • Multiply: x * y

  • Divide: x / y

  • Mod: x % y

  • Exponential: x ^ y

circle-info

Note: This is integer math, which means results are rounded down.

For example, 5 / 2 = 2

To make sure our Dinosaur's Gendna is only 10 digits, let's make another u64 value equal to 10^10. That way we can later use the modulus operator % to create valid Gendna codes from any randomly generated numbers.

Create another integer value named Gendna_modulus in the DinosaurGendna struct, and set it equal to 10 to the power of Gendna_digits

module 0xcafe::Dinosaur_nest {
    struct DinosaurGendna has key {
        Gendna_digits: u64,
        Gendna_modulus: u64, 
    }

    fun init_module(cafe_signer: &signer) {
        move_to(cafe_signer, DinosaurGendna {
            Gendna_digits: 10,
            Gendna_modulus: 10 ^ 10,
        });
    }
}

So far we've seen different types of integers: u8, u32, u64, u128, u256. Although math can be done easily among integers of the same type, it's not possible to do math directly between integers of different types,

Example Below:

Let’s cast Gendna_modulus to u256 with (Gendna_modulus as u256). Remember that the parentheses () are required when typecasting:

The actual contract would look like this: