Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

how to print a double in hex using a single printf in GCC ?

Status
Not open for further replies.
#include <stdio.h>
#include <stdlib.h>
#include <float.h>

int main (void)
{
double d;

d = 2;
printf("Ex 1: 2 in hex: %a\n\n",d);

d = 256;
printf("Ex 2: 2^8 in hex: %a\n\n",d);

d = 0.015625; //= 2^-6
printf("Ex 3: 2^-6 in hex: %a\n\n",d);

d = 0.857421875;
printf("Ex 4: 0.857421875 in hex: %a\n\n",d);

d = DBL_MAX;
printf("Ex 5: DBL_MAX in hex: %a\n\n",d);

d = DBL_MIN; //Smallest double (normalized)
printf("Ex 6: DBL_MIN in hex: %a\n\n",d);

d = 0x1p-1074; //Smallest double (unnormalized)
printf("Ex 7: 0x1p-1074 in hex: %a\n\n",d);

d = 3.1415926;
printf("Ex 8: 3.1415926 in upper case hex: %A\n\n",d);

d = 0.1;
printf("Ex 9: 0.1 in hex: %a\n\n",d);

d = 0x3.3333333333334p-5;
printf("Ex 10: 0x3.3333333333334p-5 in hex: %a\n\n",d);

d = 0xcc.ccccccccccdp-11;
printf("Ex 11: 0xcc.ccccccccccdp-11 in hex: %a\n\n",d);

d = strtod("0x1.999999999999ap-4",NULL);
printf("Ex 12: strtod 0x1.999999999999ap-4 in decimal: %0.1f\n",d);
}

//////////////////////////////////////////////output below
Ex 1: 2 in hex: 0x1p+1

Ex 2: 2^8 in hex: 0x1p+8

Ex 3: 2^-6 in hex: 0x1p-6

Ex 4: 0.857421875 in hex: 0x1.b7p-1

Ex 5: DBL_MAX in hex: 0x1.fffffffffffffp+1023

Ex 6: DBL_MIN in hex: 0x1p-1022

Ex 7: 0x1p-1074 in hex: 0x0.0000000000001p-1022

Ex 8: 3.1415926 in upper case hex: 0X1.921FB4D12D84AP+1

Ex 9: 0.1 in hex: 0x1.999999999999ap-4

Ex 10: 0x3.3333333333334p-5 in hex: 0x1.999999999999ap-4

Ex 11: 0xcc.ccccccccccdp-11 in hex: 0x1.999999999999ap-4

Ex 12: strtod 0x1.999999999999ap-4 in decimal: 0.1


please see Hexadecimal Floating-Point Constants - Exploring Binary for details
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top