将 10 的幂写成常数 [英] Writing powers of 10 as constants compactly

查看:40
本文介绍了将 10 的幂写成常数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读最近发布的 Go 编程语言,到目前为止,它一直很有趣(Brian Kernighan 是作者之一,无论如何,除了卓越之外,我不会期待任何其他东西.

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 than excellence anyway).

我在第 3 章遇到了以下练习:

I've come across the following exercise on chapter 3:

练习 3.13尽可能紧凑地为 KB、MB 到 YB 编写 const 声明.

Exercise 3.13 Write const declarations for KB, MB, up through YB as compactly as you can.

(注意:在此上下文中,KB、MB 等表示 1000 的幂)

(NOTE: in this context, KB, MB, etc, denote powers of 1000)

这之前有一节介绍了 iota 作为一种有用的常量生成器机制;特别是,上一段展示了一种将 1024 的幂定义为常量的简洁的方式:

This is preceded by a section where iota is introduced as a useful constants generator mechanism; in particular, the previous paragraph shows a nice and compact way to define the powers of 1024 as constants:

const (
    _ = 1 << (10 * iota)
    KiB
    MiB
    GiB
    TiB
    PiB
    EiB
    ZiB
    YiB
)

作者进一步提到了 10 的幂:

The authors further mention this regarding powers of 10:

iota 机制有其局限性.例如,不可能生成更熟悉的 1000 的幂(KB、MB 等),因为没有求幂运算符.

The iota mechanism has its limits. For example, it's not possible to generate the more familiar powers of 1000 (KB, MB, and so son) because there is no exponentiation operator.

我正在努力完成这个练习,因为看起来预期的解决方案比简单地手工拼出 1000 的幂要复杂一些(尤其是因为它出现在 iota 引入之后).我觉得有一些巧妙的方法可以做到这一点,以一种微妙的方式使用 iota 并结合其他东西.

I'm struggling with this exercise because it looks like the expected solution is something a little more elaborate than simply spelling out the powers of 1000 by hand (especially since it appears after iota is introduced). I feel like there is some clever way to do this that uses iota in a subtle way combined with something else.

我想过找到一种系统的方法来减去多余的".从 1024 的每个权力中提取 1000 的权力,但这让我无处可去.然后我查看了二进制表示,试图推断出 iota 可能有用的一般模式,但同样,我什么也没得到.

I thought about finding a systematic way to subtract the "excess" amount out of each of the powers of 1024 to get the powers of 1000, but it led me to nowhere. Then I looked at the binary representations to try and infer a general pattern where iota could be useful, but again, I got nothing.

我真的看不出如果没有指数运算符,如何从单个递增值 (iota) 中生成 1000 的幂.

I really can't see how one would generate powers of 1000 out of a single incrementing value (iota) without an exponentiation operator.

有什么想法吗?

推荐答案

我会说这是不可能的,因为你想要的是表示一个函数 10^(3i) where i 是一些函数 f(i) 的正整数,其中 f 是基本 go 函数(+、-、/、*)的合成函数.

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 (+, -, /, *).

2^(10i) 之所以可能,只是因为 Go 引入了另一个初等函数整数幂运算.所以如果 1 <<y 将允许 y 浮动,您将能够修改代码以使用 1 <<(log2(10) * 3 * i).这会起作用,因为这相当于解决 10^(3i) = 2^y.取两边的log2 y = log2(10) * 3 * i.

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.

这篇关于将 10 的幂写成常数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆