简体   繁体   English

.NET与Mono:从2 ^ 32转换为int的不同结果

[英].NET vs Mono: different results for conversion from 2^32 as double to int

TL;DR Why does the (int)Math.Pow(2,32) return 0 on Mono and Int32.MinValue on .NET ? TL; DR为什么(int)Math.Pow(2,32)在Mono上返回0 ,而在.NET上返回Int32.MinValue


While testing my .NET-written code on Mono I've stumbled upon the following line: 在Mono上测试.NET编写的代码时,我偶然发现以下行:

var i = X / ( (int)Math.Pow(2,32) ); well that line doesn't make much sense of course and I've already changed it to long . 那条线当然没有多大意义,我已经将它更改为long

I was however curious why didn't my code throw DivideByZeroException on .NET so I've checked the return value of that expression on both Mono and .NET 但是我很好奇为什么我的代码没有在.NET上抛出DivideByZeroException ,所以我检查了Mono和.NET上该表达式的返回值

Can anyone please explain me the results? 谁能解释一下结果?

IMHO the question is academic; 恕我直言,这个问题是学术性的; the documentation promises only that "the result is an unspecified value of the destination type" , so each platform is free to do whatever it wants. 该文档仅承诺“结果是目标类型的未指定值” ,因此每个平台都可以自由地执行其所需的任何操作。

One should be very careful when casting results that might overflow. 投射可能溢出的结果时应格外小心。 If there is a possibility of that, and it's important to get a specific result instead of whatever arbitrary implementation a given platform has provided, one should use the checked keyword and catch any OverflowException that might occur, handling it with whatever explicit behavior is desired. 如果有这种可能性,并且获得特定结果而不是给定平台提供的任何任意实现很重要,则应使用checked关键字并捕获可能发生的任何OverflowException ,以所需的任何显式行为对其进行处理。

"the result is an unspecified value of the destination type". “结果是目标类型的未指定值”。 I thought it would be interesting to see what's actually happening in the .NET implementation. 我认为查看.NET实现中实际发生的事情会很有趣。

It's to do with the OpCodes.Conv_I4 Field in IL: " Conversion from floating-point numbers to integer values truncates the number toward zero. When converting from a float64 to a float32, precision can be lost. If value is too large to fit in a float32 (F), positive infinity (if value is positive) or negative infinity (if value is negative) is returned. If overflow occurs converting one integer type to another, the high order bits are truncated" It does once again say overflow is unspecified however. 这与IL中的OpCodes.Conv_I4字段有关: " Conversion from floating-point numbers to integer values truncates the number toward zero. When converting from a float64 to a float32, precision can be lost. If value is too large to fit in a float32 (F), positive infinity (if value is positive) or negative infinity (if value is negative) is returned. If overflow occurs converting one integer type to another, the high order bits are truncated" 会将 " Conversion from floating-point numbers to integer values truncates the number toward zero. When converting from a float64 to a float32, precision can be lost. If value is too large to fit in a float32 (F), positive infinity (if value is positive) or negative infinity (if value is negative) is returned. If overflow occurs converting one integer type to another, the high order bits are truncated"它再次表示溢出是未指定。

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

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