Jayden dang Logo

Introducing Moongate: Breathing New Life Into the Attention Economy

Jayden Dang's avatar

AuthorJayden Dang

Publish AtAugust 15, 202409:13 AM
1000 views
999 likes

Integer

Summary

  • Move supports six unsigned integer types: u8, u16, u32, u64, u128, and u256.
  • Direct mathematical operations between different integer types are not allowed.
  • Type casting is necessary when performing operations with different integer types.
  • It's recommended to cast smaller types to larger types to avoid overflow risks.
  • The module demonstrates adding a u8 and a u64 by converting u8 to u64.
  • A test function verifies the addition operation.
  • The code can be tested using the Move test command.

Overview

Move supports six unsigned integer types: u8, u16, u32, u64, u128, and u256 Values of these types range from 0 to a maximum that depends on the size of the type.

Although math can be done easily among integers of the same type, it's not possible to do math directly between integers of different type

rust
1fun plus_two_types(): u64 { 2 let x: u8 = 10; 3 let y: u64 = 60; 4 // This will error 5 x + y // x and y are different types -> failed to compile 6}

To make this expression correct, you need to use two identical data types; we will convert one of the two data types to match the other.

rust
1fun plus_two_types(): u64 { 2 let x: u8 = 10; 3 let y: u64 = 60; 4 (x as u64) + y 5}

One of the things to pay attention to when using type casting, like the code above, is that we should only cast smaller types to larger types, and not the other way around. This helps to limit the risk of overflow.

Code Start

This code defines a module in Move language that includes a function plus_two_types, which adds a u8 and a u64 after converting the u8 to u64. The module also contains a test function test_plus_two_types that verifies the addition operation.

rust
1module movement::integer_module { 2 use std::debug::print; 3 4 fun plus_two_integer(x: u64, y: u64): u64 { 5 x + y 6 } 7 8 fun plus_two_types(x: u8, y: u64): u64 { 9 (x as u64) + y 10 } 11 12 fun integer_type() { 13 let _a: u8 = 0; 14 let _b: u16 = 1; 15 let _c: u32 = 2; 16 let _d: u64 = 3; 17 let _e: u128 = 4; 18 let _f: u256 = 5; 19 } 20 21 #[test] 22 fun test_plus_two_types() { 23 let result = plus_two_types(5, 100); 24 print(&result); 25 } 26 27 #[test] 28 fun test_show_interger() { 29 integer_type(); 30 } 31 32 #[test] 33 fun test_plus_two_integer() { 34 let result = plus_two_integer(5, 100); 35 print(&result); 36 } 37}

Execute test in the terminal

bash
1movement move test --filter integer_module
json
1Running Move unit tests 2[debug] 105 3[ PASS ] 0xc103109311944d8bae02fccf9273dcb4615be30653e85aec628f04b6ddd00fef::integer_module::test_plus_two_integer 4[debug] 105 5[ PASS ] 0xc103109311944d8bae02fccf9273dcb4615be30653e85aec628f04b6ddd00fef::integer_module::test_plus_two_types[ PASS ] 0xc103109311944d8bae02fccf9273dcb4615be30653e85aec628f04b6ddd00fef::integer_module::test_show_interger 6Test result: OK. Total tests: 3; passed: 3; failed: 0 7{ 8 "Result": "Success" 9}
User avatar
Submit 
John Doe
John Doe- 2h ago
Main comment
10
Previous
Next
Footer nè