在循环中格式化数字以进行计算 [英] Format a number inside a loop for calcuation

查看:66
本文介绍了在循环中格式化数字以进行计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

有没有办法在while循环中格式化动态获得的数字?

例如

  while (csf.ReadString(buffer))
          {
 int 退出;
 out = swscanf_s(buffer,_T(" ),& lat);
 double  latitude = lat;
// 纬度值是1.0652337487801220,但我想将此数字格式化为9个小数位,并将其用于其中的进一步计算环形. 
} 

解决方案

我认为您的意思是取整",而不是格式".
试试这个:

 /// //////////////////////////////////////////////////////////////////////////////////////////////////////////
 // 此类将数字四舍五入到Delta的闭合倍数
/// /////////////////////////////////////////////////////////////////////////////////////////////////////////

模板< typename float_t = "  >
 class 舍入
{
float_t q1;
float_t qh;
float_t q;
公共:
取整(float_t增量):q(增量),q1( 1 . 0 /delta),qh(增量*  0 . 5 ){}

float_t round(IN float_t f)
{
返回 q * floor(q1 *(f + qh));
}
};


#ifdef _DEBUG
 // 类Rounder的单元测试.
结构 TestUtils
{
 void 舍入( void )
{
如果(!stdout)
返回;
printf(" );
舍入< double> milli( 0 . 001 );
舍入< double> penta( 0 . 2 );
舍入< double>八( 0 . 125 );
舍入< double>完整( 1 . 0 );
printf(" );
printf(" );
 for ( int  i =  0 ; i <  10 ; i ++)
{
 double  d = rand()*  1 . 0 /(rand());
printf(" ,
d,milli.round(d),penta.round(d),8.round(d),full.round(d));
}
printf(" );
}
};
#endif  




swscanf_s函数仅将数字格式化为字符串.
格式化,

输出= swscanf_s(缓冲区,_T(" ) ,& lat); 



并且得到9位小数,但这是一个字符串,您无法进行任何数学计算(例如1.123456789 +1->字符串+ int).

要获取数字中的9位小数,可以执行以下操作:

  double 初始=  1 . 123456789123456789 ;
 double  最终 = floor(( 1 .  123456789123456789  *  1000000000 ))/1000000000; 



然后得到final = 1.123456789

请注意,这样做会失去精度.没什么可补充的.

但是,让我问您:为什么要将变量四舍五入到9位小数?通常,最好以尽可能最高的精度进行所有计算,并在输出值以供人工检查时进行四舍五入.

因此,我的建议是:保留来自scanf的变量,然后执行进一步的计算.然后,在生成输出时,请执行以下操作:

double lat = 20.78892736459; // just as an example
...
printf ("Latitude: %10.6f\n", lat);
    // the output result will be rounded to 6 decimals



通常,这比对内部变量进行四舍五入要好.


Hi all,

Is there a way to format a dynamically obtained number inside a while loop?

for example

 while (csf.ReadString(buffer))
          {
int out;
 out = swscanf_s(buffer, _T("%f"), &lat);
double latitude = lat; 
// lat value is 1.0652337487801220, but I want to format this number to 9 decimal places and use this for further calculations inside this loop. 
}

解决方案

I think you mean ''round'', not ''format''.
Try this:

	/////////////////////////////////////////////////////////////////////////////////////////////////////////
	// This class rounds a number to the closes multiple of Delta
	/////////////////////////////////////////////////////////////////////////////////////////////////////////

template <typename float_t="">
class Rounder
{
	float_t q1;
	float_t qh;
	float_t q;
public:
	Rounder(float_t delta): q(delta), q1(1.0 / delta), qh(delta * 0.5) {}

	float_t round(IN float_t f)
	{
		return q * floor( q1 * ( f + qh ) );
	}
};


#ifdef _DEBUG	
// Unit testing for class Rounder.
struct TestUtils
{
	void rounder(void)
	{
		if (!stdout)
			return;
		printf("TestUtils::rounder()\n");
		Rounder<double> milli(0.001);
		Rounder<double> penta(0.2);
		Rounder<double> eight(0.125);
		Rounder<double> full(1.0);
		printf("\n \n   Input      0.001       0.2       0.125       1.0     \n");
		     printf(" ---------- ---------- ---------- ---------- --------- \n");
		for (int i = 0; i < 10; i++)
		{
			double d = rand() * 1.0 / (rand());
			printf(" %10f %10f %10f %10f %10f \n",
				d, milli.round(d), penta.round(d), eight.round(d), full.round(d) );
		}
		printf(" \n \n");
	}
};
#endif


Hi,

swscanf_s function only format the number to string.
to format it,

out = swscanf_s(buffer, _T("%.9f"), &lat);



and you get 9 decimal places, but it is a string, you can not do any math calculations (like 1.123456789 + 1 -> string + int).

To get 9 decimal places in a number, you can do:

double initial = 1.123456789123456789;
double final = floor((1.123456789123456789*1000000000))/1000000000;



and you get final = 1.123456789

notice that you doing this, you will lost precision.


Solution 2 of Pablo is a good answer to your question. Nothing to add to that.

But let me ask you: Why do you want to round your variable to 9 decimals? Normally, it is preferable to do all your calculations with the highest possible precision and do the rounding when outputting values for human inspection.

So my recommendation is: Leave your variable as is comes from scanf and perform your further calculations. Then, when generating output, do things like this:

double lat = 20.78892736459; // just as an example
...
printf ("Latitude: %10.6f\n", lat);
    // the output result will be rounded to 6 decimals



That''s usually way better than doing intermediate rounding of your internal variables.


这篇关于在循环中格式化数字以进行计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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