about cout output format

D

David

I have following code.

float b;

b= 1.234;

cout<<hex<<b<<endl;

How come it cannot ouput the hex representation of b?
 
H

Howard

David said:
I have following code.

float b;

b= 1.234;

cout<<hex<<b<<endl;

How come it cannot ouput the hex representation of b?

As far as I can tell, "hex" is a flag specifying the base for representation
of an integer value, not a floating-point value. (I'm not even sure how you
would represent 1.234 in hex???)

-Howard
 
M

Mike Wahler

David said:
I have following code.

float b;

b= 1.234;

cout<<hex<<b<<endl;

How come it cannot ouput the hex representation of b?

The hex format is for integer values. A "hex
real number" doesn't mean anything. If you want
to look at the bit pattern of a type 'float' object,
expressed in hex:

#include <algorithm>
#include <ios>
#include <iostream>
#include <iterator>

int main()
{

float f(1.234f);

unsigned char raw[sizeof f];
*(float*)raw = f;

std::cout << std::hex;

std::copy(raw, raw + sizeof raw,
std::eek:stream_iterator<unsigned int>(std::cout, ""));

std::cout.put('\n');

return 0;
}

But note that the output will vary among platforms,
as not all use the same representation for floating
point values.

-Mike
 
D

David Harmon

On Mon, 18 Oct 2004 22:48:56 GMT in comp.lang.c++, "Mike Wahler"
The hex format is for integer values. A "hex
real number" doesn't mean anything.

Of course it does. The first hex digit after the radix point is the
number of sixteenths in the fractional part. The second is the number of
256'ths, and so on. It means exactly the same thing as a real number in
decimal format, except using base sixteen instead of ten.

Decimal 1.234 = hex 1.3BE76C

But there is not much real demand for it, so nobody bothered to include it
in C++.
 
J

John Harrison

David Harmon said:
On Mon, 18 Oct 2004 22:48:56 GMT in comp.lang.c++, "Mike Wahler"


Of course it does. The first hex digit after the radix point is the
number of sixteenths in the fractional part. The second is the number of
256'ths, and so on. It means exactly the same thing as a real number in
decimal format, except using base sixteen instead of ten.

Decimal 1.234 = hex 1.3BE76C

But there is not much real demand for it, so nobody bothered to include it
in C++.

I think its more likely that the OP wants to see the internal representation
of a real number rather than the value. But if he clarifies his requirements
I'm sure he'll get an answer either way.

john
 
P

Peter Koch Larsen

Mike Wahler said:
David said:
I have following code.

float b;

b= 1.234;

cout<<hex<<b<<endl;

How come it cannot ouput the hex representation of b?

The hex format is for integer values. A "hex
real number" doesn't mean anything. If you want
to look at the bit pattern of a type 'float' object,
expressed in hex:

#include <algorithm>
#include <ios>
#include <iostream>
#include <iterator>

int main()
{

float f(1.234f);

unsigned char raw[sizeof f];
*(float*)raw = f;

std::cout << std::hex;

std::copy(raw, raw + sizeof raw,
std::eek:stream_iterator<unsigned int>(std::cout, ""));

std::cout.put('\n');

return 0;
}

But note that the output will vary among platforms,
as not all use the same representation for floating
point values.

-Mike
Here you are on dangerous grounds, Mike. The character array might not be
suitably aligned for a float, potentially causing all kinds of bad stuff.

Why not simply go for the simple solution with

char const *raw = reinterpret_cast<char const*>(f); ?

/Peter
 
R

rossum

As far as I can tell, "hex" is a flag specifying the base for representation
of an integer value, not a floating-point value. (I'm not even sure how you
would represent 1.234 in hex???)

-Howard
<mode = nerd>
Hex is base 16 so each position is 1/16th of the position to its left.
Thus after the 'heximal point' the positions are worth 1/16, 1/256,
1/4096 etc.

so 1.234 = 1 + 3/16 + 11/256 + 14/4096 + ...

The sequence 1, 3, 11, 14, ... gives the hex.

This is 1.3BE... in hex.
<mode = what passes for normal>

rossum
 
D

David

Thanks for all your inputs. What I really need to know is the internal
bit pattern of the float number (hex presentations). All of your
answers are very helpful!!

Yi
 
D

David

I got the following error using perter's method on following code:

invalid reinterpret_cast from type `float' to type `const
char *'


#include <iostream>
#include <algorithm>


int main(){

float f = 1.234;

char const *raw = reinterpret_cast<char const*>(f);
cout<<raw<<endl;

}
 
K

Karl Heinz Buchegger

David said:
I got the following error using perter's method on following code:

invalid reinterpret_cast from type `float' to type `const
char *'

#include <iostream>
#include <algorithm>

int main(){

float f = 1.234;

char const *raw = reinterpret_cast<char const*>(f);
cout<<raw<<endl;

}

It would not have worked anyway.
 
K

Karl Heinz Buchegger

David said:
I got the following error using perter's method on following code:

invalid reinterpret_cast from type `float' to type `const
char *'

#include <iostream>
#include <algorithm>

int main(){

float f = 1.234;

char const *raw = reinterpret_cast<char const*>(f);
cout<<raw<<endl;

}

It would not have worked anyway in the way you want it to work.
 
P

Peter Koch Larsen

David said:
I got the following error using perter's method on following code:

invalid reinterpret_cast from type `float' to type `const
char *'


#include <iostream>
#include <algorithm>


int main(){

float f = 1.234;

char const *raw = reinterpret_cast<char const*>(f);
cout<<raw<<endl;

}

Sorry - it should have been

char const *raw = reinterpret_cast<char const*>(&f);

Note the ampersand.

/Peter
 
K

Karl Heinz Buchegger

Peter said:
What do you mean by that "anyway", anyway?

What do you expect to be output?

You cant change the rules. If in

cout << something

'something' is a pointer to characters (const or not), the stream
formatting function expects this to be a real 'string' (that is:
printable characters terminated with '\0'). Just by casting some
pointer to a character pointer, you don't get magically a string
representation.
 
P

Peter Koch Larsen

Karl Heinz Buchegger said:
What do you expect to be output?

You cant change the rules. If in

cout << something

'something' is a pointer to characters (const or not), the stream
formatting function expects this to be a real 'string' (that is:
printable characters terminated with '\0'). Just by casting some
pointer to a character pointer, you don't get magically a string
representation.

Ahh.. I see. I never intended the output of the binary format to be
std::cout << charptr. I only devised a way to get access to that raw data
(where i forgot the ampersand in the first post).

/Peter
 
K

Karl Heinz Buchegger

Peter said:
Ahh.. I see. I never intended the output of the binary format to be
std::cout << charptr. I only devised a way to get access to that raw data
(where i forgot the ampersand in the first post).

Got it.

It's just that exactly this question or a variation of it

"I have a xxxx and I want to output it, but the compiler won't
let me do it. Thus I casted to char* but I get only garbage.
Why? I thought char* represents a string?"

is asked *very* often. I guess, it was my 'optical code pattern matcher'
interfering when I saw the code snippet :)
 
L

Lionel B

Thanks for all your inputs. What I really need to know is the internal
bit pattern of the float number (hex presentations). All of your
answers are very helpful!!

Yi

typedef float myfloat; // try with double

static const int words = sizeof(myfloat)/sizeof(unsigned long);

union float_bits // alignment issues?
{
myfloat f;
unsigned long w[words];
};

int main()
{
cout << "words = " << words << endl;

myfloat f = 1.234;

float_bits fb;
for (int n=0;n<words;++n) fb.w[n] = 0; //not sure if we need this...
fb.f = f;

cout << "hex " << f << " =";
for (int n=0;n<words;++n) cout << ' ' << hex << fb.w[n];
cout << endl;
}

outputs:

words = 1
hex 1.234 = 3f9df3b6

on my machine. If we "typedef double myfloat" output is:

words = 2
hex 1.234 = c8b43958 3ff3be76

This may or may not be what you want.

Regards,
 
L

Lionel B

Lionel said:
(e-mail address removed) (David) wrote in message
Thanks for all your inputs. What I really need to know is the internal
bit pattern of the float number (hex presentations). All of your
answers are very helpful!!

Yi

typedef float myfloat; // try with double

static const int words = sizeof(myfloat)/sizeof(unsigned long);

union float_bits // alignment issues?
{
myfloat f;
unsigned long w[words];
};

int main()
{
cout << "words = " << words << endl;

myfloat f = 1.234;

float_bits fb;
for (int n=0;n<words;++n) fb.w[n] = 0; //not sure if we need this...
fb.f = f;

cout << "hex " << f << " =";
for (int n=0;n<words;++n) cout << ' ' << hex << fb.w[n];

Ooerr..., make that:

for (int n=words-1;n>=0;--n) cout << ' ' << hex << fb.w[n];

We (is this Western-centric?) tend to read digits left -> right = high
-> low :-/
cout << endl;
}

outputs:

words = 1
hex 1.234 = 3f9df3b6

on my machine. If we "typedef double myfloat" output is:

words = 2
hex 1.234 = c8b43958 3ff3be76
Make that:

hex 1.234 = 3ff3be76 c8b43958

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,516
Latest member
ChrisHibbs

Latest Threads

Top