Join our
Discord!
LogoLogo
SupraScan ExplorerStarKey WalletDiscord
MoveVM
  • Network
  • Oracles
  • Automation
  • SupraNova
  • Guides
MoveVM
  • Overview
  • Getting Started
    • Install Supra CLI with Docker
    • Create a Supra Account
    • Create a Move Package
      • Initialize a Package
      • Write a Module
      • Compile and Publish
      • Interact with a Package
    • Create a dApp with StarKey
  • Network Information
  • Token Standards
  • Learn Move 101
    • Getting Started with Move
    • Unsigned Integers in Move
    • Math Operations in Move
    • Using Vectors in Move
    • Reading Resource Data with borrow_global
    • Passing Data in Move: Value vs. Reference
    • Adding Elements with vector::push_back
    • Emitting Events with event::emit
  • Move Book
    • Getting Started
      • Modules and Scripts
      • Move Tutorial
    • Primitive Types
      • Integers
      • Bool
      • Address
      • Vector
      • Signer
      • References
      • Tuples and Unit
    • Basic Concepts
      • Local Variables and Scope
      • Equality
      • Abort and Assert
      • Conditionals
      • While, For, and Loop
      • Functions
      • Structs and Resources
      • Constants
      • Generics
      • Type Abilities
      • Uses and Aliases
      • Friends
      • Packages
      • Package Upgrades
      • Unit Tests
    • Global Storage
      • Structure
      • Operators
    • Reference
      • Standard Library
      • Coding Conventions
  • TypeScript SDK
    • Guides
      • Create Supra Accounts
      • Publish a Package
    • Documentation
    • Repository
  • Rest API
    • Mainnet
      • Accounts
      • Faucet
      • Transactions
      • Block
      • View
      • Consensus
      • Events
      • Tables
    • Testnet
      • Accounts
      • Faucet
      • Transactions
      • Block
      • View
      • Events
      • Tables
  • Developer Resources
    • Supra Dapp Templates
    • Supra Move VS Code Extension
  • Links
    • Supra DevHub
    • SupraScan Block Explorer
    • StarKey Wallet
    • Live Data Feeds
    • Whitepapers
    • Security Audits
    • Supra's Official GitHub
Powered by GitBook
On this page
  • Math in Move
  • 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:
Edit on GitHub
  1. Learn Move 101

Math Operations in Move

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

PreviousUnsigned Integers in MoveNextUsing Vectors in Move

For the latest RPC URL, please refer to the 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

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:

fun mixed_types_math(): u64 {
    let x: u8 = 1;
    let y: u64 = 2;
    // This will fail to compile as x and y are different types. One is u8, the other is u64.
    x + y
}

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

fun mixed_types_math(): u64 {
    let x: u8 = 1;
    let y: u64 = 2;
    // This will fail to compile as x and y are different types. One is u8, the other is u64.
    (x as u64) + y
}

The actual contract would look like this:

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

    fun init_module(cafe_signer: &signer) {
        let Gendna_digits = 10;
        let Gendna_modulus = 10 ^ Gendna_digits;
        move_to(cafe_signer, DinosaurGendna {
            Gendna_digits,
            Gendna_modulus: (Gendna_modulus as u256),
        });
    }
}
Supra Network Information