roAl
roAl

Reputation: 193

How to get the natural log of a big integer

I am trying to convert a string to integer and then to calculate its log. My first approach was to convert the string using strconv library, but I got an error about the length of the string to be converted.

After that, I used math/big library which worked fine. Now I am not able to apply math.Log()on the resulted big integer.

Code:

package main

import (
    "fmt"
    "math"
    "math/big"
)

func main() {
    bb := "11948904162160164791281681976941230184120142151411311314211115130161285142991119211447"
    bi := big.NewInt(0)
    if _, ok := bi.SetString(bb, 10); ok {
        fmt.Println(math.Log(bi))
    } else {
        fmt.Printf("error parsing line %#v\n", bb)
    }

}

Error:

cannot use bi (type *big.Int) as type float64 in argument to math.Log

Upvotes: 3

Views: 1449

Answers (1)

Gabriel Jablonski
Gabriel Jablonski

Reputation: 953

There are very few situations in which you'd need a precision greater than the one provided by the standard float64 type.

But just to satisfy any "midnight crazy ideas" (or even some very in-depth scientific research!) anyone might run into, Rob Pike's implementations of some operations with big floats are probably the best you can get right now with Go. The log function can be found here.

Upvotes: 1

Related Questions