简体   繁体   English

有没有理由使用Math.pow()与文字而不是结果本身?

[英]Is there any reason to use Math.pow() with literals rather than the result itself?

I'm currently looking at existing code. 我目前正在查看现有代码。 There are many methods that have been written to model existing formulas. 编写现有公式的方法有很多种。 I stumbled over dozens of instances of code like Math.pow(10.0D, 3.0D) , Math.pow(300.0D, 2.0D) and also stuff like 1.5D * 1000.0D . 我偶然发现了几十个代码实例,如Math.pow(10.0D, 3.0D)Math.pow(300.0D, 2.0D)以及类似1.5D * 1000.0D

Now, before I go ahead and replace all these with their respective results ( 1000d , 90000d , 1500d for the examples above), I wanted to figure out if there are any good reasons to keep with the original notation? 现在,在我继续将所有这些替换为各自的结果(上面的示例为1000d90000d1500d )之前,我想弄清楚是否有任何1500d的理由来保持原始符号?

The only thing that came to my mind is to preserve the resemblance of the code to the modeled formulas. 我想到的唯一一件事就是保持代码与建模公式的相似性。 Are there any other reasons I fail to see? 还有其他原因我看不到吗?

It's essentially all about readability. 它基本上都是关于可读性的。 You'll see this sort of code style more commonly when dealing with time, for example: 在处理时间时,您会更常见到这种代码样式,例如:

Thread.sleep(5 * 60 * 1000);

This style makes it more obvious that the thread will sleep for 5 minutes. 这种风格使线程更加明显地睡了5分钟。 When dealing with mathematical formulas, it's common to keep each value of the formula intact so it's obvious what formula is being used and that it is being used correctly. 在处理数学公式时,通常会保持公式的每个值都保持不变,因此显而易见的是使用了哪个公式并且正确使用了公式。

Programmers like to write things out long hand for the sake of readability: knowing, for example, that there are 86400 seconds in a normal day really belongs to attendees of pub quizzes, not programmers. 程序员喜欢为了可读性而长时间地写出东西:例如,知道在正常的一天有86400秒确实属于酒吧测验的参与者,而不是程序员。

But that said, I'd be inclined to not use Math.pow for this. 但是那样说,我倾向于使用Math.pow

The chief reason being that it's probably not a compile time evaluable constant expression , although that could depend on the compiler. 主要原因是它可能不是编译时可评估的常量表达式 ,尽管这可能取决于编译器。

You might find that pow(x, y) is implemented as exp(y log x) : this can "go off" for surprisingly trivial-looking values of x and y due to a floating point double being only accurate to around 15 decimal significant figures. 您可能会发现pow(x, y)实现为exp(y log x) :由于浮点double仅精确到15左右,因此可以“关闭” xy令人惊讶的微不足道的值图。

(Currently the JLS specifies only one Math.pow function which takes two double arguments. If you were to use integral literals, then the compiler would automatically convert them to double types prior to calling the function. It appears that the author is using double literals to guard against the possibility of future overloads of Math.pow being introduced.) (目前JLS只指定了一个Math.pow两个double参数的Math.pow函数。如果你要使用整数文字,那么编译器会在调用函数之前自动将它们转换为double类型。看来作者正在使用double文字防止将来引入Math.pow超载的可能性。)

In your particular cases, I'd consider replacing Math.pow(300.0D, 2.0D) with 300.0 * 300.0 and Math.pow(10.0D, 3.0D) with 10.0 * 10.0 * 10.0 . 在您的特定情况下,我会考虑将Math.pow(300.0D, 2.0D)替换为300.0 * 300.0 ,将Math.pow(10.0D, 3.0D)替换为10.0 * 10.0 * 10.0 But do check that these values are identical to the original ones; 但请检查这些值是否与原始值相同; and investigate the impact of any discrepancies carefully. 并仔细调查任何差异的影响。

It could make it easier to verify that a formula was translated correctly into code. 它可以更容易验证公式是否已正确转换为代码。 Consider also whether some of these numeric literals should be replaced with named constants to further improve readability. 还要考虑是否应该用命名常量替换其中一些数字文字,以进一步提高可读性。 Without knowing the context, it looks a lot of "magic numbers". 在不了解背景的情况下,它看起来很多“神奇的数字”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM