comp.lang.ada
 help / color / mirror / Atom feed
* School Assignment--Can this be simplified?
@ 2000-08-30  0:00 N J Chackowsky
  2000-08-30  0:00 ` Pascal Obry
                   ` (3 more replies)
  0 siblings, 4 replies; 15+ messages in thread
From: N J Chackowsky @ 2000-08-30  0:00 UTC (permalink / raw)


Hello... I'm about to teach ADA95 for the first time, and I've come up
with an assignment which helps students encode formulas. I'm using a
formula to translate from temperature and dew point to relative
humidity. The trouble is, I need to use the Pow function, so I need with
ada.numerics.aux. Does this look about right, and could it be
simplified. 

--HUMIDITY.ADB
-----------------------------------------------------------------------
--| Calculates and displays the relative humidity based on 
--| the temperature and dew point.
--|
--| Author: N J Chackowsky, Brandon School Division No. 40
--| Last modified: 2000 08 30
-----------------------------------------------------------------------

   with ada.text_io;
   with ada.integer_text_io;
   with ada.float_text_io;

	-- Normally, we'd use ada.numerics, but we also need the Pow, Double,
and Float
	-- functions. Ada.Numerics.Aux contains these.
   with ada.numerics.aux; use ada.numerics.aux;

   procedure humidity is
   
      tempc, tempk : float;   	-- temperature in C and K
      dewc, dewk : float;	-- dew point in C and K
      vp, svp : float;		-- vapour pressure and saturated vapour pressure
      rh : float;		-- relative humidity  
   
   begin
   
   	-- Get the temperature and dew point
      ada.text_io.put(item => "What is the temperature in degrees
Celcius? ");
      ada.float_text_io.get(item => tempc);
      ada.text_io.put(item => "What is the dew point in degrees Celcius?
");
      ada.float_text_io.get(item => dewc);
   
   	-- Convert from C to K
      tempk := tempc + 273.15;
      dewk := dewc + 273.15;
   
   	-- Calculate vapour pressure and saturated vapour pressure
      vp := float(pow(2.7183, double(-5438.0 / dewk + 21.72)));
      svp := float(pow(2.7183,double(-5438.0 / tempk + 21.72)));
   
   	-- Calculate relative humidity. Multiply by 100.0 for display as a %
      rh := vp / svp * 100.0;
   
   	-- Display results
      ada.text_io.put(item => "The relative humidity is ");
      ada.float_text_io.put(item => rh, fore => 1, aft => 1, exp => 0);
      ada.text_io.put(item => "%");
   
   end humidity;

Question: Why do I need the double( ) around the expression
-5438.0/dewk+21.72, but not around the literal 2.7183?

Thanks for any input.

Nick.




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

* School Assignment--Can this be simplified?
  2000-08-30  0:00 School Assignment--Can this be simplified? N J Chackowsky
  2000-08-30  0:00 ` Pascal Obry
  2000-08-30  0:00 ` Keith Thompson
@ 2000-08-30  0:00 ` tmoran
  2000-08-31  0:00 ` Martin Dowie
  3 siblings, 0 replies; 15+ messages in thread
From: tmoran @ 2000-08-30  0:00 UTC (permalink / raw)


>Question: Why do I need the double( ) around the expression
>-5438.0/dewk+21.72, but not around the literal 2.7183?
  Because the expression is of type "float" (from dewk or tempk)
and your "Pow" function presumably wants a "double" for the
second parameter.  Your first parameter is a numeric literal,
so it would serve OK as either a float or a double.

  Why do you need double precision in a calculation of vapor
pressure?  Is this in the interior of neutron star or something? ;)
Especially when e as given is accurate to only 4 places.  If
the standard Float is not adequate, and you don't want to use an
explicit declaration like "type Real is digits 11;" and
instantiate the needed generics, then check if your compiler
supports the predefined type Long_Float and matching package
Ada.Numerics.Long_Elementary_Functions.

>The trouble is, I need to use the Pow function, so I need with
>ada.numerics.aux. Does this look about right, and could it be
>simplified.
  It would be simpler to use the standard Exp(x) function than the
non-standard Pow(2.7183,x).




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

* Re: School Assignment--Can this be simplified?
  2000-08-30  0:00 School Assignment--Can this be simplified? N J Chackowsky
  2000-08-30  0:00 ` Pascal Obry
@ 2000-08-30  0:00 ` Keith Thompson
  2000-08-30  0:00 ` tmoran
  2000-08-31  0:00 ` Martin Dowie
  3 siblings, 0 replies; 15+ messages in thread
From: Keith Thompson @ 2000-08-30  0:00 UTC (permalink / raw)


N J Chackowsky <chackowsky@brandonsd.mb.ca> writes:
[...]
>    	-- Calculate vapour pressure and saturated vapour pressure
>       vp := float(pow(2.7183, double(-5438.0 / dewk + 21.72)));
>       svp := float(pow(2.7183,double(-5438.0 / tempk + 21.72)));

I think you want Exp(...), not Pow(2.7183, ...).

-- 
Keith Thompson (The_Other_Keith) kst@cts.com  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center           <*>  <http://www.sdsc.edu/~kst>
Welcome to the last year of the 20th century.




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

* Re: School Assignment--Can this be simplified?
  2000-08-30  0:00 School Assignment--Can this be simplified? N J Chackowsky
@ 2000-08-30  0:00 ` Pascal Obry
  2000-08-30  0:00 ` Keith Thompson
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 15+ messages in thread
From: Pascal Obry @ 2000-08-30  0:00 UTC (permalink / raw)



N J Chackowsky <chackowsky@brandonsd.mb.ca> writes:
> Hello... I'm about to teach ADA95 for the first time, and I've come up
> with an assignment which helps students encode formulas. I'm using a
> formula to translate from temperature and dew point to relative
> humidity. The trouble is, I need to use the Pow function, so I need with
> ada.numerics.aux. Does this look about right, and could it be
> simplified. 
> 

Nope. You should not use Ada.Numerics.Aux (which a GNAT unit) but
Ada.Numerics.[Generic_]Elementary_Functions. You should BTW use
a Reference Manual and use only things that are define there. Do
not use compiler extention in an Ada course.

BTW, you should write 'Ada' this way and not 'ADA'.

Pascal.

-- 

--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--|         http://perso.wanadoo.fr/pascal.obry
--|
--| "The best way to travel is by means of imagination"




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

* Re: School Assignment--Can this be simplified?
  2000-08-30  0:00 School Assignment--Can this be simplified? N J Chackowsky
                   ` (2 preceding siblings ...)
  2000-08-30  0:00 ` tmoran
@ 2000-08-31  0:00 ` Martin Dowie
  2000-08-31 11:31   ` Simon Wright
  2000-08-31 18:35   ` N J Chackowsky
  3 siblings, 2 replies; 15+ messages in thread
From: Martin Dowie @ 2000-08-31  0:00 UTC (permalink / raw)


A little more detail about what compile suite you are using might be
helpful...

What is 'ada.numerics.aux' - it isn't a standard library unit?
Why not use an instance of 'Ada.Numerics.Generic_Elementary_Functions',
which is the standard library generic for maths functions? (some
implementations provide 'ready-to-use' instances for float/long_float).

On the programming style front (feel free to ignore the rest of this
response ;-)

1. 'magic numbers' (e.g. 273.15) are repeated and would be more
   readable as named numbers (e.g. c_to_k_factor : constant := 273.15;)

2. Why are all the data objects 'variables' that are only ever assigned
   once? This is a little pet-hate of mine, as by declaring them as
   constants would help reduce target code size on some (most?)
   compilers. I was always taught it is simply 'better programming'.

   It is a current loathing of mine simply because we are using
   a non-optimizing compiler just now; 3 out of the original 5 s/w
   engineers have left and their stuff is litter with this and, hey,
   guess what, we're short on space on the target! If they had used
   constants like this we would have an extra couple of percent -
   not much, but for a little thought when programming, it is a couple
   of percent we wouldn't have to be scrapping together now! As a side
   effect, by reducing code size we increase throughput on the target
   too :-)

3. I best not even get into the whole 'why is everything a 'float' and
   not user-defined float types' thing... This smacks of 'C' programs
   not Ada programs.

N J Chackowsky wrote:
> 
[snip]
>
-- 
The views expressed here are personal and not those of BAE Systems.




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

* Re: School Assignment--Can this be simplified?
  2000-08-31  0:00 ` Martin Dowie
@ 2000-08-31 11:31   ` Simon Wright
  2000-08-31 11:51     ` Martin Dowie
  2000-08-31 18:35   ` N J Chackowsky
  1 sibling, 1 reply; 15+ messages in thread
From: Simon Wright @ 2000-08-31 11:31 UTC (permalink / raw)


Martin Dowie <martin.dowie@baesystems.com> writes:

> 1. 'magic numbers' (e.g. 273.15) are repeated and would be more
>    readable as named numbers (e.g. c_to_k_factor : constant := 273.15;)

Agreed in principle, but I don't think that 273.15 is a 'factor'
precisely :-)

We have just spent several minutes discussing a suitable name; one
suggestion was on the lines of Zero_Celsius_In_Kelvins.

  procedure P (Temperature : Float);
  -- Temperature is in Kelvins

  C : Float;
  -- in Celsius

  ...

  P (Temperature => C + Zero_Celsius_In_Kelvins);

which is pretty icky. Perhaps derived or even private types would be
interesting. Perhaps just conversion functions, inline if you like?

  function To_Kelvins (Temperature_In_Celsius : Float) return Float;

-- 
Simon Wright                         Email: simon.j.wright@amsjv.com
Alenia Marconi Systems                     Voice: +44(0)23 9270 1778
Integrated Systems Division                  FAX: +44(0)23 9270 1800



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

* Re: School Assignment--Can this be simplified?
  2000-08-31 11:31   ` Simon Wright
@ 2000-08-31 11:51     ` Martin Dowie
  0 siblings, 0 replies; 15+ messages in thread
From: Martin Dowie @ 2000-08-31 11:51 UTC (permalink / raw)


Simon Wright wrote:
> 
[snip]
> which is pretty icky. Perhaps derived or even private types would be
> interesting. Perhaps just conversion functions, inline if you like?
> 
>   function To_Kelvins (Temperature_In_Celsius : Float) return Float;

I was hinting at this in point 3 :-)

I have been looking at the units/conversions packages provided by 'whitaker'
(can't remember where I downloaded them from). They look
promising and if they prove efficient could be a _very_ handy
"standard" library.



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

* Re: School Assignment--Can this be simplified?
  2000-08-31  0:00 ` Martin Dowie
  2000-08-31 11:31   ` Simon Wright
@ 2000-08-31 18:35   ` N J Chackowsky
  2000-08-31 19:19     ` N J Chackowsky
  1 sibling, 1 reply; 15+ messages in thread
From: N J Chackowsky @ 2000-08-31 18:35 UTC (permalink / raw)


Thanks for the advice/warnings/etc. I think that where I'm getting
myself confused is with which libraries are standard and which aren't. I
had *thought* that any ada.* were standard. The text I'm using (Feldman)
doesn't make this clear. I've started to plow through the reference
manual, looking for the functions available in
...generics_elementary_functions.

The reason I was using such a mix of "magic numbers", constants, and
variables, was to "code around" the multiple data types, as a result of
using ada.numerics.aux (based on double). As I get to know the libraries
better, I'll tidy up my code.

(Using gnat-3_13p-nt.exe on Win95/98.)

One problem that smote my eyes *just* after making the post was, of
course, that the calculation can be vastly simplified. Since it takes
the form
	 x
	a                (x-y)
       ----   which is a       .  Not really an Ada95-specific issue,
though.
	 y
	a

Again, thanks for the responses.

Nick J Chackowsky
Brandon, MB

Martin Dowie wrote:
> 
> A little more detail about what compile suite you are using might be
> helpful...
> 
> What is 'ada.numerics.aux' - it isn't a standard library unit?
> Why not use an instance of 'Ada.Numerics.Generic_Elementary_Functions',
> which is the standard library generic for maths functions? (some
> implementations provide 'ready-to-use' instances for float/long_float).
> 
> On the programming style front (feel free to ignore the rest of this
> response ;-)
> 
> 1. 'magic numbers' (e.g. 273.15) are repeated and would be more
>    readable as named numbers (e.g. c_to_k_factor : constant := 273.15;)
> 
> 2. Why are all the data objects 'variables' that are only ever assigned
>    once? This is a little pet-hate of mine, as by declaring them as
>    constants would help reduce target code size on some (most?)
>    compilers. I was always taught it is simply 'better programming'.
> 
>    It is a current loathing of mine simply because we are using
>    a non-optimizing compiler just now; 3 out of the original 5 s/w
>    engineers have left and their stuff is litter with this and, hey,
>    guess what, we're short on space on the target! If they had used
>    constants like this we would have an extra couple of percent -
>    not much, but for a little thought when programming, it is a couple
>    of percent we wouldn't have to be scrapping together now! As a side
>    effect, by reducing code size we increase throughput on the target
>    too :-)
> 
> 3. I best not even get into the whole 'why is everything a 'float' and
>    not user-defined float types' thing... This smacks of 'C' programs
>    not Ada programs.
> 
> N J Chackowsky wrote:
> >
> [snip]
> >
> --
> The views expressed here are personal and not those of BAE Systems.



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

* Re: School Assignment--Can this be simplified?
  2000-08-31 18:35   ` N J Chackowsky
@ 2000-08-31 19:19     ` N J Chackowsky
  2000-08-31 19:33       ` Pascal Obry
  0 siblings, 1 reply; 15+ messages in thread
From: N J Chackowsky @ 2000-08-31 19:19 UTC (permalink / raw)


And here's the revised version. Interesting that there is no pow()
function defined in elementary_functions at all. Where should I look for
standard numerical constants such as e, pi, etc.?

NJC.

-----------------------------------------------------------------------
--| Calculates and displays the relative humidity based on 
--| the temperature and dew point.
--|
--| Author: N J Chackowsky, Brandon School Division No. 40
--| Last modified: 2000 08 31
-----------------------------------------------------------------------

   with ada.text_io;
   with ada.integer_text_io;
   with ada.float_text_io;
   with ada.numerics.elementary_functions; 

   procedure humidity is
      km : constant float := 5438.0;  -- magic multiplier
      c2k : constant float := 273.15;  -- Celcius -> Kelvin conversion
term

      tempc, tempk : float;   	-- temperature in C and K
      dewc, dewk : float;	-- dew point in C and K
      rh : float;		-- relative humidity
   
   begin -- humidity
   
   	-- Get the temperature and dew point
      ada.text_io.put(item => "What is the temperature in degrees
Celcius? ");
      ada.float_text_io.get(item => tempc);
      ada.text_io.put(item => "What is the dew point in degrees Celcius?
");
      ada.float_text_io.get(item => dewc);
   
   	-- Convert from C to K
      tempk := tempc + c2k;		--probably revise this with a function call
      dewk := dewc + c2k;			--in the next lesson.
   
      rh := ada.numerics.elementary_functions.
		exp(km * (1.0/tempk - 1.0/dewk)) * 100.0;

   	-- Display results
      ada.text_io.put(item => "The relative humidity is ");
      ada.float_text_io.put(item => rh, fore => 1, aft => 1, exp => 0);
      ada.text_io.put(item => "%");
   
   end humidity;



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

* Re: School Assignment--Can this be simplified?
  2000-08-31 19:19     ` N J Chackowsky
@ 2000-08-31 19:33       ` Pascal Obry
  2000-08-31 23:03         ` Nick J Chackowsky
  2000-09-01  1:02         ` tmoran
  0 siblings, 2 replies; 15+ messages in thread
From: Pascal Obry @ 2000-08-31 19:33 UTC (permalink / raw)



N J Chackowsky <chackowsky@brandonsd.mb.ca> writes:

> And here's the revised version. Interesting that there is no pow()
> function defined in elementary_functions at all. 

Sure there is ! What about the "**" operator :)

> Where should I look for
> standard numerical constants such as e, pi, etc.?
> 

In Ada.Numerics.

<<
  3.      package Ada.Numerics is
             pragma Pure(Numerics);
             Argument_Error : exception;
             Pi : constant :=
               3.14159_26535_89793_23846_26433_83279_50288_41971_69399_37511;
             e  : constant :=
               2.71828_18284_59045_23536_02874_71352_66249_77572_47093_69996;
          end Ada.Numerics;
>>

Pascal.

-- 

--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--|         http://perso.wanadoo.fr/pascal.obry
--|
--| "The best way to travel is by means of imagination"



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

* Re: School Assignment--Can this be simplified?
  2000-08-31 19:33       ` Pascal Obry
@ 2000-08-31 23:03         ` Nick J Chackowsky
  2000-09-01  1:02         ` tmoran
  1 sibling, 0 replies; 15+ messages in thread
From: Nick J Chackowsky @ 2000-08-31 23:03 UTC (permalink / raw)


Okay, how embarassing. Of course that works. I had tried it before and was
getting compile errors, but I needed to use the ** operator from
ada.numerics.elementary_functions rather than the default ** operator. What
a GOOF I am.

Thanks again for all the help, Pascal.

Bye.


"Pascal Obry" <p.obry@wanadoo.fr> wrote in message
news:uitsh8bv6.fsf@wanadoo.fr...
>
> N J Chackowsky <chackowsky@brandonsd.mb.ca> writes:
>
> > And here's the revised version. Interesting that there is no pow()
> > function defined in elementary_functions at all.
>
> Sure there is ! What about the "**" operator :)
>
> > Where should I look for
> > standard numerical constants such as e, pi, etc.?
> >
>
> In Ada.Numerics.
>
> <<
>   3.      package Ada.Numerics is
>              pragma Pure(Numerics);
>              Argument_Error : exception;
>              Pi : constant :=
>
3.14159_26535_89793_23846_26433_83279_50288_41971_69399_37511;
>              e  : constant :=
>
2.71828_18284_59045_23536_02874_71352_66249_77572_47093_69996;
>           end Ada.Numerics;
> >>
>
> Pascal.
>
> --
>
> --|------------------------------------------------------
> --| Pascal Obry                           Team-Ada Member
> --| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
> --|------------------------------------------------------
> --|         http://perso.wanadoo.fr/pascal.obry
> --|
> --| "The best way to travel is by means of imagination"





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

* Re: School Assignment--Can this be simplified?
  2000-08-31 19:33       ` Pascal Obry
  2000-08-31 23:03         ` Nick J Chackowsky
@ 2000-09-01  1:02         ` tmoran
  2000-09-01  4:14           ` Robert Dewar
  1 sibling, 1 reply; 15+ messages in thread
From: tmoran @ 2000-09-01  1:02 UTC (permalink / raw)


>> And here's the revised version. Interesting that there is no pow()
>> function defined in elementary_functions at all.
>
>Sure there is ! What about the "**" operator :)
  I gather the pow function takes two double precision arguments.
The ** operator, as I read the ARM, only takes integers as the
exponent.



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

* Re: School Assignment--Can this be simplified?
  2000-09-01  1:02         ` tmoran
@ 2000-09-01  4:14           ` Robert Dewar
  2000-09-01 17:51             ` tmoran
  0 siblings, 1 reply; 15+ messages in thread
From: Robert Dewar @ 2000-09-01  4:14 UTC (permalink / raw)


In article <4fDr5.155496$i5.2240834@news1.frmt1.sfba.home.com>,
  tmoran@bix.com wrote:

> The ** operator, as I read the ARM, only takes integers as the
> exponent.

I am afraid this is not a very good effort at ARM reading!
Certainly if you read the entry on the integer operations
that's what you will find, but in A.5.1 my copy of the RM
has:

  function "**" (Left, Right : Float_Type'Base)
    return Float_Type'Base;

Those don't look like integers to me :-)


Sent via Deja.com http://www.deja.com/
Before you buy.



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

* Re: School Assignment--Can this be simplified?
  2000-09-01  4:14           ` Robert Dewar
@ 2000-09-01 17:51             ` tmoran
  2000-09-02  3:12               ` Robert Dewar
  0 siblings, 1 reply; 15+ messages in thread
From: tmoran @ 2000-09-01 17:51 UTC (permalink / raw)


>that's what you will find, but in A.5.1 my copy of the RM
  It would be nice if in the next revision the ARM index entry on
"operator" "**" would also include a reference to the
Ada.Numerics.[Generic]_Elementary_Functions."**" function/operator.



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

* Re: School Assignment--Can this be simplified?
  2000-09-01 17:51             ` tmoran
@ 2000-09-02  3:12               ` Robert Dewar
  0 siblings, 0 replies; 15+ messages in thread
From: Robert Dewar @ 2000-09-02  3:12 UTC (permalink / raw)


In article <E0Sr5.157544$i5.2287748@news1.frmt1.sfba.home.com>,
  tmoran@bix.com wrote:
> >that's what you will find, but in A.5.1 my copy of the RM
>   It would be nice if in the next revision the ARM index entry
on
> "operator" "**" would also include a reference to the
> Ada.Numerics.[Generic]_Elementary_Functions."**"
function/operator.


No point in posting a comment like that here :-) Follow
the documented procedures and send a proper comment as
suggested in the RM!


Sent via Deja.com http://www.deja.com/
Before you buy.



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

end of thread, other threads:[~2000-09-02  3:12 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-08-30  0:00 School Assignment--Can this be simplified? N J Chackowsky
2000-08-30  0:00 ` Pascal Obry
2000-08-30  0:00 ` Keith Thompson
2000-08-30  0:00 ` tmoran
2000-08-31  0:00 ` Martin Dowie
2000-08-31 11:31   ` Simon Wright
2000-08-31 11:51     ` Martin Dowie
2000-08-31 18:35   ` N J Chackowsky
2000-08-31 19:19     ` N J Chackowsky
2000-08-31 19:33       ` Pascal Obry
2000-08-31 23:03         ` Nick J Chackowsky
2000-09-01  1:02         ` tmoran
2000-09-01  4:14           ` Robert Dewar
2000-09-01 17:51             ` tmoran
2000-09-02  3:12               ` Robert Dewar

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