简体   繁体   English

打印浮标。 (Powerpc)

[英]Printing floats. (Powerpc)

I am trying to print floats. 我正在尝试打印花车。 Although variadic functions does not work with floats so they are promoted to double. 尽管可变参数函数不适用于浮点数,但它们被提升为两倍。 I can manage to get rid of the warning by casting it to a double. 我可以通过将警告转换为两倍来消除警告。 While printing the results on some powerpc architecture, it gives incorrect value if printed with %f. 在某些Powerpc架构上打印结果时,如果使用%f打印,它将给出不正确的值。 Why ? 为什么呢

Test Code: 测试代码:

 #include <stdio.h>

 #define _echo_size(X) \
        printf ("Sizeof "#X" %u\n", sizeof(X))

int main (void)
{

        float x;
        long usec = 7L;

        _echo_size(float);
        _echo_size(double);
        _echo_size(short);
        _echo_size(int);
        _echo_size(long);
        _echo_size(long long);

        x = ((float) usec) / 2;
        printf("Expected: 3.5 Got: %1.1f\n", (double) x);
        printf("Expected: 3.5 Got: %1d.%.1d\n", (int)x,
                (int)((x-(int)x)*10));
        return 0;
}

X86 system result: X86系统结果:

Sizeof float 4
Sizeof double 8
Sizeof short 2
Sizeof int 4
Sizeof long 8
Sizeof long long 8
Expected: 3.5 Got: 3.5
Expected: 3.5 Got: 3.5

ppc system result: ppc系统结果:

Sizeof float 4
Sizeof double 8
Sizeof short 2
Sizeof int 4
Sizeof long 4
Sizeof long long 8
Expected: 3.5 Got: 0.0  <--- Why this ?
Expected: 3.5 Got: 3.5

Is it a bug in the tool-chain ? 这是工具链中的错误吗? otherwise what is the elegant way to print floats ? 否则,打印浮标的优雅方法是什么?

Oh, DUH! 哦,! Your floating point value for x is being computed correctly, regardless of whether your FPU is doing it or if your compiler is doing it for you in software; 为您的浮点值x 正确计算的,不管你的FPU是否是做什么的,或者如果你的编译器做你的软件; it's just that your printf() implementation apparently doesn't treat %f as a double ... 只是您的printf()实现显然没有将%f视为double ...

Are you on an embedded system with a custom printf() implementation? 您是否在具有自定义printf()实现的嵌入式系统上? If so, it likely wasn't written to handle floating point numbers anyway, since floating point numbers often aren't even used in embedded systems, where programmers often opt to use integer variables & fixed point arithmetic, which is historically much much faster than floating point (and may still be on small embedded systems). 如果是这样,则可能根本不会编写处理浮点数的方法,因为浮点数通常甚至不用于嵌入式系统,在嵌入式系统中,程序员经常选择使用整数变量和定点算法,这在历史上比浮点数(可能仍在小型嵌入式系统上)。

Or, another possibility: Your custom printf() is written to handle floating point numbers, but treats %f as a float , even though your compiler promotes float to double when passing it through ... . 或者,另一种可能性:您的自定义printf() 写入处理浮点数字,但对待%ffloat ,即使你的编译器促进floatdouble穿过时... Find your custom implementation & fix it to pull %f off the stack as a double instead of a float . 寻找你的自定义实现和解决它拉%f从堆栈作为double ,而不是float (Your custom implementation may be getting a warning by trying to do that anyway, which people may have just been ignoring.) Search for va_arg(args,float) in that code and change it to va_arg(args,double) . (您的自定义实现可能会尝试通过执行此操作来得到警告,而人们可能对此一直无视。 va_arg(args,float)在该代码中搜索va_arg(args,float)并将其更改为va_arg(args,double) (Of course args in that example is my own variable name for a va_list ; your variable name might be different.) (当然,该示例中的args是我自己的va_list的变量名;您的变量名可能不同。)

我想这全是关于“大端”和“小端”的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM