C编程以十六进制的长双精度打印浮点数

问题描述 投票:0回答:3

我正在创建一个家庭作业计划,目标是试图教会我们工会在概念上如何运作。我相信我理解这个概念,但我的一个输出有困难;长双作为十六进制。

我假设在打印百分号后,任何长期需要大写字母L的假设。例如,要打印一个长双浮点数:

printf(“\ Long double:%Lf \ n”,value.ld);

同样,如果我想用十六进制打印Long Double浮点数,我会使用:

printf(“\ Long double:%Lx”,value.ld);

我教授的输出如下:

Professors Output长双22fde0

我的输出如下:

My Output长双62fe40

差异在十六进制的Long Double的打印值中。这是一个可能因计算机而异的价值,还是我在思考中犯了错误。

我的代码

#include <stdio.h>

// define union data
    union data {
        float f;
        double d;
        long double ld;
    }value; //union variable

// begin main function
int main(void){

    // get initial user input
    printf("Enter data for type float: ");
    scanf(" %f", &value.f);

    puts("\nBreakdown of the element in the union: ");
    printf("Float: %f", value.f);
    printf("\nDouble: %f", value.d);
    printf("\nLong double: %Lf\n", value.ld);

    puts("\nBreakdown in hex: ");
    printf("Float: %x", value.f);
    printf("\nDouble: %x", value.d);
    printf("\nLong double: %Lx", value.ld);

    // get user input
    printf("\n\nEnter data for type double: ");
    scanf(" %lf", &value.d);

    puts("\nBreakdown of the element in the union: ");
    printf("Float: %f", value.f);
    printf("\nDouble: %f", value.d);
    printf("\nLong double: %Lf\n", value.ld);

    puts("\nBreakdown in hex: ");
    printf("Float: %x", value.f);
    printf("\nDouble: %x", value.d);
    printf("\nLong double: %Lx", value.ld);

    // get user input
    printf("\n\nEnter data for type long double: ");
    scanf(" %lf", &value.ld);

    puts("\nBreakdown of the element in the union: ");
    printf("Float: %f", value.f);
    printf("\nDouble: %f", value.d);
    printf("\nLong double: %Lf\n", value.ld);

    puts("\nBreakdown in hex: ");
    printf("Float: %x", value.f);
    printf("\nDouble: %x", value.d);
    printf("\nLong double: %Lx", value.ld);

    return 0;
} // end main function
c
3个回答
1
投票

%x格式说明符期望以十六进制格式打印整数类型。为此传递浮点类型会调用未定义的行为。

您可以使用%a格式说明符以十六进制打印浮点数:

printf("\nLong double: %La", value.ld);

1
投票

这是一个可能因计算机而异的值

当然。目前还不清楚你的教授所谓的“长双十六进制”。如果你这样做,将long double传递给%x说明符,那么你会得到未定义的行为 - %x说明符需要一个整数(或变体)并且如果你传递一个浮点数(或者传递浮点数)会很难预测会发生什么变体)。

如果使用%a说明符(显式用于以十六进制打印浮点数),则获得浮点值的十六进制表示,而不是内存的内容。因此,例如1.5将打印为0x1.8p+0,因为.8是十六进制的.5十进制。

如果你真正想要的是内存的内容,如hex,那么你需要传递%x说明符一个整数。最简单的方法是为你的联合添加适当的整数类型。

union data {
        float f;
        int i;
        double d;
        long int li;
        long double ld;
        long long int lli;
    }value; //union variable

然后,如果你想以双十进制的十六进制存储器内容,例如,只需使用:

// get user input
printf("\n\nEnter data for type double: ");
scanf(" %f", &value.d);

puts("\nBreakdown of the element in the union: ");
printf("Double: %f", value.d);
printf("Hex: %lx", value.li);

顺便说一下,%l...(小写字母)用于整数,%L...(大写字母)用于浮点类型。 long int通常与64位平台上的double大小相同(但不保证是这样!)。 %f,有些令人困惑,是为了double论点。 floats没有说明符 - 它们会自动升级为doubles。


0
投票

请使用union指出的how to print float in hex format in C?

这是我从链接中抓取的内容:

#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <math.h>

int main (void) {

    float pi = (float)M_PI;
    union {
        float f;
        uint32_t u;
    } f2u = { .f = pi };

    printf ("pi (floating point): %f\n", pi);
    printf ("pi (hex representation): 0x%\n", f2u.u);

    return 0;
}

输出:

$ ./bin/float_hex
pi (floating point) : 3.141593
pi (hex representation): 0x40490fdb
© www.soinside.com 2019 - 2024. All rights reserved.