ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Well, U have to know about memory format using for storing of different types.
int - integer value (N: -687, 0, 145, but NOT 1.2 )
float - value with a float point (like 1.2, and 145.).
Say we have:
int i = 999;
float f = i; // this is a normal situation , here compiler know we make type conversation
But
float * f = (float*)&i; // if U use then *f than compiler will interpret the memory used by i like floating (but NOT integer value!), so with
cout << *f;
U get the floating value interpreted from the memory used for integer i,
because there are different formats for storing integer and floating values as I've mentioned above.
The output is in float format.
Sorry for my mistake about data formatting.
But I think the output should be *((float*)fp)=3.2767e4 (its value equal to 32767) instead of 4.59163e-41;
My main trouble is why the value had been changed (from 3.2767e4 to 4.59163e-41) after I casted a void pointer to a int pointer, is not type formatting.
Last edited by Xiangbuilder; 10-07-2003 at 09:38 PM.
Originally posted by Xiangbuilder Thank you.[code]
But I think the output should be *((float*)fp)=3.2767e4 (its value equal to 32767) instead of 4.59163e-41;
My main trouble is why the value had been changed (from 3.2767e4 to 4.59163e-41) after I casted a void pointer to a int pointer, is not type formatting.
No, humm... sorry may be my English is not so perfect, but I'll try to find right words...
The problem is that the same set of bits in memory have different interpretation which depends on the type of value!
The bits, U have, are interpreted like 32767 for integer value, but
4.59163e-41 for the value with floating point.
For ex. : for integer value(size = 4 bytes) = 1 we will have:
00000000 00000000 00000000 00000001
But the same bits are interpreted for float like (I donot remember exactly right, but the idea)
Every float value is interpreted like (+-)1.mantise (+-)e
Bit N 31 - is a sign bit . If it is 0 then the float value is positive, 1 - negative ("-")
Bits N 22 - 30 are bits for mantise ,
Bits N 1 - 21 - is a power for e.
Write some simple program, wich shows bits of integer = 32767, and float = 32767 and U'll se that the same value (32767) has different sets of bits for different types.
Sorry, I still can't understand it.
This is because of my poor c++ knowledge instead of your words.
Do you mean 32767 and 4.59163e-41 are two data forms in bytes for the same value and they have same data forms, 00000000 00000000 01111111 11111111 in bits?
Thank you.
Originally posted by Xiangbuilder Sorry, I still can't understand it.
This is because of my poor c++ knowledge instead of your words.
Do you mean 32767 and 4.59163e-41 are two data forms in bytes for the same value and they have same data forms, 00000000 00000000 01111111 11111111 in bits?
Thank you.
Yes, I mean that
int n = 32767;
and
float f = 4.59163e-41;
has the same set of bits in memory =
00000000 00000000 01111111 11111111
So when U use *(float*)&n, compiler interprets (00000000 00000000 01111111 11111111) like float value = 4.59163e-41.
So the main idea - that the same set of bits have different interpretation which depends on type of value.
U need to find and read some info about memory format for storing of different value types.
Can you tell me how to convert the float number as 4.59163e-41 to the integer number as 32767, the two number have same bitwise value?
"Bit N 31 - is a sign bit . If it is 0 then the float value is positive, 1 - negative ("-")"
00000000 00000000 01111111 11111111.
is Bit N 31 "1" that is red?
Thank you.
Originally posted by Xiangbuilder Can you tell me how to convert the float number as 4.59163e-41 to the integer number as 32767, the two number have same bitwise value?
//Say, we have float value
float f = 4.59163e-41;
// now n will point to the memory of f
int *n = (int*)&f;
// write *f, here compiler will interpret the set bits of f, like integer value
cout << *n << endl;
So, U will get 32767 on output.
Quote:
"Bit N 31 - is a sign bit . If it is 0 then the float value is positive, 1 - negative ("-")"
00000000 00000000 01111111 11111111.
is Bit N 31 "1" that is red?
Thank you.
U have to count from right to left and to begin from N0
So, your red bit is N1.
And the most left is N31(red) (the most right is N0(blue))
00000000 00000000 01111111 11111111.
Thank you for your patience.
"Bits N 22 - 30 are bits for mantise ,
Bits N 1 - 21 - is a power for e."
00000000 00000000 01111111 11111111
How to understand N0?
Why Bits N1~21 is e-41?
Why Bits N22~30 is 4.59163?
Thank you.
Do you know some studying material deal with this in the internet?
Last edited by Xiangbuilder; 10-10-2003 at 02:42 AM.
Originally posted by Xiangbuilder Thank you for your patience.
"Bits N 22 - 30 are bits for mantise ,
Bits N 1 - 21 - is a power for e."
00000000 00000000 01111111 11111111
Well, I just wanted to show U the idea that float and integer values have different interpretation of the same bits.
Well, I've just looked for original info and that is the rigth interpretation:
________________________________________________
|S(31)| Exponent (30-23) | Mantise (22-0) |
------------------------------------------------------------------------------------
How to understand N0?
The bit Number = 0 is the most right bit.
Because we have to count from ZERO
Not 1, 2, 3, ... 32
BUT 0, 1, 2, ... 31
Like when U have in C/C++ the array - int a[N], then the first element is a[0] (NOT a[1]), the last a[N-1].
Quote:
Thank you.
Do you know some studying material deal with this in the internet?
Try to find with google for example, I cannot enough free time to do this.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.