关于 C 中除以零的一些事情 [英] A few things about division by zero in C

查看:20
本文介绍了关于 C 中除以零的一些事情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
返回值 1.#INF000

我一直认为除以 0 会导致编译后的程序崩溃

I always thought division by 0 would result in a compiled program crashing

但是我今天发现(使用 VC++ 2010 Express)除以 0 给出了一个叫做 1.#INF000 的东西,它应该是正无穷大

However I discovered today (using VC++ 2010 Express) that division by 0 gives something called 1.#INF000 and it is supposed to be positive infinity

当它被传递给一个函数时,它被传递为 -1.#IND000

When it was passed to a function, it got passed as -1.#IND000

这是怎么回事?

在 google 上搜索 1.#INF000 和 -1.#IND000 也没有提供任何明确的解释

Searching 1.#INF000 and -1.#IND000 on google do not provide any clear explanations either

它只是 VC++ 特有的东西吗?

Is it just something specific to VC++ ?

推荐答案

浮点数除以零与整数除以零的行为不同.

Floating point division by zero behaves differently than integer division by zero.

IEEE 浮点标准区分+inf 和-inf,而整数不能存储无穷大.整数除以零会导致未定义的行为.浮点除以零由浮点标准定义,结果为 +inf 或 -inf.

The IEEE floating point standard differentiates between +inf and -inf, while integers cannot store infinity. Integer division by zero results in undefined behaviour. Floating point division by zero is defined by the floating point standard and results in +inf or -inf.

正如 Luchian 所指出的,C++ 实现不需要遵循 IEEE 浮点标准.如果您使用的实现不遵循 IEEE 浮点标准,则浮点除以零的结果未定义.

As pointed out by Luchian, C++ implementations are not required to follow the IEEE Floating point standard. If the implementation you use doesn't follow the IEEE Floating point standard the result of floating point division by zero is undefined.

这篇关于关于 C 中除以零的一些事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆