[Webinar] Streamline your web hosting managementRegister Today


float structure

Posted on 1999-11-27
Medium Priority
Last Modified: 2008-02-20
I need to know the float structure (float representation in the memory).

I wrote a small program to do so but I don't understand the output.
Can someone help
(I'm using gcc under Linux with a 80x86)

#include <stdio.h>

typedef union
        float f;
        char c[4];
} fc;

void print(fc nb)
        printf("%f\t%03d %03d %03d %03d\n", nb.f, nb.c[0], nb.c[1], nb.c[2], nb.c[3]);

        fc a;
        printf("float size: %d\n", sizeof(float));
        a.f=1; print(a);
        a.f=2; print(a);
        a.f=3; print(a);
        a.f=10; print(a);
        a.f=20; print(a);
        a.f=-1; print(a);
        a.f=-2; print(a);
        a.f=1; print(a);

float size: 4
1.000000        000 000 -128 063
2.000000        000 000 000 064
3.000000        000 000 064 064
10.000000       000 000 032 065
20.000000       000 000 -96 065
100.000000      000 000 -56 066
-1.000000       000 000 -128 -65
-2.000000       000 000 000 -64
1.000000        000 000 -128 063
Question by:hakossem
  • 2
  • 2
  • 2
  • +2

Expert Comment

ID: 2237871
ma kore hakosem? ;)

where did u initialized the string a.c?
i think it prints the crapy data in a.c and the correct data for a.f [as inserted...]

ma habeaya?
whats the problem?


Author Comment

ID: 2237917
fc is an union not a structure.
a.c and a.f occuppe the same memory place so when I initialize a.f I change the content of a.c

Expert Comment

ID: 2237937
Your program is absolutely correct if
the sizeof(float) is 4. I think that is
true in Linux.

I think the problem is in printing :
Try this :
void print(fc nb)
        printf("%f\t%03d %03d %03d %03d\n", nb.f, (int)nb.c[0], (int) nb.c[1], (int) nb.c[2], (int) nb.c[3]);

You are trying to print integer but sending the characters. I think by casting this will solve your problem.

Give me the output. I will certainly solve this.
Choose an Exciting Career in Cybersecurity

Help prevent cyber-threats and provide solutions to safeguard our global digital economy. Earn your MS in Cybersecurity. WGU’s MSCSIA degree program was designed in collaboration with national intelligence organizations and IT industry leaders.


Expert Comment

ID: 2237945
basant, I think hakossem is just trying to find out, how the value is stored in memory.

hakossem, perhaps this helps:

It is problaly stored according to the IEEE standard for 4-byte real numbers:

sign bit, 8-bit exponent, 23-bit mantissa.
There is an assumed leading 1 in the mantissa that is not stored in memory, so the mantissa is actually 24 bits, even though only 23 bits are stored.

The exponent is biased by half of the possible value. This means you subtract this bias from the stored exponent to get the actual exponent. If the stored exponent is less than the bias, it is actually a negative exponent.
The exponent is biased by 127 for 4-byte reals.
The exponent is powers of two.

Remember that Intel cpu's stores the bytes in reverse order in memory, so in your example, c[0] is bit 24-31, c[1] is bit 16-23, c[2] is bit 8-15 and c[3] is bit 0-7.
The sign bit is the first bit of c[3], as you can se is the only difference between 1, 2 and -1, -2.
1: The 1-bit for the mantissa is not stored, so it is all 0-bits. The exponent is zero, so because of the bias, the stored value is 127 or 7 1-bits. The first is stored as the first bit of c[2] giving it the value of -128. The rest are stored in c[3], giving the value of 63.
2: Now the exponent is 1, storing 128, or one 1-bit, which gives the value of 64.
3: As 2, except the value of the mantissa is now 3, storing one 1-bit, which gives the value of 64 in c[2]


Expert Comment

ID: 2238003
I agree to hustch. Probably printing will be OK.

Author Comment

ID: 2238076
it doesn't need casting

hustch post your comment as an answer it works fine.
If you know the description of double too I'll enjoy you to send it too.
LVL 85

Expert Comment

ID: 2238082

Accepted Solution

hustch earned 400 total points
ID: 2238115
8 byte reals are sign bit, 11-bit exponent, 52-bit mantissa.

The exponent bias is 1023.

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
There's never been a better time to become a computer scientist. Employment growth in the field is expected to reach 22% overall by 2020, and if you want to get in on the action, it’s a good idea to think about at least minoring in computer science …
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Suggested Courses

640 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question