comp.lang.ada
 help / color / mirror / Atom feed
* Float to Fixed conversions
@ 1995-03-16 22:29 Wayne Magor
  1995-03-18 13:01 ` Tucker Taft
  1995-03-19  1:31 ` Robert Dewar
  0 siblings, 2 replies; 4+ messages in thread
From: Wayne Magor @ 1995-03-16 22:29 UTC (permalink / raw)



Is it legal for the program below to print 0.0?  The reason I question it
is because it is DEC's Vax Ada that returns 0.0, but their Alpha Ada
returns 1.0.  The 'Machine_Rounds is FALSE for Vax and TRUE for Alpha,
but a compiler on a Sun machine has 'Machine_Rounds to be FALSE and yet
still returns 1.0.

Is there a way to adjust for these differences or is there just a bug in
the Vax Ada compiler?  Is the conversion different if it is exactly half
way between the fixed point model numbers?


procedure Fixed_Pt is
  type Fixed is delta 1.0 range -10.0 .. 10.0;
  Fix : Fixed;
  Flt : Float := 0.999;
begin
  Fix := Fixed(Flt);  -- should Fix alway be set to 1.0?
end Fixed_Pt;



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Float to Fixed conversions
  1995-03-16 22:29 Float to Fixed conversions Wayne Magor
@ 1995-03-18 13:01 ` Tucker Taft
  1995-03-20 21:29   ` Wayne Magor
  1995-03-19  1:31 ` Robert Dewar
  1 sibling, 1 reply; 4+ messages in thread
From: Tucker Taft @ 1995-03-18 13:01 UTC (permalink / raw)


Wayne Magor (wemagor@fmsa.cca.rockwell.com) wrote:

: Is it legal for the program below to print 0.0?  The reason I question it
: is because it is DEC's Vax Ada that returns 0.0, but their Alpha Ada
: returns 1.0.  The 'Machine_Rounds is FALSE for Vax and TRUE for Alpha,
: but a compiler on a Sun machine has 'Machine_Rounds to be FALSE and yet
: still returns 1.0.

Conversions between (ordinary) fixed and float can either truncate or round.
The 'Machine_Rounds attribute of the *target* type "should" tell you
which one happens, but that attribute is mostly oriented toward operations
within a type, not conversions between types.  Have you checked
the 'Machine_Rounds attribute of type Fix?

(By the way, in Ada 95, conversion to *decimal* fixed point requires 
truncation, unless you use the 'Round attribute function.)

: Is there a way to adjust for these differences or is there just a bug in
: the Vax Ada compiler?  Is the conversion different if it is exactly half
: way between the fixed point model numbers?

Being exactly half way is not a special case for conversions to real type.
On the other hand, when converting from real to integer, rounding is required
and in Ada 95, if exactly halfway between two integers, the result has
to be rounded away from zero (Ada 83 allows either direction in this case).

See RM95 4.6(29-33) for the official rules, plus G.2.3(24).

: procedure Fixed_Pt is
:   type Fixed is delta 1.0 range -10.0 .. 10.0;
:   Fix : Fixed;
:   Flt : Float := 0.999;
: begin
:   Fix := Fixed(Flt);  -- should Fix alway be set to 1.0?
: end Fixed_Pt;

-Tucker Taft  stt@inmet.com
Intermetrics, Inc.



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Float to Fixed conversions
  1995-03-16 22:29 Float to Fixed conversions Wayne Magor
  1995-03-18 13:01 ` Tucker Taft
@ 1995-03-19  1:31 ` Robert Dewar
  1 sibling, 0 replies; 4+ messages in thread
From: Robert Dewar @ 1995-03-19  1:31 UTC (permalink / raw)


Wayne Magor asks about conversion of float to fixed. The disparate
behaviors of the two compilers he used are both correct. The number
in question was a non-model number which lay between two modem numbers
0.0 and 1.0 (oops thats model numbers, although I rather like the
idea of modem numbers :-)

In this situation, either 0.0 or 1.0 is a valid result, there is no
requirement for round-to-nearest (except in the case of Ada 95 decimal
types, where rounding/truncation is indeed deterministic and well
defined).




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Float to Fixed conversions
  1995-03-18 13:01 ` Tucker Taft
@ 1995-03-20 21:29   ` Wayne Magor
  0 siblings, 0 replies; 4+ messages in thread
From: Wayne Magor @ 1995-03-20 21:29 UTC (permalink / raw)



In article <D5n06v.D8F@inmet.camb.inmet.com>, stt@henning.camb.inmet.com (Tucker Taft) writes:
>
>Conversions between (ordinary) fixed and float can either truncate or round.
>The 'Machine_Rounds attribute of the *target* type "should" tell you
>which one happens, but that attribute is mostly oriented toward operations
>within a type, not conversions between types.  Have you checked
>the 'Machine_Rounds attribute of type Fix?


Thanks to you and Robert for your advice on this question.  I should have
stated that I am using Ada 83, and I also should have been more clear about
the type used for the attributes:

                         Fix'Machine_Rounds    Rounds the conversion
                         ------------------    ---------------------
     DEC Vax Ada 83            FALSE                    No
     DEC Alpha Ada 83          TRUE                     Yes
     Rational Sun Ada 83       FALSE                    Yes

The use of the value of Fix'Machine_Rounds to decide if an adjustment
should be made before the conversion is apparently not adequate to
guarantee that rounding is always performed.  It seems the user must
look at the value after the conversion to determine if the machine
performed rounding.  This is unfortunately inconvenient and slow.

Wayne.



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1995-03-20 21:29 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1995-03-16 22:29 Float to Fixed conversions Wayne Magor
1995-03-18 13:01 ` Tucker Taft
1995-03-20 21:29   ` Wayne Magor
1995-03-19  1:31 ` Robert Dewar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox