A few things about division by zero in C(关于 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 中除以零的一些事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:关于 C 中除以零的一些事情
基础教程推荐
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- c++ STL设置差异 2022-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 提升 ASIO 流缓冲 2021-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
