From: Dan Nagle on
Hello,

On 2009-12-07 03:13:50 -0500, Frank <frank(a)example.invalid> said:

> Do C and Fortran have the same bit
> model for a float/default real?

Hardware encodes floating point.

A program written in either C or Fortran,
running on particular hardware, will use the supported
encoding (or it will execute very slowly).

The languages do not have encodings.
The languages tell you how to access the facilities
of IEEE 754, if available.

The languages may tell you the effects of operations,
but not how the operations are done.

--
Cheers!

Dan Nagle

From: Tim Prince on
Frank wrote:
> On Fri, 4 Dec 2009 17:16:19 -0500, Dan Nagle wrote:
>

>>
>> Logicals have two values, true and false.
>> These may be encoded at the processor's whim.
>>
>> I have seen zero v nonzero, positive v negative,
>> even v odd, and probably something else I can't recall.
>>
>> Others have, no doubt, seen other encodings.
>>
>> That way are dragons.
>
> This shakes my faith a little bit. Do C and Fortran have the same bit
> model for a float/default real?
Neither C nor Fortran has a useful relationship between floating point
formats and true/false bit patterns. If you had faith in such a thing,
it was misplaced. Dan already answered what you asked explicitly.
C guarantees that the relational operators return 0 or 1. A very few
Fortran compilers match C in that respect. If you think about it, when
that is done, it works under any of 3 possibilities Dan mentioned above.
From: frank on
On Mon, 07 Dec 2009 05:35:49 -0800, Tim Prince wrote:

>>> That way are dragons.
>>
>> This shakes my faith a little bit. Do C and Fortran have the same bit
>> model for a float/default real?
> Neither C nor Fortran has a useful relationship between floating point
> formats and true/false bit patterns. If you had faith in such a thing,
> it was misplaced. Dan already answered what you asked explicitly. C
> guarantees that the relational operators return 0 or 1. A very few
> Fortran compilers match C in that respect. If you think about it, when
> that is done, it works under any of 3 possibilities Dan mentioned above.

But one and zero are comparable in that one is greater than zero every
day of the week. Furthermore, I think there's guarantees that 0 is false
on the C side of it. I for one wouldn't understand a logic without that
being the case, although I wouldn't doubt that some pathological
alternative exists.

I've been working up this same material in C, where they don't have an
intrinsic like btest and rely on left and right shifts and a mask
instead, and then there's the ultimate comparison to zero:

printf("%d", (*p & mask) > 0);

If we take a float and transfer it to an integer of an appropriate width,
do I then have the bit model discussed at the beginning of 8.8 MR&C? Can
I speak then of "setting a bit" and thinking that that means giving it
the value one?
--
frank

"Guns: yes, they are harmful."
From: frank on
On Mon, 07 Dec 2009 06:16:31 -0500, Dan Nagle wrote:

> Hello,
>
> On 2009-12-07 03:13:50 -0500, Frank <frank(a)example.invalid> said:
>
>> Do C and Fortran have the same bit
>> model for a float/default real?
>
> Hardware encodes floating point.

Ok.
>
> A program written in either C or Fortran, running on particular
> hardware, will use the supported encoding (or it will execute very
> slowly).
>
> The languages do not have encodings.
> The languages tell you how to access the facilities of IEEE 754, if
> available.
>
> The languages may tell you the effects of operations, but not how the
> operations are done.

If I take a float in C, assign it a value, pass a pointer to it to
fortran using the ISO_C_BINDING, dereference the pointer to assign its
value to a default real, do I necessarily have the same value?
--
frank

"Guns: yes, they are harmful."
From: glen herrmannsfeldt on
frank <frank(a)example.invalid> wrote:
(snip)

> If I take a float in C, assign it a value, pass a pointer to it to
> fortran using the ISO_C_BINDING, dereference the pointer to assign its
> value to a default real, do I necessarily have the same value?

Theoretically you should use TYPE(C_FLOAT) to get the appropriate
type, but I would be pretty surprised to find that different from
default REAL.

They might be different on hardware with more than one floating
point type of the appropriate size. VAX has two different 64 bit
floating point formats. With the addition of decimal float to
the IEEE standard, it would be possible for a compiler to use that
as its default floating point type.

-- glen