-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path31_Library.sol
More file actions
36 lines (30 loc) · 973 Bytes
/
Copy path31_Library.sol
File metadata and controls
36 lines (30 loc) · 973 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
// Libraries are similar to contracts, but you can't declare any state variable and you can't send ether.
// A library is embedded into the contract if all library functions are internal.
// Otherwise the library must be deployed and then linked before the contract is deployed.
library Math {
function sqrt(uint y) internal pure returns (uint z) {
if (y > 3) {
z = y;
uint x = y / 2 + 1;
while (x < z) {
z = x;
x = (y / x + x) / 2;
}
} else if (y != 0) {
z = 1;
}
// else z = 0 (default value)
}
}
contract TestMath {
function testSquareRoot(uint x) public pure returns (uint) {
return Math.sqrt(x);
}
using Math for uint256;
uint256 squareNumber = 16;
function returnSqrt() public view returns(uint256){
return squareNumber.sqrt();
}
}