为什么编译器决定2.3双,而不是十进制?编译器、而不是、十进制

2023-09-03 02:05:06 作者:回到最初的自己 ≈

为什么编译器决定,2.3是双所以这个code不能编译:

Why does the compiler decide that 2.3 is double so this code won't compile:

decimal x;
x = 2.3; // Compilation error - can not convert double to decimal.
x = (decimal) 2.3 // O.k.

为什么编译器不会这样想: 他希望得到一个小数,他给我一个值,该值可以是十进制,所以它的小数!

Why the compiler doesn't think like this: He wants to get a decimal, he give me a value that can be decimal, So it's decimal!

和为什么没有得到编译错误:

And why this doesn't get compilation error:

short x;
x = 23; // O.K.

谁说23不是int?

Who said that 23 isn't an int?

推荐答案

有很多问题在这里。让我们打破他们成小问题。

There are a lot of questions here. Let's break them down into small questions.

为什么是double类型,而不是十进制的字面2.3?

Why is the literal 2.3 of type double rather than decimal?

历史的原因。 C#的设计是类似C的语法语言家族中的一员,因此,它的表象和基本成语是熟悉的人谁使用类似C语言的编程人员。在几乎所有这些语言中,浮点文字被视为的二的没有的十进制的花车,因为公司的C是如何做的最初。

Historical reasons. C# is designed to be a member of the "C-like syntax" family of languages, so that its superficial appearance and basic idioms are familiar to programmers who use C-like languages. In almost all of those languages, floating point literals are treated as binary not decimal floats because that's how C did it originally.

是我设计一种新的语言从头开始我可能会做出暧昧的文字非法的;每浮点字面将必须是双unambigiuously,单或十进制,等

Were I designing a new language from scratch I would likely make ambiguous literals illegal; every floating point literal would have to be unambigiuously double, single or decimal, and so on.

为什么是非法的,一般以隐式转换双人和小数的?

Why is it illegal in general to convert implicitly between double and decimal?

由于这样做可能是一个错误,有两种方式。

Because doing so is probably a mistake, in two ways.

首先,双打和小数有不同范围和不同数量的重presentation错误的 - 即如何不同的是,数量实际上是重新若想在precise数学数量psented $ P $再present。将double转换为十进制,反之亦然是一件危险的事情,你应该确保你正确地做;让你拼出投提请注意的事实,你可能会失去precision或幅度。

First, doubles and decimals have different ranges and different amounts of "representation error" -- that is, how different is the quantity actually represented from the precise mathematical quantity you wish to represent. Converting a double to a decimal or vice versa is a dangerous thing to do and you should be sure that you are doing it correctly; making you spell out the cast calls attention to the fact that you are potentially losing precision or magnitude.

二,双打和小数有非常不同的用法。双打通常用于科学计算,其中1.000000000001和0.99999999999之间的差远小于实验误差小。累积小重presentation错误是无关紧要的。小数通常用于精确的财务计算需要是完全精确到一分钱。混合这两种意外,似乎危险。

Second, doubles and decimals have very different usages. Doubles are usually used for scientific calculations where a difference between 1.000000000001 and 0.99999999999 is far smaller than experimental error. Accruing small representation errors is irrelevant. Decimals are usually used for exact financial calculations that need to be perfectly accurate to the penny. Mixing the two accidentally seems dangerous.

有,当你有这样做的时间;例如,它更容易制定出指数的问题,像抵押贷款摊销或复利计息双打。在这种情况下,我们再一次让你拼写出你是从双以非常清楚地转换成十进制,这是在程序中的一个点,如果你还没有得到它的权利在那里可能发生precision或幅度损失

There are times when you have to do so; for example, it is easier to work out "exponential" problems like mortgage amortization or compounded interest accrual in doubles. In those cases again we make you spell out that you are converting from double to decimal in order to make it very clear that this is a point in the program where precision or magnitude losses might occur if you haven't gotten it right.

为什么是非法的转换双重面值为十进制文字?为什么不只是pretend,这是一个小数文字?

为什么计算机不能进行十进制,计算机为什么用二进制而不是十进制

Why is it illegal to convert a double literal to a decimal literal? Why not just pretend that it was a decimal literal?

C#是不是一个隐藏你的错误,你之类的语言。这是一个告诉你你的错误,这样你就可以解决这些问题之类的语言。如果你的意思是说2.3米你忘了米,那么编译器会告诉你这件事。

C# is not a "hide your mistakes for you" kind of language. It is a "tell you about your mistakes so you can fix them" kind of language. If you meant to say "2.3m" and you forgot the "m" then the compiler should tell you about it.

那么,为什么它是合法的一个整数文字(或任何整型常量)转换为短,字节,等等?

Then why is it legal to convert an integer literal (or any integer constant) to short, byte, and so on?

由于一个整常数,可以检查,看它是否是在正确的范围内,在编译时间。并从一个范围内的整数,以更小的积分型的转换总是精确的;它从来没有失去precision或幅度,不像双/十进制转换。此外,整型常量算术总是处于选中背景下完成的,除非您覆盖与一个未经检查的块,所以没有溢出,甚至危险。

Because an integer constant can be checked to see if it is in the correct range at compile time. And a conversion from an in-range integer to a smaller integral type is always exact; it never loses precision or magnitude, unlike double/decimal conversions. Also, integer constant arithmetic is always done in a "checked" context unless you override that with an unchecked block, so there is not even the danger of overflow.

和它不太可能是整数/短路运算跨越了域边界像双/小数运算。双运算很可能是科学,小数运算很可能是金融。但是,整数和短算术不是每一个都明显依赖于不同的业务领域。

And it is less likely that integer/short arithmetic crosses a "domain" boundary like double/decimal arithmetic. Double arithmetic is likely to be scientific, decimal arithmetic is likely to be financial. But integer and short arithmetic are not each clearly tied to different business domains.

和使之成为您不必写丑不必要code,它蒙上常数权类型法律手段。

And making it legal means that you don't have to write ugly unnecessary code that casts constants to the right types.

因此​​,没有充分的理由是非法,并很好的理由,使之合法。

There is therefore no good reason to make it illegal, and good reasons to make it legal.