首页 > Web开发 > 详细

dotnet core 中数值溢出

时间:2018-07-31 12:56:26      阅读:183      评论:0      收藏:0      [点我收藏+]

.net core中使用C#的int类型,存在数值上下限范围,如下:

int max = int.MaxValue;
int min = int.MinValue;
Console.WriteLine($"The range of integers is {min} to {max}");

运行得到结果

The range of integers is -2147483648 to 2147483647


此时如果执行以下代码

int what = max + 3;
Console.WriteLine($"An example of overflow: {what}");

得到范围结果是

An example of overflow: -2147483646

很奇怪,执行max+3得到结果成了min+2

查询官方教程

If a calculation produces a value that exceeds those limits, you have an underflow or overflow condition.

如果计算产生的值超过这些限制,则会出现下溢溢出情况。

max+3发生了下溢,则变为min+2了。

 

这在Python中却是另外一种光景

import sys

print sys.maxint

得到最大值2147483647

然执行以下

print sys.maxint+3

得到2147483650

看到没,没有溢出,原来Python在int超出最大值后,自动将之转为long类型,所以就不存在溢出了,只要内存足够大。

dotnet core 中数值溢出

原文:https://www.cnblogs.com/lnkDel/p/9395026.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!