Skip to content

Instantly share code, notes, and snippets.

@Ultra-Tech-code
Created August 10, 2022 15:18
Show Gist options
  • Save Ultra-Tech-code/93cbcecc053ad36487d1a9eda5594317 to your computer and use it in GitHub Desktop.
Save Ultra-Tech-code/93cbcecc053ad36487d1a9eda5594317 to your computer and use it in GitHub Desktop.
an arithmetic library with examples
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.8;
library arithmetic{
//Add two numbers together
function add(uint num1, uint num2) external pure returns(uint){
uint result = num1 + num2;
return result;
}
//Subtracts two numbers
function subtract(uint num1, uint num2) external view returns(uint){
uint result = num1 - num2;
return result;
}
//Multiply two numbers
function multiply(uint num1, uint num2) external pure returns(uint){
uint result = num1 * num2;
return result;
}
//divide two numbers
function divide(uint num1, uint num2) external pure returns(uint){
uint result = num1 / num2;
return result;
}
//modulo of two numbers
function modulo(uint num1, uint num2) external pure returns(uint){
uint result = num1 % num2;
return result;
}
//Exponential of two numbers
function exponential(uint num1, uint num2) external pure returns(uint){
uint result = num1 ** num2;
return result;
}
}
contract Test {
//using the library
using arithmetic for uint;
// uint results;
uint _num1 = 5;
uint _num2 = 6;
uint public results = _num1.add(_num2);
function getExponent(uint num1, uint num2) external view returns(uint){
return num1.exponential(num2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment