-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support math pow for primitive types
- Loading branch information
Showing
3 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package math | ||
|
||
type uint8 uint8 | ||
type uint16 uint16 | ||
type uint32 uint32 | ||
type uint64 uint64 | ||
type uint128 uint128 | ||
type uint256 uint256 | ||
|
||
// x ** y | ||
func (x uint8) pow(y uint64) uint256 { | ||
if y == 0 { | ||
return 1u256 | ||
} | ||
ret := uint256(x) | ||
for i := 1; i < y; i += 2 { | ||
ret *= ret | ||
} | ||
mod := y % 2 | ||
if mod == 1 { | ||
ret *= uint256(x) | ||
} | ||
return ret | ||
} | ||
|
||
func (x uint16) pow(y uint64) uint256 { | ||
if y == 0 { | ||
return 1u256 | ||
} | ||
ret := uint256(x) | ||
for i := 1; i < y; i += 2 { | ||
ret *= ret | ||
} | ||
mod := y % 2 | ||
if mod == 1 { | ||
ret *= uint256(x) | ||
} | ||
return ret | ||
} | ||
|
||
func (x uint32) pow(y uint64) uint256 { | ||
if y == 0 { | ||
return 1u256 | ||
} | ||
ret := uint256(x) | ||
for i := 1; i < y; i += 2 { | ||
ret *= ret | ||
} | ||
mod := y % 2 | ||
if mod == 1 { | ||
ret *= uint256(x) | ||
} | ||
return ret | ||
} | ||
|
||
func (x uint64) pow(y uint64) uint256 { | ||
if y == 0 { | ||
return 1u256 | ||
} | ||
ret := uint256(x) | ||
for i := 1; i < y; i += 2 { | ||
ret *= ret | ||
} | ||
mod := y % 2 | ||
if mod == 1 { | ||
ret *= uint256(x) | ||
} | ||
return ret | ||
} | ||
|
||
func (x uint128) pow(y uint64) uint256 { | ||
if y == 0 { | ||
return 1u256 | ||
} | ||
ret := uint256(x) | ||
for i := 1; i < y; i += 2 { | ||
ret *= ret | ||
} | ||
mod := y % 2 | ||
if mod == 1 { | ||
ret *= uint256(x) | ||
} | ||
return ret | ||
} | ||
|
||
func (x uint256) pow(y uint64) uint256 { | ||
if y == 0 { | ||
return 1u256 | ||
} | ||
ret := uint256(x) | ||
for i := 1; i < y; i += 2 { | ||
ret *= ret | ||
} | ||
mod := y % 2 | ||
if mod == 1 { | ||
ret *= uint256(x) | ||
} | ||
return ret | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import ( | ||
"math" | ||
) | ||
|
||
func u8() { | ||
a := 10u8.pow(3) | ||
if a != 1000u256 { | ||
panic("error") | ||
} | ||
} | ||
|
||
func u16() { | ||
a := 10u16.pow(2) | ||
if a != 100u256 { | ||
panic("error") | ||
} | ||
} | ||
|
||
func u32() { | ||
a := 10u32.pow(2) | ||
if a != 100u256 { | ||
panic("error") | ||
} | ||
} | ||
|
||
func u64() { | ||
a := 10u64.pow(2) | ||
if a != 100u256 { | ||
panic("error") | ||
} | ||
} | ||
|
||
func u128() { | ||
a := 10u128.pow(2) | ||
if a != 100u256 { | ||
panic("error") | ||
} | ||
} | ||
|
||
func u256() { | ||
a := 10u256.pow(2) | ||
if a != 100u256 { | ||
panic("error") | ||
} | ||
} | ||
|
||
func main() { | ||
u8() | ||
u16() | ||
u32() | ||
u64() | ||
u128() | ||
u256() | ||
return 0 | ||
} |