Can someone look over my program and tell me if i am doing it correctly?
I am accepting user input in the form of 8 hexadecimal digits. I want to interpret those 8 digits as an IEEE 754 32-bit floating point number and will print out information about that number.
here is my output:
IEEE 754 32-bit floating point
byte order: little-endian
>7fffffff
0x7FFFFFFF
signBit 0, expbits 255, fractbits 0x007FFFFF
normalized: exp = 128
SNaN
>40000000
0x40000000
signBit 0, expbits 128, fractbits 0x00000000
normalized: exp = 1
>0
0x00000000
signBit 0, expbits 0, fractbits 0x00000000
+zero
here is the code..
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int HexNumber;
int tru_exp =0;
int stored_exp;
int negative;
int exponent;
int mantissa;
printf("IEEE 754 32-bit floating point");
int a = 0x12345678;
unsigned char *c = (unsigned char*)(&a);
if (*c == 0x78)
{
printf("\nbyte order: little-endian\n");
}
else
{
printf("\nbyte order: big-endian\n");
}
do{
printf("\n>");
scanf("%x", &HexNumber);
printf("\n0x%08X",HexNumber);
negative = !!(HexNumber & 0x80000000);
exponent = (HexNumber & 0x7f800000) >> 23;
mantissa = (HexNumber & 0x007FFFFF);
printf("\nsignBit %d, ", negative);
printf("expbits %d, ", exponent);
printf("fractbits 0x%08X", mantissa);
// "%#010x, ", mantissa);
if(exponent == 0)
{
if(mantissa != 0)
{
printf("\ndenormalized ");
}
}
else{
printf("\nnormalized: ");
tru_exp = exponent - 127;
printf("exp = %d", tru_exp);
}
if(exponent == 0 && mantissa == 0 && negative == 1)
{
printf("\n-zero");
}
if(exponent ==0 && mantissa == 0 && negative == 0)
{
printf("\n+zero");
}
if(exponent == 255 && mantissa != 0 && negative == 1)
{
printf("\nQNaN");
}
if(exponent == 255 && mantissa != 0 && negative == 0)
{
printf("\nSNaN");
}
if(exponent == 0xff && mantissa == 0 && negative == 1)
{
printf("\n-infinity");
}
if(exponent == 0xff && mantissa == 0 && negative == 0)
{
printf("\n+infinity");
}
printf("\n");
}while(HexNumber != 0);
return 0;
}
I dont think the de normalized is right?
The IEEE 754 standard specifies two precisions for floating-point numbers. Single precision numbers have 32 bits − 1 for the sign, 8 for the exponent, and 23 for the significand. The significand also includes an implied 1 to the left of its radix point.
So there are 2^32 - 2^25 = 4261412864 distinct normal numbers in the IEEE 754 binary32 format.
IEEE single-precision floating point computer numbering format, is a binary computing format that occupies 4 bytes (32 bits) in computer memory. In IEEE 754-2008 the 32-bit base 2 format is officially referred to as binary32. It was called single in IEEE 754-1985.
IEEE floating point numbers have three basic components: the sign, the exponent, and the mantissa.
Generally, you're pretty close. Some comments:
0x7fffffff
is a quiet NaN, not a signaling NaN. The signbit does not determine whether or not a NaN is quiet; rather it is the leading bit of the significand (the preferred term for what you call "mantissa") field. 0xffbfffff
is a signaling NaN, for example.Edit: interjay correctly points out that this encoding isn't actually required by IEEE-754; a platform is free to use a different encoding for differentiating quiet and signaling NaNs. However, it is recommended by the standard:
A quiet NaN bit string should be encoded with the first bit of the trailing significand field T being 1. A signaling NaN bit string should be encoded with the first bit of the trailing significand field being 0.
Infinities and NaNs usually aren't called "normal numbers" in the IEEE-754 terminology.
Your condition for calling a number "denormal" is correct.
For normal numbers, it would be nice to add the implicit leading bit when you report the significand. I personally would probably print them out in the C99 hex notation: 0x40000000
has a significand (once you add the implicit bit) of 0x800000
and an exponent of 1
, so becomes 0x1.000000p1
.
I'm sure some aging PDP-11 hacker will give you a hard time about "big endian" and "little endian" not being the only two possibilities.
Edit Ok, example of checking for qNaN on platforms that use IEEE-754's recommended encoding:
if (exponent == 0xff && mantissa & 0x00400000) printf("\nqNaN");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With