comp.lang.ada
 help / color / mirror / Atom feed
From: Al Christians <achrist@easystreet.com>
Subject: Re: C++ to Ada95 -- bitten!
Date: Sat, 09 Sep 2000 19:28:19 -0700
Date: 2000-09-09T19:28:19-07:00	[thread overview]
Message-ID: <39BAF1C3.FECD497B@easystreet.com> (raw)
In-Reply-To: 6oBu5.666$zo.17816@news1.mts.net

If you have a problem understanding what is going on, walk in smaller
steps.  I tend to overuse parentheses and split complications into
separate statements.  For example:

 Really_Small_Number := 2.7183 ** (-5340);
 g := (Really_Small_Number / f) + 21.5;

That's probably not what you wanted. How about:
 
 G_Exponent := -5340.0/ f;
 g := 2.7183 ** G_Exponent + 21.5; 		

If you break the expression up into separate statements, the
statement number out of the crash yields more information. 
		
Sometimes my code is criticized for including redundant parentheses,
but that's much better than spacing that suggests an order of operations
to humans but doesn't mean anything to the compiler.

Your: 

>       g := 2.7183 ** (-5340)/f + 21.5;

has  (-5340)/f closely spaced as a clue that it happens before the 
exponentiation.  Check the reference manual (4.5) for the facts. 
I'd always make it explicit, either

       g := ((2.7183 ** (-5340))/f) + 21.5;

or

       g := (2.7183 ** (-5340/f)) + 21.5;


I might omit the outermost set of parens, since everyone(?) knows(?)
that addition happens last.  It just depends on the situation and
on who I guessed might eventually be reading my code.


Al


Nick J Chackowsky wrote:
> 
> So just when I think I'm getting the idea of Ada95, along comes this
> problem, demonstrated by an admittedly contrived program. Gnat 3.13p crashes
> trying to compile this:
> 
>    with ada.float_text_io;
>    with ada.numerics;
>    with ada.numerics.elementary_functions;
>    use ada.numerics.elementary_functions;
>    procedure crash is
>       f, g : float;
>    begin
>       ada.float_text_io.get(f);
>       g := 2.7183 ** (-5340)/f + 21.5;
>    end crash;
> 
> Now, I can eliminate the problem by changing the -5340 to -5340.0, but it
> took me a *long* time to see that (in a student's code). I actually thought
> I had a virus infecting the student's computer until I watched the "problem"
> follow him from workstation to workstation!
> 
> I guess my question is, what _category_ of error is this? I know to look
> (now) for an integer in a floating-point calculation, but that doesn't tell
> me what _else_ I should be looking for to avoid these "virus-like" crashes.
> 
> --
> =====================================
> N J Chackowsky
> Brandon, MB  Canada



  reply	other threads:[~2000-09-10  2:28 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-09-10  1:24 C++ to Ada95 -- bitten! Nick J Chackowsky
2000-09-10  2:28 ` Al Christians [this message]
2000-09-10  3:07 ` (null)
2000-09-10  4:49   ` Nick J Chackowsky
2000-09-10  7:39     ` Laurent Guerby
2000-09-10 15:47       ` 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