如何删除"1.#INF"在Visual C ++中,当您将任何数字除以零时? [英] how to remove "1.#INF" in visual C++ when you divide any number by zero?

查看:154
本文介绍了如何删除"1.#INF"在Visual C ++中,当您将任何数字除以零时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我有一个小问题,我无法弄清楚该如何消除它,就像我在控制台中将任何数字除以一样,它向我显示了 1.#INF 不知道它来自哪里,有什么办法可以消除它.

例如,如果我将4/0除以1,则显示#INF,我知道我们不能除以零,但是如何删除呢?

这是简单的c ++程序:

hello guys,
i have a small question which i cannot figure out how to get rid of that like when ever i divide any number by zero in console it shows me 1.#INF i don''t know where it comes from is there any way to remove this.

for example if i divide 4/0 it shows 1.#INF i know we cannot divide by zero but how can i remove this?

here is the simple c++ program:

#include <iostream>
#include <math.h>

using namespace std;

int main()
{

	double left;
	double right;
	char op;
	
	cin >> left >> op >> right;
	pow(right, left);

	switch (op){
		case ''^'':
			cout << pow(left, right) << endl;
			break;

		case ''+'':
			cout << left + right << endl;
			break;

		case ''-'':
			cout << left - right << endl;
			break;

		case ''*'':
			cout << left * right << endl;
			break;

		case ''/'':
			cout << (left / right) << endl;
			break;
	
		default:
			cout << "Error: invalid operator" << endl;
			break;
	}

	if (op == ''/'' && right == 0){
		cout << "Error: division by zero" << endl;
	}


	system("pause");
	return 0;
}

推荐答案

您不必删除"任何东西,除非您想对自己说谎.相反,您应该面对它:如果您将浮点值定义为零,则结果为-Inf或+ Inf.在整数除以零时,这是无效的运算.您甚至可以使用这些Inf值.例如,您可以使用<,< =,!=,>将+或-Inf与任何其他非NaN值进行比较.或> =运算符并获得有效的比较结果,则可以将其乘以任意数字,甚至更多.只要您了解了它们的含义,这些都是完全有效的值.

这些值,因为所有浮点类型的表示形式都在CPU 指令集中表示,并根据IEEE 754进行了标准化.请参见:
http://en.wikipedia.org/wiki/IEEE_floating_point [ http://en.wikipedia.org/wiki/NaN [ http://en.wikipedia.org/wiki/Infinity#Computing [
—SA
You don''t have to "remove" anything, unless you want to lie to yourself. Instead, you should face it: if you define a floating point value by zero, the result is -Inf or +Inf. In integer division by zero, this is invalid operation. You can even work with those Inf values. For example, you can compare + or -Inf with any other non-NaN value using <, <=, !=, > or >= operators and get a valid result of comparison, you can multiply it by any number, and a lot more. These are perfectly valid values, as soon as you understand their meaning.

These values, as all the presentations of floating-point type are represented in CPUs instruction sets and standardized under IEEE 754. Please see:
http://en.wikipedia.org/wiki/IEEE_floating_point[^],
http://en.wikipedia.org/wiki/NaN[^],
http://en.wikipedia.org/wiki/Infinity#Computing[^].

As you can see, the "normal" floating-point values are complemented with +/-Infinity and NaN (Not a Number) values. If you think well about it, you will find many great uses of all these values.

—SA


您必须先检查0,然后再除以零,然后再除.只需将if-Block放在进行除法并完成操作的行之前即可.

像这样的东西:
You have to check for 0 before you divide by zero, not afterwards. Just put that if-Block before the line where you make the division and you''re done.

something like this:
switch(op)
...
case '/':
	if (right == 0)
	{
		cout << "Error: division by zero" << endl;
	}
	else
		cout << (left / right) << endl;
	
	break;
...


这篇关于如何删除"1.#INF"在Visual C ++中,当您将任何数字除以零时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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