Writing powers of 10 as constants compactly

后端 未结 2 1572
花落未央
花落未央 2020-12-18 03:01

I\'m reading the recently released The Go Programming Language, and it\'s been a joy so far (with Brian Kernighan being one of the authors, I wouldn\'t expect anything other

2条回答
  •  無奈伤痛
    2020-12-18 03:56

    I would say that this is impossible because what you want is to represent a function 10^(3i) where i is a positive integer as some function f(i), where f is a compositive function of your elementary go functions (+, -, /, *).

    It was possible for 2^(10i) only because go introduced another elementary function integer exponentiation. So if 1 << y would allow y being float, you would be able to modify your code to use 1 << (log2(10) * 3 * i). This would worked because this is equivalent to solving 10^(3i) = 2^y. Taking log2 of both sides y = log2(10) * 3 * i.

    But sadly enough bitwise shift is an integer operation.

提交回复
热议问题