Math Operations in Move
Move supports familiar math operations with integer-based precision, requiring typecasting for calculations between different unsigned integer types.
Math in Move
Doing math in Move is easy and very similar to other programming languages:
Add:
x + ySubtract:
x - yMultiply:
x * yDivide:
x / yMod:
x % yExponential:
x ^ y
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,
