相关疑难解决方法(0)

正确的printf格式说明符为double

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)

c floating-point double printf format-specifiers

451
推荐指数
5
解决办法
96万
查看次数

为什么printf("%f",0); 给出未定义的行为?

该声明

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)

c c++ printf undefined-behavior implicit-conversion

86
推荐指数
7
解决办法
6931
查看次数

为什么不是`printf`中定义的`float`的说明符?

它看起来像它可能是,有(至少在C99),可以适用于长度修改int:%hhd,%hd,%ld%lldsigned char,short,longlong long.甚至还有一个长度修饰符适用于double:%Lf均值long double.

问题是他们为什么省略float?按照模式,它可能是%hf.

c floating-point printf rationale c-standard-library

69
推荐指数
4
解决办法
3392
查看次数

printf和co如何区分float和double

由于它没有强类型,我认为它只是选择了正确的内存大小并根据参数的类型对其进行解释.但是float和double都使用%f,它们的大小不同.

PS我可以看到如何通过将值复制到临时和转换(这是正确的吗?)可以起作用,但是它对scanfs/sscanf有什么作用?

c floating-point printf

44
推荐指数
1
解决办法
2万
查看次数

C编程:当混合数据类型相乘时,float总是自动转换为double吗?

在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来推广浮点数,当它被评估时加倍吗?

c

10
推荐指数
2
解决办法
7374
查看次数

不,真的,浮点数促销何时实际发生?

从另一个问题中,他们谈到了Bjarne Stroustrup如何说,就像整数数据类型比int(例如short)被提升为a一样int,floats被提升为a double.然而,与扩展的扩展不同于扩展int,浮点升级不会以相同的方式发生,而是发生在其他地方.

我知道如果你要计算float + double,float那么在应用double二元运算符(+)之前将转换为a .但是,根据Learncpp.com,这不是浮点促销.这是通常的算术转换.

浮点数促销何时实际发生?

c++ floating-point promotions

9
推荐指数
2
解决办法
660
查看次数

为什么C作为参数传递时会自动将类型float值扩展为double类型?

C Primer Plus中有一句我很难理解的句子.

当C 作为参数传递给任何函数时,C会自动将类型float值扩展为类型double,例如printf().

我感到困惑,因为在以前的内容中,引入了多种数据类型,似乎通过使用适当的数据类型,您可以到达C程序最有效运行的位置.例如,在某些程序中,使用short而不是int可能导致更快的运行而不需要任何功能成本.

因此,为什么在充当参数时会float自动更改double?有人说用这种方式更方便.但是,这种扩张是否符合C的理念?

c

9
推荐指数
0
解决办法
753
查看次数

当传递浮点常量而不是变量时,为什么%f打印较大的值?

在给定的程序中,为什么每个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)

c

9
推荐指数
1
解决办法
219
查看次数

是否有一个printf说明符要求浮点数不是双倍?

当我使用"%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 …

c c++ promotions misra format-specifiers

7
推荐指数
3
解决办法
2522
查看次数

为什么在使用带有 %f printf 格式说明符的 float 时,clang 3.8 会发出警告?

这段代码:

#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)

c clang compiler-warnings

5
推荐指数
1
解决办法
2640
查看次数

printf 不适用于 LLVM IR 中的浮点数

我想将浮点变量的值打印到屏幕上。我printf()在 LLVM IR 代码中声明函数,并且它已成功链接。

每当我打印整数或字符数据类型或字符串时,都会printf()像在 C 代码中打印它们一样将它们正常打印到屏幕上。但是,如果我将 a 传递给floatprintf()它不会打印浮点数,而是打印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)

floating-point printf llvm llvm-ir

3
推荐指数
1
解决办法
1111
查看次数

如何以双精度打印?

我对 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 ?

  • 我需要改变什么才能获得双打作为输出?

c floating-point double

2
推荐指数
1
解决办法
215
查看次数

PostgreSQL 实型在使用 PHP sprintf 打印时丢失整数精度

我已将此表从 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,它们也应该正确显示。

php postgresql precision

1
推荐指数
1
解决办法
2105
查看次数