【发布时间】:2010-12-02 16:50:34
【问题描述】:
我正在尝试使用此代码打印 f/sprintf 浮点数:
sprintf(buff, "FPS: %d\n%.4f N %.4f E\nAl: %.1fm Rl: %.1f\n", fps, p_viewer.p.lat, p_viewer.p.lon, p_viewer.p.alt, p_viewer.roll);
但是当我这样做时会收到这些警告:
gfx_game_engine.c:300: warning: format '%.4f' expects type 'double', but argument 4 has type 'float'
gfx_game_engine.c:300: warning: format '%.4f' expects type 'double', but argument 5 has type 'float'
gfx_game_engine.c:300: warning: format '%.1f' expects type 'double', but argument 6 has type 'float'
gfx_game_engine.c:300: warning: format '%.1f' expects type 'double', but argument 7 has type 'float'
sprintf 浮点数的正确方法是什么?有特殊格式字符吗?我觉得编译器可能会以某种方式强制转换类型,这可能会导致它变慢。
我正在使用 dsPIC33FJ128GP802 微控制器并使用 MPLAB C30(GCC v3.23 的变体)进行编译。
【问题讨论】:
-
%f是 float%lfdouble 和%Lflong double。你用的是什么编译器? -
如果您担心导致运行缓慢的原因,请对其进行分析。
-
@Let_Me_Be GCC (v3.23) 用于 dsPIC33F。 (也称为 Microchip MPLAB C30。)
-
@Nathon 像我这样的小型微控制器几乎没有基本的调试功能,忘记配置文件吧!
-
@Let_Me_Be:根据我来自 glibc6 的 sprintf 手册页,
f和F都是双倍的。
标签: c floating-point printf