comp.lang.ada
 help / color / mirror / Atom feed
From: "Ken Garlington" <Ken.Garlington@computer.org>
Subject: Re: About conversions
Date: 2000/11/20
Date: 2000-11-20T00:00:00+00:00	[thread overview]
Message-ID: <RaaS5.5488$6W1.468063@news.flash.net> (raw)
In-Reply-To: 8vb0h9$1ou$1@nnrp1.deja.com

"Sandro Binetti" <sandrobinetti@my-deja.com> wrote in message
news:8vb0h9$1ou$1@nnrp1.deja.com...
: Hi everybody.
:
: Can you help me understand the meaning of a type conversion, like:
:   INTEGER(v)  ?
:
: Take an example like the following.
:
: -------------------------
:
: procedure PROVA is
:   type TYP is range 1..10;
:   c:typ;
:
:   procedure P(a,b:in integer;result:out integer) is
:   begin
:     result:=a+b;
:   end p;
:
: begin -- PROVA
:   p(10,20,INTEGER(c)); -- <<<<
: end prova;
:
: ----------------------------------------
:
:
: The compiler doesn't say anything at the point <<<<
:
: Why?

Here's an example that might help: Suppose I need to define a type that
represents temperature, from roughly the freezing point of water to its
boiling point. If I define something like

  type Temperature is new Float range 0.0 .. 100.0;

I might be right, or I might be wrong, depending upon what units I had in
mind. However, if I define something like

  type Celsius is new Float range 0.0 .. 100.0;
  type Fahrenheit is new Float range 32.0 .. 212.0;

then the units are clear. By defining these as separate types, I can catch
certain kinds of errors, such as attempting to display a Celsius value
assuming it was Fahrenheit:

  Thermometer : Celsius;
  ...
  procedure Display_Temperature ( Value : in Fahrenheit );
  ...
  Display_Temperature (Thermometer); -- error!

Your compiler will object to this attempt. To fix it, you might discover
that Thermometer should really be Fahrenheit, or that Display_Temperature
should really display Celsius values. However, what happens if
Thermometer is really a Celsius thermometer, and the display should show
values in Fahrenheit?

This can be fixed, of course: I could create a function, such as

  function To_Fahrenheit (Temperature : Celsius) return Fahrenheit is
  begin
    return (9.0/5.0)*Temperature-32.0; -- error!
  end To_Fahrenheit;

and use it to convert the Thermometer to the right scale. Unfortunately, the
compiler will complain here, as well. It will assume that you are
inadvertantly returning a Celsius value as a Fahrenheit value. To make it
clear that the calculation actually causes the value to be of a different
type, you have to do an explicit type conversion, as follows:

  function To_Fahrenheit (Temperature : Celsius) return Fahrenheit is
  begin
    return Fahrenheit((9.0/5.0)*Temperature-32.0);  -- OK
  end To_Fahrenheit;

As you noticed, the compiler does not complain when you use an explicit type
conversion to make a value the expected type. In your example, you have a
procedure P that expects values of type Integer, you convert your Typ value
to an Integer value before calling P, so everything is consistent.

There are common-sense limits to type conversions, of course. See the Ada
Reference Manual section 4.6 for details.

http://www.adahome.com/LRM/95/rm95html/rm9x-04-06.html

package Temperature is

  type Celsius is new Float range 0.0 .. 100.0;
  type Fahrenheit is new Float range 32.0 .. 212.0;

  function To_Celsius (Temperature : Fahrenheit) return Celsius;
  function To_Fahrenheit (Temperature : Celsius) return Fahrenheit;

end Temperature;

package body Temperature is

  function To_Celsius (Temperature : Fahrenheit) return Celsius is
  begin
    return Celsius((5.0/9.0)*(Temperature-32.0));
  end To_Celsius;

  function To_Fahrenheit (Temperature : Celsius) return Fahrenheit is
  begin
    return Fahrenheit((9.0/5.0)*Temperature-32.0);
  end To_Fahrenheit;

end Temperature;






  reply	other threads:[~2000-11-20  0:00 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-11-20  0:00 About conversions Sandro Binetti
2000-11-20  0:00 ` Ken Garlington [this message]
2000-11-20  0:00   ` Robert Dewar
2000-11-20  0:00     ` Stephen Leake
2000-11-21  0:00       ` Robert Dewar
2000-11-21  0:00         ` Warren W. Gay VE3WWG
2000-11-21  0:00           ` Robert Dewar
2000-11-21  0:00             ` Ted Dennison
2000-11-22  3:27               ` Warren W. Gay VE3WWG
2000-11-22  4:54               ` Robert Dewar
2000-11-22  0:00                 ` Wes Groleau
2000-11-22  0:00                 ` Ted Dennison
2000-11-22  0:00         ` Tristan Gingold
2000-11-24  0:00         ` Jean-Pierre Rosen
2000-11-24  0:00       ` Jean-Pierre Rosen
2000-11-21  2:57     ` DuckE
2000-11-21  0:00       ` Robert Dewar
  -- strict thread matches above, loose matches on Subject: below --
2000-11-23  6:21 Christoph Grein
2000-11-23  0:00 ` Wilhelm Spickermann
2000-11-23  0:00   ` Florian Weimer
2000-11-23  0:00     ` Wilhelm Spickermann
2000-11-28  2:20   ` Robert Dewar
2000-11-24  0:00 Christoph Grein
2000-11-28  1:33 ` Robert Dewar
replies disabled

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