将10的幂作为常量紧凑地书写

将10的幂作为常量紧凑地书写

本文介绍了将10的幂作为常量紧凑地书写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读最近发布的,迄今为止一直是一种喜悦(Brian Kernighan是其中之一作者,我不会期望任何卓越成就)。



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

注意:在此上下文中,KB,MB等,表示权力为1000)



在前面加入 iota 作为有用的常量生成器机制。特别是,前面的段落展示了一个很好且紧凑的方法来定义1024的幂作为常量:

pre $ const $ $ $ $ b _ = 1 < KiB
MiB
GiB
TiB
PiB
EiB
ZiB






$ b

作者进一步提到了10的权力:

我在这个练习中苦苦挣扎,因为它看起来像预期的解决方案比纯粹用手写出1000的权力更复杂一点(尤其是因为它出现在 iota 被引入)。我觉得有一些聪明的方式来做到这一点,使用 iota 以微妙的方式结合其他东西。



我想过要找到一种有系统的方法来从1024的每个幂中减去超额的数量以获得1000的幂,但它使我无处可去。然后,我看着二进制表示,试图推断出 iota 可能有用的一般模式,但我再一次没有任何结果。



我真的不知道如何在没有幂运算符的情况下从单个递增值( iota )产生1000的幂。



有什么想法?

解决方案

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



仅仅因为引入了另一个基本函数整数指数,所以 2 ^(10i)是可能的。所以如果 1 将允许y浮动,您将能够修改您的代码以使用 1<< (log2(10)* 3 * i)。这会奏效,因为这相当于解决 10 ^(3i)= 2 ^ y 。以双方的log2 y = log2(10)* 3 * i

但是遗憾的是,按位移是一个整数运算。


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).

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

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

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
)

The authors further mention this regarding powers of 10:

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.

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.

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

Any ideas?

解决方案

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.

这篇关于将10的幂作为常量紧凑地书写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 07:37