github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/examples/gno.land/p/demo/int256/conversion.gno (about) 1 package int256 2 3 import ( 4 "gno.land/p/demo/uint256" 5 ) 6 7 // SetInt64 sets z to x and returns z. 8 func (z *Int) SetInt64(x int64) *Int { 9 z.initiateAbs() 10 11 neg := false 12 if x < 0 { 13 neg = true 14 x = -x 15 } 16 if z.abs == nil { 17 panic("abs is nil") 18 } 19 z.abs = z.abs.SetUint64(uint64(x)) 20 z.neg = neg 21 return z 22 } 23 24 // SetUint64 sets z to x and returns z. 25 func (z *Int) SetUint64(x uint64) *Int { 26 z.initiateAbs() 27 28 if z.abs == nil { 29 panic("abs is nil") 30 } 31 z.abs = z.abs.SetUint64(x) 32 z.neg = false 33 return z 34 } 35 36 // Uint64 returns the lower 64-bits of z 37 func (z *Int) Uint64() uint64 { 38 return z.abs.Uint64() 39 } 40 41 // Int64 returns the lower 64-bits of z 42 func (z *Int) Int64() int64 { 43 _abs := z.abs.Clone() 44 45 if z.neg { 46 return -int64(_abs.Uint64()) 47 } 48 return int64(_abs.Uint64()) 49 } 50 51 // Neg sets z to -x and returns z.) 52 func (z *Int) Neg(x *Int) *Int { 53 z.abs.Set(x.abs) 54 if z.abs.IsZero() { 55 z.neg = false 56 } else { 57 z.neg = !x.neg 58 } 59 return z 60 } 61 62 // Set sets z to x and returns z. 63 func (z *Int) Set(x *Int) *Int { 64 z.abs.Set(x.abs) 65 z.neg = x.neg 66 return z 67 } 68 69 // SetFromUint256 converts a uint256.Uint to Int and sets the value to z. 70 func (z *Int) SetUint256(x *uint256.Uint) *Int { 71 z.abs.Set(x) 72 z.neg = false 73 return z 74 } 75 76 // OBS, differs from original mempooler int256 77 // ToString returns the decimal representation of z. 78 func (z *Int) ToString() string { 79 if z == nil { 80 panic("int256: nil pointer to ToString()") 81 } 82 83 t := z.abs.Dec() 84 if z.neg { 85 return "-" + t 86 } 87 return t 88 }