LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-07-2003, 12:42 AM   #1
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Rep: Reputation: 30
What is wrong on the conversion?


Why when I convert a void pointer to a float pointer the value is wrong?
Code:
#include<iostream> 
using namespace std;
void func (int);
int main ()
{
   int i=0x7fff;
   float f=i;
   cout<<"f="<<f<<endl;
   float* fp_b=&f;
   cout<<"*fp_b="<<*fp_b<<endl; 
   void* vp=&i;
   cout<<"*((int*)vp)="<<*((int*)vp)<<"; vp="<<vp<<endl;
   float *fp=static_cast<float*>(vp);
   cout<<"*((float*)fp)="<<*((float*)fp)<<"; fp="<<fp<<endl;
   fp=(float*) vp;
   cout<<"*((float*)fp)="<<*((float*)fp)<<"; fp="<<fp<<endl;   
}
[root@localhost a_static]# g++ static_cast_ea.cpp -o static_cast_ea
[root@localhost a_static]# ./static_cast_ea
f=32767
*fp_b=32767
*((int*)vp)=32767; vp=0xbfffe754
*((float*)fp)=4.59163e-41; fp=0xbfffe754
*((float*)fp)=4.59163e-41; fp=0xbfffe754
[root@localhost a_static]#
I thought *((float*)fp)=32767; would be outputed, however not.
In this case, is there some mysterious things?
Thank you.

Last edited by Xiangbuilder; 10-07-2003 at 12:43 AM.
 
Old 10-07-2003, 07:40 AM   #2
dimm_coder
LQ Newbie
 
Registered: Oct 2003
Location: Minsk, Belarus
Distribution: Mandrake, FreeBSD
Posts: 28

Rep: Reputation: 15
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.
 
Old 10-07-2003, 09:36 PM   #3
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
Thank you.
Code:
*((float*)fp)=4.59163e-41;
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.
 
Old 10-08-2003, 02:19 AM   #4
dimm_coder
LQ Newbie
 
Registered: Oct 2003
Location: Minsk, Belarus
Distribution: Mandrake, FreeBSD
Posts: 28

Rep: Reputation: 15
Quote:
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.
 
Old 10-08-2003, 02:59 AM   #5
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
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.
 
Old 10-08-2003, 03:27 AM   #6
dimm_coder
LQ Newbie
 
Registered: Oct 2003
Location: Minsk, Belarus
Distribution: Mandrake, FreeBSD
Posts: 28

Rep: Reputation: 15
Quote:
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.
 
Old 10-08-2003, 03:56 AM   #7
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
The knowledge is too difficult to understand to me.
Perhaps I could learn it in the future.
Thank you.
 
Old 10-10-2003, 01:26 AM   #8
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
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.
 
Old 10-10-2003, 02:03 AM   #9
dimm_coder
LQ Newbie
 
Registered: Oct 2003
Location: Minsk, Belarus
Distribution: Mandrake, FreeBSD
Posts: 28

Rep: Reputation: 15
Quote:
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.
 
Old 10-10-2003, 02:37 AM   #10
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
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.
 
Old 10-10-2003, 03:02 AM   #11
dimm_coder
LQ Newbie
 
Registered: Oct 2003
Location: Minsk, Belarus
Distribution: Mandrake, FreeBSD
Posts: 28

Rep: Reputation: 15
Quote:
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.
 
Old 10-10-2003, 03:34 AM   #12
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
Thank you for your patience.
You have taought me many things.
Very helpful
"I cannot enough free time to do this."
Sorry.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Case conversion in C zaichik Programming 21 09-12-2005 06:55 PM
Alien Conversion aquaboot Ubuntu 2 09-09-2005 03:28 PM
my time is wrong and calender is also wrong Paxmaster Linux - General 6 12-16-2004 12:46 AM
File Conversion justaguynsrq Slackware 4 05-26-2003 07:10 PM
linux conversion emory7825 Linux - General 3 11-06-2001 03:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration