doubleprintf中正确的格式说明符是什么?是它%f还是它%lf?我相信它%f,但我不确定.
#include <stdio.h>
int main()
{
double d = 1.4;
printf("%lf", d); // Is this wrong?
}
Run Code Online (Sandbox Code Playgroud) 该声明
printf("%f\n",0.0f);
Run Code Online (Sandbox Code Playgroud)
打印0.
但是,声明
printf("%f\n",0);
Run Code Online (Sandbox Code Playgroud)
打印随机值.
我意识到我表现出某种未定义的行为,但我无法弄明白为什么具体.
所有位都为0的浮点值仍然有效float,值为0.
float并且int在我的机器上具有相同的大小(如果它甚至相关).
为什么使用整数文字而不是浮点文字printf会导致此行为?
如果我使用PS,可以看到相同的行为
int i = 0;
printf("%f\n", i);
Run Code Online (Sandbox Code Playgroud) 它看起来像它可能是,有(至少在C99),可以适用于长度修改int:%hhd,%hd,%ld和%lld指signed char,short,long和long long.甚至还有一个长度修饰符适用于double:%Lf均值long double.
问题是他们为什么省略float?按照模式,它可能是%hf.
由于它没有强类型,我认为它只是选择了正确的内存大小并根据参数的类型对其进行解释.但是float和double都使用%f,它们的大小不同.
PS我可以看到如何通过将值复制到临时和转换(这是正确的吗?)可以起作用,但是它对scanfs/sscanf有什么作用?
在Steven Prata的书"C Primer Plus"中,有一个关于类型转换的部分,其中"基本规则"部分已在规则1中说明:
Under K&R C, but not under current C, float is automatically converted to double.
Run Code Online (Sandbox Code Playgroud)
http://www.9wy.net/onlinebook/CPrimerPlus5/ch05lev1sec5.html
有人可以解释是什么but not under current C意思?是否有自动转换的C版本和不自动转换的版本?
我正在尝试理解,如果我有一个混合浮点数和双精度数的表达式,我可以依靠C来推广浮点数,当它被评估时加倍吗?
从另一个问题中,他们谈到了Bjarne Stroustrup如何说,就像整数数据类型比int(例如short)被提升为a一样int,floats被提升为a double.然而,与扩展的扩展不同于扩展int,浮点升级不会以相同的方式发生,而是发生在其他地方.
我知道如果你要计算float + double,float那么在应用double二元运算符(+)之前将转换为a .但是,根据Learncpp.com,这不是浮点促销.这是通常的算术转换.
浮点数促销何时实际发生?
C Primer Plus中有一句我很难理解的句子.
当C 作为参数传递给任何函数时,C会自动将类型
float值扩展为类型double,例如printf().
我感到困惑,因为在以前的内容中,引入了多种数据类型,似乎通过使用适当的数据类型,您可以到达C程序最有效运行的位置.例如,在某些程序中,使用short而不是int可能导致更快的运行而不需要任何功能成本.
因此,为什么在充当参数时会float自动更改double?有人说用这种方式更方便.但是,这种扩张是否符合C的理念?
在给定的程序中,为什么每个printfs 得到不同的结果?
#include <stdio.h>
int main()
{
float c = 4.4e10;
printf("%f\n", c);
printf("%f\n", 4.4e10);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它显示以下输出:
44000002048.000000
44000000000.000000
Run Code Online (Sandbox Code Playgroud) 当我使用"%f"说明符snprintf和类型参数时,我得到MISRA类型错误float.
根据我的研究,MISRA是正确的,因为"%f"是一种类型的double.
是否有一个浮点说明符或修饰符将使用float类型参数而不是double?
我正在研究一个嵌入式系统,不想将32位浮点数转换为64位double只是为了取悦该snprintf功能.代码打印到调试/控制台端口,这是转换发生的唯一位置.
对于那些需要代码示例的人:
// This section is for those C++ purists and it fulfills the C++ tag.
#if __cplusplus
#include <cstdio>
#else
#include <stdio.h>
#endif
#define BUFFER_SIZE (128U)
int main(void)
{
char buffer[BUFFER_SIZE];
float my_float = 1.234F;
// The compiler will promote the single precision "my_float"
// to double precision before passing to snprintf.
(void)snprintf(buffer, BUFFER_SIZE, "%10.4f", my_float);
puts(buffer);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我对SO和Web的所有研究都是关于打印浮点值,而不是关于哪些说明符需要float …
这段代码:
#include <stdio.h>
int main(void)
{
float value = 10.10f;
printf("%f\n",value);
}
Run Code Online (Sandbox Code Playgroud)
在 Ubuntu 14.04.1 上使用 clang 3.8 使用 -Weverything 选项编译给出:
clang -Weverything w-double-precision.c
w-double-precision.c:5:17: warning: implicit conversion increases floating-point precision: 'float' to 'double' [-Wdouble-promotion]
printf("%f\n",value);
~~~~~~ ^~~~~
1 warning generated.
Run Code Online (Sandbox Code Playgroud) 我想将浮点变量的值打印到屏幕上。我printf()在 LLVM IR 代码中声明函数,并且它已成功链接。
每当我打印整数或字符数据类型或字符串时,都会printf()像在 C 代码中打印它们一样将它们正常打印到屏幕上。但是,如果我将 a 传递给float,printf()它不会打印浮点数,而是打印0.000000。我检查了多次源代码,语法似乎是正确的。应该是打印的吧2.75!我正在查看这段代码,我完全不明白代码的行为与我编写的代码有何不同。
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
@obj1 = global {i32, float, i8} zeroinitializer
@format_string = constant [10 x i8] c"%i %f %c\0A\00"
declare i32 @printf(i8*, ...)
define i32 @main() {
entry:
%obj1 = load {i32, float, i8}, {i32, float, i8}* @obj1
%obj2 = insertvalue {i32, float, i8} %obj1, i32 44, 0
%obj3 = insertvalue {i32, float, i8} …Run Code Online (Sandbox Code Playgroud) 我对 C 完全陌生,我正在尝试完成一项任务。练习是打印 tan(x),x 从 0 递增到 pi/2。
我们需要在 float 和 double 中打印它。我写了一个似乎有效的程序,但我只打印了浮点数,而我预计会加倍。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
double x;
double pi;
pi = M_PI;
for (x = 0; x<=pi/2; x+= pi/20)
{
printf("x = %lf, tan = %lf\n",x, tan(x));
}
exit(0);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:
为什么我得到浮点数,而我将变量定义为 double 并在 printf 函数中使用 %lf ?
我需要改变什么才能获得双打作为输出?
我已将此表从 Wikipedia复制到 PostgreSQL 数据库中。该列Cultivated land (km2)变为类型为 的列real。然后我使用PHP命令
echo rtrim(rtrim(sprintf('%.10F',$v),'0'),'.');
Run Code Online (Sandbox Code Playgroud)
$v在表格中显示数字 ( )(整数和浮点数),但某些值会失去精度。例如,来自美国的值 1669302 变成了 1669300,这很奇怪,因为我期望精度为 10 位十进制数字。我以为我在保存到real列中时失去了精度,但是将列转换为double precision使差异 (02) 再次出现,所以它在某处。
我认为我不需要双精度,那么如何正确显示真实值?请记住,有些列有小数位,而有些则是bigint,它们也应该正确显示。