Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

math/big: cannot calculate log16 of a 256 bit integer #28300

Closed
thisismohitgupta opened this issue Oct 21, 2018 · 4 comments
Closed

math/big: cannot calculate log16 of a 256 bit integer #28300

thisismohitgupta opened this issue Oct 21, 2018 · 4 comments

Comments

@thisismohitgupta
Copy link

thisismohitgupta commented Oct 21, 2018

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

Does this issue reproduce with the latest release?

What operating system and processor architecture are you using (go env)?

What did you do?

If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.

What did you expect to see?

how it works in python...

import math 
a = 0xc6d86e5a2cb4bc532361c2d4940f0b1a0138066e25d65c1c530d080b11f8ca24
a>>89940344608680314083397671686667731393131665861770496634981932531495305005604L
math.log(a)/math.log(16.0)

63.908875905794794

What did you see instead?

does not work for Big Ints

s := "c6d86e5a2cb4bc532361c2d4940f0b1a0138066e25d65c1c530d080b11f8ca24" // Hex value
i := new(big.Int)
i.SetString(s, 16)
// how to get the log with base 16 for a math/big Int variable.
@ALTree
Copy link
Member

ALTree commented Oct 21, 2018

There's no Log for big.Float in the standard library. You'll have to use an external library. For example, using mine (ALTree/bigfloat):

package main

import (
	"fmt"
	"math/big"

	"github.com/ALTree/bigfloat"
)

func main() {
	x := new(big.Float).SetPrec(256)
	x.SetString("89940344608680314083397671686667731393131665861770496634981932531495305005604")

	log16 := bigfloat.Log(big.NewFloat(16.0).SetPrec(256))
	logX := bigfloat.Log(x)

	res := new(big.Float).SetPrec(256)
	fmt.Printf("%g\n", res.Quo(logX, log16))
}
$ go run test.go
63.908875905794799149011030723455843229394283193466612998787786375106246936971

@ALTree
Copy link
Member

ALTree commented Oct 21, 2018

(There was a proposal to integrate a few additional operations (Log, Pow) into the standard library, but it was rejected: #14102).

@ALTree ALTree changed the title Cannot calculate log16 of a 256 bit Integer in Golang math/big: cannot calculate log16 of a 256 bit integer Oct 21, 2018
@thisismohitgupta
Copy link
Author

Thank You. That was exactly what I was looking for.

@ALTree ALTree closed this as completed Oct 21, 2018
@cznic
Copy link
Contributor

cznic commented Oct 21, 2018

Alternative solution

@golang golang locked and limited conversation to collaborators Oct 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants