C语言 C:将double转为float,保留小数点精度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3498192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:12:14  来源:igfitidea点击:

C: convert double to float, preserving decimal point precision

cfloating-pointdoublec89

提问by sdfg

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

我想在 C 中将 double 转换为浮点数,但想在不做任何更改的情况下尽可能准确地保留小数点...

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 个数字。我可以在点之后得到这五个数字,就像双倍一样吗?使上述结果如下:

   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)之后的所有数字都将被截断。

回答by dan04

floatand doubledon't store decimal places. They store binaryplaces: floatis (assuming IEEE 754) 24 significant bits (7.22 decimal digits) and double is 53 significant bits (15.95 significant digits).

float并且double不存储小数位。它们存储二进制位:float是(假设 IEEE 754)24 个有效位(7.22 个十进制数字),double 是 53 个有效位(15.95 个有效数字)。

Converting from doubleto floatwill 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.

从转换doublefloat会给你最接近的可能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 doubleapproximation to the nice decimal value you intended, you can write something like:

要获得double您想要的漂亮十进制值的近似值,您可以编写如下内容:

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

回答by Michael Burr

A floatgenerally 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.

Afloat一般有 7 位左右的精度,与小数点的位置无关。因此,如果您希望小数点后有 5 位精度,则需要将数字范围限制在 +/-100 左右的范围内。

回答by C Johnson

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:

浮点数以科学记数法表示为只有七位有效数字的数字乘以代表小数点位置的较大数字。维基百科上关于它的更多信息:

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

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