简体   繁体   English

为什么我不能将双精度隐式转换为整数?

[英]Why can't I implicitly convert a double to an int?

You can implicitly convert an int to a double: double x = 5;您可以将 int 隐式转换为 double: double x = 5;

You can explicitly convert an int to a double: double x = (double) 5;您可以显式地将 int 转换为 double: double x = (double) 5;

You can explicitly convert a double to an int: int x = (int) 5.0;您可以显式地将 double 转换为 int: int x = (int) 5.0;

Why can't you implicitly convert a double to an int?为什么不能将双精度隐式转换为整数? : int x = 5.0; : int x = 5.0;

The range of double is wider than int . double的范围比int宽。 That's why you need explicit cast.这就是为什么你需要显式转换。 Because of the same reason you can't implicitly cast from long to int :由于同样的原因,您不能隐式地从longint

long l = 234;
int x = l; // error

Implicit casting is only available when you're guaranteed that a loss of data will not occur as a result of a conversion.隐式转换仅在您保证不会因转换而丢失数据时才可用。 So you could cast an integer to a double , or an integer to a long data type.因此,您可以将integer转换为double ,或将integer转换为long数据类型。 Casting from a double to an integer however, runs the risk of you losing data.但是,从double转换为integer冒丢失数据的风险。

Console.WriteLine ((int)5.5);  
// Output > 5

This is why Microsoft didn't write an implicit cast for this specific conversion.这就是微软没有为这个特定转换编写隐式转换的原因。 The .NET framework instead forces you to use an explicit cast to ensure that you're making an informed decision by explicitly casting from one type to another.相反,.NET 框架强制您使用显式转换,以确保您通过从一种类型显式转换为另一种类型来做出明智的决定。

The implicit keyword is used to declare an implicit user-defined type conversion operator.隐式关键字用于声明隐式用户定义的类型转换运算符。 Use it to enable implicit conversions between a user-defined type and another type, if the conversion is guaranteed not to cause a loss of data.如果保证转换不会导致数据丢失使用它来启用用户定义类型和另一种类型之间的隐式转换

Source > MSDN来源> MSDN

C# follows the lead of Java's type system, which ranks types int -> long -> float -> double , and specifies that any type which appears to the left of another may be cast to it. C# 遵循 Java 的类型系统,它对类型int -> long -> float -> double ,并指定出现在另一个类型左侧的任何类型都可以强制转换为它。 I personally think ranking types in this fashion was a bad idea, since it means that code like long l = getSomeValue(); float f=l; double d=f;我个人认为以这种方式对类型进行排序是一个坏主意,因为这意味着像long l = getSomeValue(); float f=l; double d=f;这样的代码long l = getSomeValue(); float f=l; double d=f; long l = getSomeValue(); float f=l; double d=f; will compile cleanly without a squawk despite a severe loss of precision compared with storing l directly to d ;尽管与将l直接存储到d相比精度损失严重,但仍会干净利落地编译; it has some other unfortunate consequences as well.它还有其他一些不幸的后果。

I suspect Gosling ranked the types as he did in order to ensure that passing a float and a double to a method which is overloaded for (float,float) and (double,double) would use the latter.我怀疑 Gosling 像他那样对类型进行排序,以确保将floatdouble传递给为(float,float)(double,double)重载的方法将使用后者。 Unfortunately, his ranking has the unfortunate side-effect that passing an int or long to a method which is only overloaded for float and double will cause the method to use the float overload rather than the double overload.不幸的是,他的排名有一个不幸的副作用,即将intlong传递给仅对floatdouble重载的方法将导致该方法使用float重载而不是double重载。

C# and .NET follows Java's lead in preferring a long -to- float conversion over a long -to- double ; C# 和 .NET 遵循 Java 的领先优势,优先采用long to- float转换而不是long to- double the one thing which "saves" them from some of the consequent method-overloading horrors is that nearly all the .NET Framework methods with overloads for float and double also have an overload for Decimal , and no implicit conversions exist between Decimal and the other floating-point types.将它们从一些随之而来的方法重载恐怖中“拯救”出来的一件事是,几乎所有具有floatdouble重载的 .NET Framework 方法也有Decimal的重载,并且在Decimal和另一个浮点数之间不存在隐式转换- 点类型。 As a consequence, attempting to pass a long to a method which has overloads for float , double , and Decimal but not long will result in a compilation error rather than a conversion to float .因此,尝试将long传递给具有floatdoubleDecimal重载但不是long将导致编译错误,而不是转换为float

Incidentally, even if the people choosing which implicit conversions to allow had given the issue more thought, it's unlikely that implicit conversions from floating-point types (eg double ) to discrete types (eg int ) would have been permitted.顺便说一句,即使选择允许哪些隐式转换的人更仔细地考虑了这个问题,也不太可能允许从浮点类型(例如double )到离散类型(例如int )的隐式转换。 Among other things, the best integer representation for the result of a computation that yielded 2.9999999999994 would in some cases be 2, and in other cases it would be 3. In cases where a conversion might sometimes need to be done one way and sometimes another, it's generally good for the language to require that the programmer indicate the actual intention.除其他外,产生 2.9999999999994 的计算结果的最佳整数表示在某些情况下为 2,在其他情况下为 3。在有时需要以一种方式进行转换有时需要以另一种方式进行转换的情况下,语言要求程序员表明实际意图通常是好的。

Imagine that you have this piece of code:想象一下,你有这样一段代码:

double x = pi/6;
double y = sin(x);

y would then be 0.5. y 将是 0.5。 Now suppose that x is cast as an integer as follows:现在假设 x 被转换为整数,如下所示:

int x = pi/6;
double y = sin(x);

In this case x would be truncated to 0, and then the sin(0) would be taken, ie y = 0.在这种情况下,x 将被截断为 0,然后将采用 sin(0),即 y = 0。

This is the primary reason why implicit conversion from double to int is not implemented in a lot of strong-typed languages.这就是为什么很多强类型语言没有实现从 double 到 int 的隐式转换的主要原因。

The conversion of int to double actually increases the amount of information in the type, and can thus always be done safely. int 到 double 的转换实际上增加了类型中的信息量,因此总是可以安全地完成。

WHEN YOU CONVERT FROM DOUBLE TO INT IMPLICITLY then you are trying to store a no.当您隐式地从 DOUBLE 转换为 INT 时,您正在尝试存储一个 no。 with large memory into a variable having small memory(downcasting)将大内存转换为具有小内存的变量(向下转换)

double d=4.5;双d=4.5;

int x=d;//error or warning int x=d;//错误或警告

which can be dangerous as you may lose out the information like you may lose the fractional part of a double while storing it in an integer这可能很危险,因为您可能会丢失信息,就像在将其存储为整数时可能会丢失 double 的小数部分一样

whereas that is not the case while storing an int value in a double variable(upcasting).而将 int 值存储在双变量(向上转换)中时情况并非如此。

int x=2;整数 x=2;

double d=x;双 d=x; //correct //正确的

so the compiler doesn't allows to implicitly convert from double to int( or storing double value in an int) because someone might do it unknowingly and expect no loss of data.因此编译器不允许从 double 隐式转换为 int(或将 double 值存储在 int 中),因为有人可能会在不知不觉中这样做并且期望不会丢失数据。 But if you explicitly cast it means that you say to compiler that cast whatever be the danger ,no matter ,i will manage....hope it helps..但是,如果您明确强制转换,则意味着您对编译器说,无论危险如何,我都会处理....希望它有所帮助..

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

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