我的visual studio 2013专业人员可能会丢失数据错误 [英] I'm getting possible loss of data error in my visual studio 2013 professional

查看:70
本文介绍了我的visual studio 2013专业人员可能会丢失数据错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<string.h>
#include <iostream>
using namespace std;
class Average{

private:
	void sumFn(int);
	int userValue, sum;
	float avg;

public:
	Average();
	~Average();
	float printAvg();
	void inputVal(int);
};

Average::Average()
{
	/* 
	int uVal[4];
	for (int i = 0; i < 5; i++)
	userValues[i] =  uVal[i] = 00; */
	
	sum = 0;
	userValue = 0;
	avg = 0.0;
}

void Average:: sumFn(int ss){
	sum += ss;
}

Average::~Average()
{
	cout << "***  \nThank you for using Average Software!  ***";
	cout << "\n*** Have a Nice Day  ***!";
}

void Average::inputVal(/* int uVal[4]*/ int uu)
{
	/*for (int i = 0; i < 5; i++){
		userValues[i] = uVal[i];
	}*/
	sumFn(uu);
}

float Average::printAvg()
{
	/*for (int i = 0; i <= 5; i++)

		return (userValues[i]); */
		
	return (avg = (sum/4));
}

int main()
{
	//int v1[4] = 0;
	int v2 = 0;
	//Average av[2]
		Average a2;
	//int uVal[4];

		//a2.~Average();
	
	for (int i = 0; i < 4; i++)
	{
		cout << "\nEnter marks of subject: " << i <<" : ";
		cin >> v2;
		//av[i].inputVal(v2);
		a2.inputVal(v2);

	}// end of for loop

	/* 
	a1.inputVal(v1, v2);
	cout << "\nThe Quotient is:" << d1.printQuote();*/

	cout << "\nThe Average is: " << a2.printAvg()<<endl;
	
	system("pause");

}// end of main



[由Jochen Arndt编辑:添加代码块并删除空行]


推荐答案

这是因为您使用的是单精度 float 类型。要避免错误,请将所有变量,操作和返回值更改为 double 或投射值。



在您的使用必须转换为 float 的整数参数执行操作时发生错误的代码。整数可以表示数字多于单精度浮点值可以保持的数字。最大32位有符号整数值 INT_MAX 是2147483647,有十位十进制数。但单精度浮点值的尾数只有24位(只能包含6位十进制数字,参见 FLT_DIG FLT_MANT_DIG float.h 中)。因此,将大整数值转换为 float 时,某些数字可能会丢失。
This is because you are using the single precision float type. To avoid the error, change all variables, operations and return values to double or cast values.

In your code the error occurs when performing operations with integer arguments that must be converted to float. Integers can represent numbers with more digits than can be hold by single precision floating values. The max. 32-bit signed integer value INT_MAX is 2147483647 which has ten decimal digits. But the mantissa of single precision floating values has only 24 bits (can only hold 6 decimal digits, see definition of FLT_DIG and FLT_MANT_DIG in float.h). So some digits may get loss when converting large integer values to float.


您可能会丢失一些小数。更改从

You are possibly loosing some decimals. Change from
Quote:

return(avg =(sum / 4));

return (avg = (sum/4));



to


to

return (avg = ((float)sum/4));





值得注意的是,您的代码可能更简洁,例如< br $> b $ b



It is worthy noting your code could be more concise, e.g.

#include <iostream>
using namespace std;


class Average
{
  int sum, items;
public:
  Average():sum(0), items(0){}
  void add(int v){ sum += v; ++items; }
  double avg()
  {
    if (items==0) throw("sorry, no items");
    return (double)sum/items;
  }
};

int main()
{
  int marks;
  Average a;

  while (true)
  {
    cout << "plaese input marks (-1 to stop): " << endl;
    cin >> marks;
    if ( marks < 0) break;
    a.add(marks);
  };
  try
  {
    cout << "the average is " << a.avg() << endl;
  }
  catch( const char * msg )
  {
    cout << msg << endl;
  }
}


这篇关于我的visual studio 2013专业人员可能会丢失数据错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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