浮动与双precision [英] float vs. double precision
问题描述
以下code
浮动X = 3.141592653589793238;
双Z = 3.141592653589793238;
的printf(X =%F \\ N,X);
的printf(Z =%F \\ N,Z);
的printf(X =%20.18f \\ n,x)的;
的printf(Z =%20.18f \\ n,Z);
会给你的输出
X = 3.141593
Z = 3.141593
X = 3.141592741012573242
Z = 3.141592653589793116
在这里输出的第三行 741012573242
是垃圾,在第四行 116
是垃圾。做双打总是有16显著的数字,而花车总是有7显著数字?为什么不双打有14显著数字?
在C使用浮点数 IEEE 754 编码。
这类型的编码采用的标志,一个尾数和一个指数。
由于这种编码的,很多数字将有小的变化,以使它们能够被存储
同样,显著数字数量可以稍微,因为它是一个二进制重新presentation,而不是一个小数之一更改 P>
单precision(float)可为您尾数23位,指数的8位和1个符号位。
双击precision(双)为您提供了有效数位52,指数的11位和1个符号位。
The following code
float x = 3.141592653589793238;
double z = 3.141592653589793238;
printf("x=%f\n", x);
printf("z=%f\n", z);
printf("x=%20.18f\n", x);
printf("z=%20.18f\n", z);
will give you the output
x=3.141593
z=3.141593
x=3.141592741012573242
z=3.141592653589793116
where on the third line of output 741012573242
is garbage and on the fourth line 116
is garbage. Do doubles always have 16 significant figures while floats always have 7 significant figures? Why don't doubles have 14 significant figures?
Floating point numbers in C use IEEE 754 encoding.
This type of encoding uses a sign, a significand, and an exponent.
Because of this encoding, many numbers will have small changes to allow them to be stored.
Also, the number of significant digits can change slightly since it is a binary representation, not a decimal one.
Single precision (float) gives you 23 bits of significand, 8 bits of exponent, and 1 sign bit.
Double precision (double) gives you 52 bits of significand, 11 bits of exponent, and 1 sign bit.
这篇关于浮动与双precision的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!