从浮点数向整数转换时的溢出导致严重问题

从位数大的数值类型向位数小的转换时,可能会出现溢出,编译器也常常给出警告。一个强制转换就可以消除这个警告,让工作瞬间就变得轻松很多,但是在放入强制转换之前,应该考虑些什么呢?一旦溢出,又会带来多大的问题呢?这里有一个例子,希望它能带来更多的思考。


1996年6月4日,阿丽亚娜5型运载火箭(Ariane 5)在法国库鲁的欧洲运载火箭发射场发射,37秒后火箭解体并爆炸。火箭的开发费用大约70亿美元,火箭本体及运载的设备价值约5亿美元。两周后的调查报告指出,事故原因来自将火箭的水平速度的64位浮点数转换成16位整数时的溢出。


使用强类型语言时,当数值类型不相同时,转换是必须的。从位数多的数据类型向少的转换有危险,但并不是一定出问题。
阿丽亚娜5型的转换,在4型时就在使用,但4型没有出问题。因为在决定做浮点数到整数的转换前,开发人员做过仔细调查,可以确定浮点数转换后的大小不会超过16位整数的范围,也就是说不会有溢出。但是,在开发5型时,这部分功能被直接拿来使用,而没有意识到5型火箭的水平速度产生的数值要比4型大很多,16位整数类型不能放下转换后的数值,这导致了灾难性的事故。

 

下面是 CNN 的报道。

On 4 June 1996, the maiden flight of the Ariane 5 launcher ended in a failure. Only about 40 seconds after initiation of the flight sequence, at an altitude of about 3700 m, the launcher veered off its flight path, broke up and exploded.

The failure of the Ariane 501 was caused by the complete loss of guidance and altitude information 37 seconds after start of the main engine ignition sequence (30 seconds after lift-off). This loss of information was due to specification and design errors in the software of the inertial reference system.

The internal SRI* software exception was caused during execution of a data conversion from 64-bit floating point to 16-bit signed integer value. The floating point number which was converted had a value greater than what could be represented by a 16-bit signed integer.

这里是事故报告。
http://www-users.math.umn.edu/~arnold/disasters/ariane5rep.html

转载于:https://www.cnblogs.com/os2os/p/6565252.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值