冷知识 | 为什么招行消费金额排名第一的显示"负数"?

<p style="line-height: 25.6px;">发个旧闻,就当是科普。
</p><section data-role="outer" label="Powered by wxb-style.com" style="font-family: 微软雅黑; font-size: 16px;"><p style="white-space: normal;">
</p><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; white-space: normal;"><span style="font-size: 14px;"><strong>问:</strong>为什么招行消费金额排名第一的显示"负数"?
<strong>答:</strong>因为他/她刷到了2147483647元
</span></p><p style="white-space: normal;"><img class="imagebubble-image" data-ratio="0.6125984251968504" data-src="http://upload-images.jianshu.io/upload_images/2075039-4d68d618330333b1.jpg" data-type="png" data-w="635" style="vertical-align: middle; border-width: 0px; border-style: initial; border-color: initial; cursor: -webkit-zoom-in; transition: all 0.25s ease-in-out;" src="http://upload-images.jianshu.io/upload_images/2075039-4d68d618330333b1.jpg"/>
</p><p style="text-align: center; white-space: normal;"><span style="font-size: 12px; color: #7F7F7F;">招行消费金额排名</span></p><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; white-space: normal;">
<span style="font-size: 14px;">先看下维基百科上对这个数字的解释:
</span></p><p style="white-space: normal;"><img class="imagebubble-image" data-ratio="0.23809523809523808" data-src="http://upload-images.jianshu.io/upload_images/2075039-b3ce818084f81a9c.png" data-type="png" data-w="735" style="vertical-align: middle; border-width: 0px; border-style: initial; border-color: initial; cursor: -webkit-zoom-in; transition: all 0.25s ease-in-out;" src="http://upload-images.jianshu.io/upload_images/2075039-b3ce818084f81a9c.png"/>
</p><p style="text-align: center; white-space: normal;"><span style="font-size: 12px; color: #7F7F7F;">2147483647@wikipedia</span></p><p style="white-space: normal;">
</p><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; white-space: normal;"><span style="font-size: 14px;">我们再来看一段来自维基百科“天书” <span style="font-size: 12px;">(计算机相关专业的剁友请笑而不语)</span>~</span></p><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; white-space: normal;"><strong style="color: rgb(226, 121, 97); text-align: center;">什么是整型</strong><span style="font-size: 14px;"></span></p><section class="_wxb-style" data-tools="135编辑器" data-id="29735" style=" border-width: 0px; border-style: none; border-color: initial; box-sizing: border-box; "><section style="padding-right: 5px; padding-left: 5px; line-height: 10px; color: inherit; border-width: 1px; border-style: solid; border-color: rgb(226, 121, 97); box-sizing: border-box;"><section style="color: inherit; height: 8px; margin-top: -8px; margin-left: 140px; width: 50%; background-color: rgb(254, 254, 254); box-sizing: border-box;" data-width="50%"><section style="width: 8px; height: 8px; border-radius: 100%; line-height: 1; box-sizing: border-box; font-size: 18px; text-decoration: inherit; border-color: rgb(226, 121, 97); display: inline-block; color: inherit; background-color: rgb(226, 121, 97);"></section></section><p style="line-height: 2em; color: rgb(62, 62, 62); font-size: 14px; margin: 15px; box-sizing: border-box;"><span style="font-size: 12px;">在计算机科学中,整数的概念指数学上整数的一个有限子集。它也称为整数数据类型,或简称整型数、整型。通常是程序设计语言的一种基础数据类型,例如java及C 编程语言的int 数据类型,然而这种基础数据类型只能表示有限的整数,其范围受制于电脑的一个字组所包含的比特数所能表示的组合总数。当运算结果超出范围时,即出现演算溢出,微处理器的状态寄存器中的溢出旗标(overflow flag)会被设置,而系统则会产生溢出例外(overflow exception)或溢出错误(overflow error)。</span></p><p style="line-height: 2em; color: rgb(62, 62, 62); font-size: 14px; margin: 15px; box-sizing: border-box;"><span style="font-size: 12px;">电脑可处理带号(signed)及非带号(unsigned)整数,非带号整数不包括负数。由于一般情况下要同时处理正数及负数,带号整数把字组的最高有效比特(msb,即最左边的比特)视为正负号(0代表正,1代表负),而数字则以补码形式编码,以简化二进制运算的逻辑电路。</span></p><section style="background-color: rgb(254, 254, 254); color: inherit; text-align: right; height: 10px; margin-bottom: -4px; margin-left: 25px; width: 65%; box-sizing: border-box;" data-width="65%"><section style="margin-right: auto; margin-bottom: 1px; margin-left: auto; border-radius: 100%; line-height: 1; box-sizing: border-box; text-decoration: inherit; border-color: rgb(226, 121, 97); display: inline-block; height: 8px; width: 8px; color: inherit; background-color: rgb(226, 121, 97);"></section></section></section></section><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; white-space: normal;"><span style="font-size: 14px;">
</span></p><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; white-space: normal;"><span style="font-size: 14px;">看到这里,我猜各位剁友一定更加清(meng)晰(bi)了~ <img data-s="300,640" data-type="png" data-src="http://upload-images.jianshu.io/upload_images/2075039-2ffae3b6efab817f.png" data-ratio="0.9016393442622951" data-w="61" src="http://upload-images.jianshu.io/upload_images/2075039-2ffae3b6efab817f.png"/> 下面我开始说“人话”~</span>
</p><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; white-space: normal;"><span style="font-size: 14px; color: rgb(0, 82, 255);">当一条数据在计算机内生成时,它会占用计算机的一部分可用内存,当数字越大它会需要更多存储位。一台32位机中最大的整型常量就是2,147,483,647(<strong>二十一亿四千七百四十八万三千六百四十七</strong>),它等于2<sup style="box-sizing: border-box;">31 -1 (为什么不是2<sup style="box-sizing: border-box;">32? 因为第一位要放正负号,0代表正,1代表负)。</span></p><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; white-space: normal;"><span style="font-size: 14px;">当排名第1的这位招行持卡人正好刷到2,147,483,647元时,计算机存储的数值为:</span></p><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; white-space: normal;"><span style="font-size: 14px; color: rgb(255, 0, 0);">0</span><span style="font-size: 14px; color: rgb(0, 176, 80);">1111111111111111111111111111111</span><span style="font-size: 14px;"> </span></p><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; white-space: normal;"><span style="font-size: 14px;">(一个“0”后面三十一个“1”)</span></p><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; white-space: normal;"><span style="font-size: 14px;">当他/她再刷1元时,计算机存储为: </span></p><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; white-space: normal;"><span style="font-size: 14px;"><span style="color: rgb(255, 0, 0);">0</span><span style="color: rgb(0, 176, 80);">000000000000000000000000000000</span></span></p><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; white-space: normal;"><span style="font-size: 14px;">( 三十二个“0”) 此时显示消费金额变成了0!</span></p><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; white-space: normal;"><span style="font-size: 14px;">再再刷1元时,计算机存储为: </span></p><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; white-space: normal;"><span style="font-size: 14px; color: #FF0000;">1</span><span style="font-size: 14px; color: #00B050;">000000000000000000000000000000</span><span style="font-size: 14px;"> </span></p><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; white-space: normal;"><span style="font-size: 14px;">(一个“1”后面三十一个0)还是0,因为"-0"还是0</span></p><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; white-space: normal;"><span style="font-size: 14px;">...</span></p><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; white-space: normal;"><span style="font-size: 14px;">再后面刷的,都会“显示”为负数。</span></p><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; white-space: normal;"><span style="font-size: 14px;"><strong>那为什么负数还能显示到排名第一位呢?</strong>
因为只是排名程序用了32位的int, 而后台的程序用的int64,可以存储大得多的数,而不会溢出。int64即有符号 64 位整数数据类型,相当于C++中的long long、 C# 中的 long 和 SQL Server 中的 bigint,可以存储最大2<sup style="box-sizing: border-box;">63-1(+9,223,372,036,854,775,807)的整数。
<span style="font-size: 14px; text-shadow: rgb(255, 255, 0) 2px 2px 10px; background-color: rgb(255, 251, 0);">也就是缩,这位持卡人实际消费的金额是2,147,483,647+2+1,105,940=2,148,589,589元!(21亿多)</span></span></p><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; white-space: normal;"><span style="font-size: 14px;">这么大额的刷卡交易一般是买什么呢?</span></p><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; white-space: normal;"><span style="font-size: 14px;">2014年上海藏家刘益谦刷卡支付2.8亿购买鸡缸杯,已经长羊毛哥大开眼界:</span></p><p style="white-space: normal;"><img class="imagebubble-image" data-ratio="0.8393700787401575" data-src="http://upload-images.jianshu.io/upload_images/2075039-760e63169a328ad1.jpg" data-type="png" data-w="635" style="vertical-align: middle; border-width: 0px; border-style: initial; border-color: initial; cursor: -webkit-zoom-in; transition: all 0.25s ease-in-out;" src="http://upload-images.jianshu.io/upload_images/2075039-760e63169a328ad1.jpg"/>
</p><p style="text-align: center; white-space: normal;"><span style="color: #7F7F7F; font-size: 12px;">百夫长黑卡持卡人</span></p><p style="white-space: normal;">
</p><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; white-space: normal;"><span style="font-size: 14px;">再多10倍的刷卡金额莫非是买了十个鸡缸杯?羊毛哥的脑洞也仅止于此了。直到看到了微博上的波音787:
</span></p><p style="white-space: normal;"><img class="imagebubble-image" data-ratio="0.6642547033285094" data-src="http://upload-images.jianshu.io/upload_images/2075039-c1e3079afa63166b.jpg" data-type="png" data-w="691" style="vertical-align: middle; border-width: 0px; border-style: initial; border-color: initial; cursor: -webkit-zoom-in; transition: all 0.25s ease-in-out;" src="http://upload-images.jianshu.io/upload_images/2075039-c1e3079afa63166b.jpg"/>
</p><p style="text-align: center; white-space: normal;"><span style="font-size: 12px; color: #7F7F7F;">波音787宽体客机被中国买家匿名购买</span></p><p style="white-space: normal;">
</p><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; white-space: normal;"><span style="font-size: 14px;">回顾历史,因为计算机位存储位数不足而发生的事故和错误其实不只一次,众所周知的千年虫问题之外,还有很多案例。</span></p><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; white-space: normal;"><span style="font-size: 14px;"><strong>阿丽亚娜5型运载火箭坠毁:</strong>
1996年6月4日,阿丽亚娜5型运载火箭的首次发射点火后,火箭开始偏离路线,最终被逼引爆自毁,整个过程只有短短30秒。阿丽亚娜5型运载火箭基于前一代4型火箭开发。在4型火箭系统中,对一个水平速率的测量值使用了16位的变量及内存,因为在4型火箭系统中反复验证过,这一值不会超过16位的变量,而5型火箭的开发人员简单复制了这部分程序,而没有对新火箭进行数值的验证,结果发生了致命的数值溢出。发射后这个64位带小数点的变量被转换成16位不带小数点的变量,引发了一系列的错误,从而影响了火箭上所有的计算机和硬件,瘫痪了整个系统,因而不得不选择自毁,4亿美金变成一个巨大的烟花。
</span></p><p style="white-space: normal; text-align: center;"><img class="imagebubble-image" data-ratio="1.4081632653061225" data-src="http://upload-images.jianshu.io/upload_images/2075039-f865311374f4d025.jpg" data-type="png" data-w="196" style="vertical-align: middle; border-width: 0px; border-style: initial; border-color: initial; cursor: -webkit-zoom-in; transition: all 0.25s ease-in-out;" src="http://upload-images.jianshu.io/upload_images/2075039-f865311374f4d025.jpg"/>
</p><p style="text-align: center; white-space: normal;"><span style="font-size: 12px; color: #7F7F7F;">阿丽亚娜5型运载火箭</span></p><p style="white-space: normal;">
</p><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; white-space: normal;"><span style="font-size: 14px;"><strong>PayPal系统错误:</strong>
2013年7月PayPal因系统错误而从用户Chris Reynolds的账号上扣除了92233万亿美元(具体扣除的金额是92,233,720,368,547,940.25美元,比2<sup style="box-sizing: border-box;">63 − 1美分多出了182.18美元,扣除后账户余额为-92,233,720,368,547,800.00美元;相比之下,2012年美国全国的GDP总额“只有”16.24万亿美元[1]
)。PayPal后来更正了这一错误,并承诺将向Chris Reynolds指定的慈善团体提供一份捐助[2]。
</span></p><p style="white-space: normal;"><img class="imagebubble-image" data-ratio="0.5277777777777778" data-src="http://upload-images.jianshu.io/upload_images/2075039-ba708dfc9b48d6cb.jpg" data-type="png" data-w="720" style="vertical-align: middle; border-width: 0px; border-style: initial; border-color: initial; cursor: -webkit-zoom-in; transition: all 0.25s ease-in-out;" src="http://upload-images.jianshu.io/upload_images/2075039-ba708dfc9b48d6cb.jpg"/>
</p><p style="text-align: center; white-space: normal;"><span style="color: #7F7F7F; font-size: 12px;">Chris Reynolds账单</span></p><p style="white-space: normal;">
</p><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; white-space: normal;"><span style="font-size: 14px;">其实羊毛哥想说,错了,就是错了,为什么公开道个歉这么难?</span></p><p style="margin-bottom: 25px; text-align: justify; word-break: break-word; white-space: normal;"><strong><span style="font-size: 12px;">参考资料</span></strong><span style="font-size: 10px;">
[1] 2012年各国GDP统计 -- 世界银行
[2] PayPal Glitch Actually Put Man $92 Quadrillion in the Red. Daily Finance. 2013 [18 September 2013].</span></p><ul class="reading-list unstyled list-paddingleft-2" style="list-style-type: none;"></ul></section>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 151,688评论 1 330
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 64,559评论 1 273
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 101,749评论 0 226
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 42,581评论 0 191
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 50,741评论 3 271
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 39,684评论 1 192
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,122评论 2 292
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,847评论 0 182
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,441评论 0 228
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,939评论 2 232
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,333评论 1 242
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,783评论 2 236
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,275评论 3 220
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,830评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,444评论 0 180
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 34,553评论 2 249
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 34,618评论 2 249

推荐阅读更多精彩内容