D
Doug Miller
Expecting an universal edit descriptor, I expected that a 2-byte
quantity resulted in 4 hex digits.
Not if you tell it explicitly you want 8...
Expecting an universal edit descriptor, I expected that a 2-byte
quantity resulted in 4 hex digits.
LC's No-Spam Newsreading account wrote:
Even less likely to work than REAL. Again TRANSFER is a good
solution. Though there is no guarantee that a default integer is four
characters long.
< In Java I tried
Look at the FloatToIntBits and IntToFloatBits functions.
Why is the result not expected? You are assigning an integer
constant to a float variable.
float x = 0x3.f8p-2f;
For heaven's sake read up on the syntax rather than trying to guess
it.
The problems with the "%o" and "%X" format specifiers in this case are
technically exactly the same as in the previous two cases. However,
LC's No-Spam Newsreading account said:Hmm... what's then the "standard" way to obtain an hex dump of a float
(in the way the Z format will do in Fortran), other than writing it to a
binary file and using od ?
LC's No-Spam Newsreading account said:I tried to play with this form, e.g.
f=0X1.ap0f; /* 1.625 */
f=0X1.ap1f; /* 3.25 */
f=0X1.ap-1f; /* 0.8125 */
But it is not obvious to me what the two parts are ... mantissa in hex
and exponent as power of 2 in decimal ... but are they the same that
constitute exactly the IEEE representation or can be scaled
arbitrarily ?
LC's No-Spam Newsreading account said:Well, my reference for Java is the tutorial on the java.sun.com site and
I've never found a CLEAR statement that an hex pattern is necessarily
typed as an integer. I assumed it to be an arbitrary bit pattern, which
could be reversed into any type of variable.
LC's No-Spam Newsreading account said:Hmm... what's then the "standard" way to obtain an hex dump of a float
(in the way the Z format will do in Fortran), other than writing it to
a binary file and using od ?
I tried the above using one of the following instead of "somevalue"
(with gcc)
f=1.0f ; /* gives 3FF00000 instead of 3F800000 */
f=2.0f ; /* gives 40000000 */
f=4.0f ; /* gives 40800000 instead of 40100000 */
f=8.0f ; /* gives 41000000 instead of 40200000 */
C FORTRAN
For 2.0 I obtain the same binary representation I obtain in Fortran,
for the other values (note all powers of 2) I obtain discrepant
values. 'm pretty sure (checked with my old Excel spreadsheet doing
the bit-by-bit
display of a real in IEEE and VAX form) the Fortran value is the
correct one (IEEE).
Is this a side effect of some signed vs unsigned thing ?
LC's No-Spam Newsreading account said:- There may be means to store an hex constant
into a float like Float.intBitsToFloat in Java
- it is not obvious to find the relevant information in the
documentation :-(
Hmm... what's then the "standard" way to obtain an hex dump of a float
(in the way the Z format will do in Fortran), other than writing it to a
binary file and using od ?
I tried the above using one of the following instead of "somevalue"
(with gcc)
f=1.0f ; /* gives 3FF00000 instead of 3F800000 */
f=2.0f ; /* gives 40000000 */
f=4.0f ; /* gives 40800000 instead of 40100000 */
f=8.0f ; /* gives 41000000 instead of 40200000 */
C FORTRAN
For 2.0 I obtain the same binary representation I obtain in Fortran, for
the other values (note all powers of 2) I obtain discrepant values. 'm
pretty sure (checked with my old Excel spreadsheet doing the bit-by-bit
display of a real in IEEE and VAX form) the Fortran value is the correct
one (IEEE).
Is this a side effect of some signed vs unsigned thing ?
LC's No-Spam Newsreading account wrote:
I'd do something like this (warning, been too long since C for me):
union float_int {
float f;
int i;
} x;
x.f = 1.0f;
printf( "%08.8X", x.i );
LC's No-Spam Newsreading account said:I thought they were "universal" like O and Z in Fortran.
I wasn't aware of this. See above.
Expecting an universal edit descriptor, I expected that a 2-byte
quantity resulted in 4 hex digits.
because that's the internal representation of the float 4.0 in IEEE
(just re-checked with my old Excel spreadsheet which gives the internal
representation of IEEE and VAX floats). ...
... I assumed that an "hex constant"
was just a bit pattern, with no type implied.
I realize now that, at least in C, hex constants are integer constants
(is this implicit or documented ?)
LC's No-Spam Newsreading account said:Hmm... what's then the "standard" way to obtain an hex dump of a float
(in the way the Z format will do in Fortran), other than writing it to a
binary file and using od ?
I tried the above using one of the following instead of "somevalue"
(with gcc)
f=1.0f ; /* gives 3FF00000 instead of 3F800000 */
f=2.0f ; /* gives 40000000 */
f=4.0f ; /* gives 40800000 instead of 40100000 */
f=8.0f ; /* gives 41000000 instead of 40200000 */
C FORTRAN
For 2.0 I obtain the same binary representation I obtain in Fortran, for
the other values (note all powers of 2) I obtain discrepant values. 'm
pretty sure (checked with my old Excel spreadsheet doing the bit-by-bit
display of a real in IEEE and VAX form) the Fortran value is the correct
one (IEEE).
Is this a side effect of some signed vs unsigned thing ?
Nick said:and if a float won't fit in an int?
markspace said:I'd do something like this (warning, been too long since C for me):
union float_int {
float f;
int i;
} x;
x.f = 1.0f;
printf( "%08.8X", x.i );
LC's No-Spam Newsreading account said:I am collecting a sort of phrase books with equivalent "things" in
various languages.
I am now considering the indication of constants as hex, oct or binary
constants.
> f=1.0f ; /* gives 3FF00000 instead of 3F800000 */
> f=2.0f ; /* gives 40000000 */
> f=4.0f ; /* gives 40800000 instead of 40100000 */
> f=8.0f ; /* gives 41000000 instead of 40200000 */
> C FORTRAN
>
> For 2.0 I obtain the same binary representation I obtain in Fortran, for
> the other values (note all powers of 2) I obtain discrepant values.
But reading Metcalf, Reid and Cohen (Fortran 95/2003 explained) I got
the impression they were standardized NOW.
What is exactly non standard in my usage ?
- usage of hex constants in assignments instead of DATA ?
- usage of hex constants for non-integers ?
- usage of hex edit descriptor for non-integers ?
I'd better revert to my old usages and consider hex constants as
something to avoid.
Well, unless one has access to The Standard Itself, the only way one can
gain experience is testing with specific compilers
LC's No-Spam Newsreading account said:I thought they were "universal" like O and Z in Fortran.
I realize now that, at least in C, hex constants are integer constants
(is this implicit or documented ?)
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.