[英]How can I define mathematical constants without typing out the number?
I need to use the square root of two, 1.414..., in a loop. 我需要在循环中使用两个平方根1.414 ....
Obviously, I don't want to call the function Math.Sqrt(2) all the time. 显然,我不想一直调用函数Math.Sqrt(2)。 Sure, it's a single instruction on modern processors, and the JIT or compiler will probably figure out what's going on, but I want my code to be clear, readable, and fast.
当然,它是现代处理器上的单一指令,JIT或编译器可能会弄清楚发生了什么,但我希望我的代码清晰,可读,快速。
Because I want my code to be clear and readable, I'd prefer to define this constant by calling sqrt2 = Math.Sqrt(2)
instead of typing in the magic number sqrt2 = 1.4142135623731d
. 因为我希望我的代码清晰可读,所以我更喜欢通过调用
sqrt2 = Math.Sqrt(2)
来定义此常量,而不是输入幻数sqrt2 = 1.4142135623731d
。
Finally, because this value is a constant, I want to declare it with the const
keyword. 最后,因为这个值是常量,我想用
const
关键字声明它。 But when I write: 但是当我写道:
const double sqrt2 = Math.Sqrt(2);
// const double sqrt2 = 1.4142135623731d;
the compiler complains: 编译器抱怨:
Error 1 The expression being assigned to 'sqrt2' must be constant
错误1分配给'sqrt2'的表达式必须是常量
How do I best define this value? 我如何最好地定义这个值?
Math.Sqrt
is a method call, so that cannot be assigned to a compile-time constant. Math.Sqrt
是一个方法调用,因此无法分配给编译时常量。
You can only assign it to a run-time constant : 您只能将其分配给运行时常量 :
static readonly double sqrt2 = Math.Sqrt(2);
You can try to use static readonly
instead of const
. 您可以尝试使用
static readonly
而不是const
。
static readonly double sqrt2 = Math.Sqrt(2);
Do understand that when you declare a constant then it has to be compile time constants. 要明白,当你声明一个常量时,它必须是编译时常量。
A constant expression is an expression that can be fully evaluated at compile time.
常量表达式是一个可以在编译时完全计算的表达式。
I see no reason that you shouldn't initialise it as a constant. 我认为没有理由不将它初始化为常数。
In my opinion, if you use a named constant for a number, then that number is not a magic number. 在我看来,如果你对一个数字使用命名常量,那么这个数字不是一个神奇的数字。
You could create a MathematicalConstants class to hold that, and other values: 您可以创建一个MathematicalConstants类来保存它和其他值:
public static class MathematicalConstants
{
public const double SQRT2 = 1.4142135623731d;
public const double SQRT3 = 1.7320508075689d;
}
Then if you're using C#6 or later, you can put 然后,如果您使用的是C#6或更高版本,则可以使用
using static MathematicalConstants;
at the start of your code module, and you wouldn't need to refer to it as MathematicalConstants.SQRT2
. 在代码模块的开头,您不需要将其称为
MathematicalConstants.SQRT2
。 You could just use SQRT2
in your code: 您可以在代码中使用
SQRT2
:
const double sin45 = 1/SQRT2;
const double tan30 = 1/SQRT3;
Incidentally, there's a precedent for putting constants in static classes. 顺便提一下,有一个先例可以将常量放在静态类中。 The
Math
class has PI
and E
as constants, and they are initialized directly in the source code. Math
类将PI
和E
作为常量,它们直接在源代码中初始化。
In C# constant initializer must be compile const because they are embedded as literals in place of usage during compilation. 在C#中,常量初始化器必须是编译const,因为它们在编译期间作为文字嵌入而不是使用。 This means that every usage of a member marked as
const
will be replaced with its value during compilation, while readonly members will be resolved at run-time. 这意味着标记为
const
的成员的每次使用都将在编译期间替换为其值,而readonly成员将在运行时解析。 You can use static readonly
instead. 您可以使用
static readonly
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.