How to work with large integers in Go? -


i need perform operations, such exponentiation , division, on large values of int64 in go, have problems overflow. tried converting them float64, run other problems. here tried.

i have integer variable, had cast float64 use handy math package (https://golang.org/pkg/math).

however, doesn't cast correctly when integer variable big. i'm assuming it's because size bigger float64. ex:

fmt.printf("%f",float64(111111111111111110)) //outputs 111111111111111104.000000 

i'm trying use math.mod, math.pow10, , math.log10. how able following logic, large number shown above?

int(math.mod(float64(123) / math.pow10(1),10))) // gets second digit 

the question not clear me, assume want perform operations on large integers , using float64 try.

in case, right tool math/big package. here how use extract nth decimal digit of int64:

// first digit n=0 func nthdigit(i int64, n int64) int64 {     var quotient big.int     quotient.exp(big.newint(10), big.newint(n), nil)      bigi := big.newint(i)     bigi.div(bigi, &quotient)      var result big.int     result.mod(bigi, big.newint(10))      return result.int64() } 

Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -