简体   繁体   English

如何设置浮点数的精度

[英]How to set precision of a float

Can someone explain me how to choose the precision of a float with a C function? 有人可以解释一下如何用C函数选择浮点精度吗?

Examples: 例子:

theFatFunction(0.666666666, 3) returns 0.667 theFatFunction(0.666666666, 3)返回0.667

theFatFunction(0.111111111, 3) returns 0.111 theFatFunction(0.111111111, 3)返回0.111

You can't do that, since precision is determined by the data type (ie float or double or long double ). 你不能这样做,因为精度是由数据类型决定的(即floatdoublelong double )。 If you want to round it for printing purposes, you can use the proper format specifiers in printf() , ie printf("%0.3f\\n", 0.666666666) . 如果要将其舍入以进行打印,可以在printf()使用正确的格式说明符,即printf("%0.3f\\n", 0.666666666)

You can't. 你不能。 Precision depends entirely on the data type. 精度完全取决于数据类型。 You've got float and double and that's it. 你有floatdouble ,就是这样。

Floats have a static, fixed precision. 浮子具有静态,固定的精度。 You can't change it. 你无法改变它。 What you can sometimes do, is round the number. 您有时可以做的是围绕数字。

See this page , and consider to scale yourself by powers of 10. Note that not all numbers are exactly representable as floats, either. 请参阅此页面 ,并考虑使用10的幂来扩展自己。请注意,并非所有数字都可以完全表示为浮点数。

Most systems follow IEEE-754 floating point standard which defines several floating point types. 大多数系统遵循IEEE-754浮点标准,该标准定义了几种浮点类型。

On these systems, usually float is the IEEE-754 binary32 single precision type: it has 24-bit of precision. 在这些系统上,通常float是IEEE-754 binary32单精度类型:它具有24位精度。 double is the binary64 double precision type; doublebinary64双精度类型; it has 53-bit of precision. 它具有53位的精度。 The precision in bit numbers is defined by the IEEE-754 standard and cannot be changed. 位数的精度由IEEE-754标准定义,不能更改。

When you print values of floating point types using functions of the fprintf family (eg, printf ), the precision is defined as the maximum number of significant digits and is by default set to 6 digits. 使用fprintf系列的函数(例如, printf )打印浮点类型的值时,精度定义为最大有效位数,默认设置为6位。 You can change the default precision with a . 您可以使用a更改默认精度. followed by a decimal number in the conversion specification. 后跟转换规范中的十进制数。 For example: 例如:

printf("%.10f\n", 4.0 * atan(1.0));  // prints 3.1415926536

whereas

printf("%f\n", 4.0 * atan(1.0));     // prints 3.141593

Precision is determined by the data type (ie float or double or long double). 精度由数据类型决定(即float或double或long double)。

If you want to round it for printing purposes, you can use the proper format specifiers in printf(), ie 如果要将其舍入以进行打印,可以在printf()中使用正确的格式说明符,即

printf("%0.3f\n", 0.666666666)  //will print 0.667 in c

Now if you want to round it for calculating purposes you have to first multiply the float by 10^number of digits then typecast to int , do the calculation and then again typecast to float and divide by same power of 10 现在,如果你想将它舍入为计算目的,你必须首先将浮点数乘以10 ^位数然后将类型转换为int,进行计算然后再次进行类型转换以浮点并除以相同的幂10

float f=0.66666; 
f *= 1000;  // 666.660
int i = (int)f; // 666
i = 2*i;  //  1332
f = i;   //  1332
f /= 1000;  // 1.332
printf("%f",f);  //1.332000

It might be roughly the following steps: 它可能大致是以下步骤:

  1. Add 0.666666666 with 0.0005 (we get 0.667166666) 添加0.666666666和0.0005(我们得到0.667166666)
  2. Multiply by 1000 (we get 667.166666) 乘以1000(我们得到667.166666)
  3. Shift the number to an int (we get 667) 将数字移到int(我们得到667)
  4. Shift it back to float (we get 667.0) 把它转回浮动(我们得到667.0)
  5. Divide by 1000 (we get 0.667) 除以1000(我们得到0.667)

Thank you. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM