分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

dotnet core 中数值溢出

发布时间:2023-09-06 02:07责任编辑:傅花花关键词:暂无标签

.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 sysprint sys.maxint

得到最大值2147483647

然执行以下

print sys.maxint+3

得到2147483650

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

dotnet core 中数值溢出

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

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved