C:将double转换为float,保持小数点的精度

时间:2021-05-16 17:07:12

i wanted to convert double to float in C, but wanted to preserve the decimal point exactly as possible without any changes...

我想把double转换成C中的浮点数,但我想在不做任何修改的情况下尽可能精确地保留小数点……

for example, let's say i have

例如,假设我有

   double d = 0.1108;
   double dd = 639728.170000;
   double ddd = 345.2345678

now correct me if i am wrong, i know that floating point precision is about 5 numbers after the dot. can i get those five numbers after the dot exactly as the double had it? so that above results as follows:

如果我错了,请纠正我,我知道浮点精度在点之后大约是5个数字。我能得到那个点后面的5个数吗?以上结果如下:

   float f = x(d);
   float ff = x(dd);
   float fff = x(ddd);

   printf("%f\n%f\n%f\n", f, ff, fff);

it should print

它应该打印

   0.1108
   639728.17000
   345.23456

all digits after the precision limit (which i assume as 5) would be truncated.

在精度限制(我假定为5)之后的所有数字都将被截断。

3 个解决方案

#1


26  

float and double don't store decimal places. They store binary places: float is (assuming IEEE 754) 24 significant bits (7.22 decimal digits) and double is 53 significant bits (15.95 significant digits).

浮点和双精度浮点数不存储小数。它们存储二进制位:float是(假设IEEE 754) 24位有效位(7.22位小数),double是53位有效位(15.95位有效数字)。

Converting from double to float will give you the closest possible float, so rounding won't help you. Goining the other way may give you "noise" digits in the decimal representation.

从double转换为float将使您得到最接近的float,因此舍入对您没有帮助。另一种方法可能会在十进制表示中给你“噪声”数字。

#include <stdio.h>

int main(void) {
    double orig = 12345.67;
    float f = (float) orig;
    printf("%.17g\n", f); // prints 12345.669921875
    return 0;
}

To get a double approximation to the nice decimal value you intended, you can write something like:

为了得到你想要的精确的十进制值的双重逼近,你可以这样写:

double round_to_decimal(float f) {
    char buf[42];
    sprintf(buf, "%.7g", f); // round to 7 decimal digits
    return atof(buf);
}

#2


11  

A float generally has about 7 digits of precision, regardless of the position of the decimal point. So if you want 5 digits of precision after the decimal, you'll need to limit the range of the numbers to less than somewhere around +/-100.

无论小数点位置如何,浮点数一般都有大约7位数的精度。如果你想要在小数点后5位的精度,你需要把数字的范围限制在+/-100左右。

#3


-1  

Floating point numbers are represented in scientific notation as a number of only seven significant digits multiplied by a larger number that represents the place of the decimal place. More information about it on Wikipedia:

浮点数在科学符号中表示为只有7个有效数字乘以一个更大的数字,表示小数点位置。更多关于*的信息:

http://en.wikipedia.org/wiki/Floating_point

http://en.wikipedia.org/wiki/Floating_point

#1


26  

float and double don't store decimal places. They store binary places: float is (assuming IEEE 754) 24 significant bits (7.22 decimal digits) and double is 53 significant bits (15.95 significant digits).

浮点和双精度浮点数不存储小数。它们存储二进制位:float是(假设IEEE 754) 24位有效位(7.22位小数),double是53位有效位(15.95位有效数字)。

Converting from double to float will give you the closest possible float, so rounding won't help you. Goining the other way may give you "noise" digits in the decimal representation.

从double转换为float将使您得到最接近的float,因此舍入对您没有帮助。另一种方法可能会在十进制表示中给你“噪声”数字。

#include <stdio.h>

int main(void) {
    double orig = 12345.67;
    float f = (float) orig;
    printf("%.17g\n", f); // prints 12345.669921875
    return 0;
}

To get a double approximation to the nice decimal value you intended, you can write something like:

为了得到你想要的精确的十进制值的双重逼近,你可以这样写:

double round_to_decimal(float f) {
    char buf[42];
    sprintf(buf, "%.7g", f); // round to 7 decimal digits
    return atof(buf);
}

#2


11  

A float generally has about 7 digits of precision, regardless of the position of the decimal point. So if you want 5 digits of precision after the decimal, you'll need to limit the range of the numbers to less than somewhere around +/-100.

无论小数点位置如何,浮点数一般都有大约7位数的精度。如果你想要在小数点后5位的精度,你需要把数字的范围限制在+/-100左右。

#3


-1  

Floating point numbers are represented in scientific notation as a number of only seven significant digits multiplied by a larger number that represents the place of the decimal place. More information about it on Wikipedia:

浮点数在科学符号中表示为只有7个有效数字乘以一个更大的数字,表示小数点位置。更多关于*的信息:

http://en.wikipedia.org/wiki/Floating_point

http://en.wikipedia.org/wiki/Floating_point