comp.lang.ada
 help / color / mirror / Atom feed
From: "Steve Doiel" <nospam_steved@pacifier.com>
Subject: Re: Ada or C++ acting 'correctly' here?
Date: 1999/02/27
Date: 1999-02-27T00:00:00+00:00	[thread overview]
Message-ID: <36d83977.0@news.pacifier.com> (raw)
In-Reply-To: 7b8c7u$sj1@drn.newsguy.com

>Now. Which would you consider the correct language behaviour?
>(same machine, Pentium pro, Linux.
>

Both are giving the correct language behavior.  For their language.

The nature of C/C++ is to not notify you of overflows or suprises in
calculations.

The nature of Ada is to let you know when suprises occur.

Your example is a good example of why I prefer Ada.

Here's another.  Consider the following C++ program

#include <iostream.h>

void main()
{
  int value = -4;
  unsigned divisor = 2;

  cout << value / divisor << endl;
}

Output:
  214748364

And here's it's Ada Counterpart:

WITH Ada.Integer_Text_Io;
WITH Ada.Text_Io;
WITH Interfaces; USE Interfaces;
PROCEDURE Demo IS
  value   : Integer_32 := -4;
  divisor : Unsigned_32 := 2;
BEGIN
  Ada.Integer_Text_Io.Put( value / divisor );
  Ada.Text_Io.New_Line;
END Demo;

Which gives a compile error:
  demo.adb:8:34: invalid operand types for operator "/"
  demo.adb:8:34: left operand has type "Interfaces.Integer_32"
  demo.adb:8:34: right operand has type "Interfaces.Unsigned_32"
  gnatmake: "demo.adb" compilation error

The C++ program is more than happy to give you the wrong answer (as it must
in accordance with the language standard).  But the Ada compiler tells you
you're doing something silly.

BTW: I ran into this bug in a C program that was calculating an average
where the sum was an integer and the count was unsigned.

SteveD






  parent reply	other threads:[~1999-02-27  0:00 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-02-27  0:00 Ada or C++ acting 'correctly' here? bill
1999-02-27  0:00 ` Florian Weimer
     [not found]   ` <36e8e201.48455851@netnews.worldnet.att.net>
1999-03-18  0:00     ` bglbv
1999-02-27  0:00 ` Claudius Proculus
1999-02-27  0:00   ` Biju Thomas
1999-02-28  0:00     ` robert_dewar
1999-02-28  0:00       ` Florian Weimer
1999-03-01  0:00         ` dewar
1999-02-27  0:00 ` Steve Doiel [this message]
1999-02-27  0:00 ` Matthew Heaney
1999-02-27  0:00 ` Gautier.DeMontmollin
1999-02-28  0:00 ` William McKenzie
1999-03-01  0:00   ` fraser
1999-03-01  0:00   ` Hubert B. Keller
1999-02-28  0:00 ` robert_dewar
1999-03-02  0:00 ` Willliam V
1999-03-02  0:00   ` Pascal Obry
1999-03-02  0:00   ` Gautier.DeMontmollin
1999-03-02  0:00     ` kvisko
1999-03-02  0:00   ` robert_dewar
1999-03-02  0:00   ` SpamSpamSpam
replies disabled

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