dsaf415212 2018-10-21 16:27
浏览 245

如何在Golang中计算256位整数的log16

how to get the log with base 16 for a math/big Int variable.

Any help would be great since I am new to Go and came from Python and C environment

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

How it works in python

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

answer turns out to be 63.908875905794794

  • 写回答

3条回答 默认 最新

  • douyan7916 2018-10-21 16:42
    关注

    An interesting property of logarithms is that change-of-base is actually pretty easy.

    log_b (x) = log_a (x) / log_a (b)
    

    So if you want to get log_16 (x), you could use the Log function and do change of base:

    log_e (x) = log_16 (x) / log_16 (e)
    log_16 (e) = approximately 0.36067 
    => log_16 (x) = 0.36067 * log_e (x)
    

    So in Go, I think this would be:

    li := Log(i) * 0.36067
    

    Edit: When I wrote the answer above I did not realize that Log wouldn't work on a Big Int. Reading the Go Github, it looks like this is a requested feature in the language which has not yet been implemented due to lack of a satisfactorily quick solution. From what I read it looks like the best solution for the moment, if you must use a Big Int, is probably a Taylor Series implementation, which in my opinion would be non-trivial to write. The thread I linked to indicates that one or more such implementations may exist but are not necessarily computationally correct.

    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办