tags:

views:

273

answers:

1

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?

+3  A: 

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");
Stephen Canon
+1. Worth a mention that distinguishing qNan/sNan based on the first bit of the significand is correct for Intel and AMD processors, but may be different on other systems since it isn't specified in the standard.
interjay
@interjay: Thanks for catching that!
Stephen Canon
What is wrong with my SNaN and QNaN? and how can i fix it?
Corey
On most (not all) modern hardware, a NaN is quiet if and only if the `0x00400000` bit is set, and signaling if it is clear. You're using the signbit (`0x80000000`) to do your check, which doesn't do the right thing. (Note that the distinction between quiet and signaling NaNs is not actually pinned down by the standard)
Stephen Canon
Corey
The bit and clear thing really confuses me
Corey
I understand that: QNaN is a NaN with the most significant fraction bit set like 10000000000I just dont know how to test for it.
Corey
I actually tried something similiar. which didnt workif (exponent == 0xff
Corey
Stephen Canon
how do you check for SNan?
Corey
Corey
No, that would fail on `0x7f900000`, for example.
Stephen Canon
How are you coming up with these numbers?0x7f900000 and 0x00400000is their a chart or how can i learn what to use? do i need to convert from something? any help is much appreciated. I just want to learn. is that a mask?
Corey