comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: Unsigned Integer Restraint Errors
Date: Mon, 12 Mar 2007 14:22:04 -0500
Date: 2007-03-12T14:22:04-05:00	[thread overview]
Message-ID: <et4cma$o0v$1@jacob-sparre.dk> (raw)
In-Reply-To: 1173726806.656979.305660@8g2000cwh.googlegroups.com

"frikk" <frikker@gmail.com> wrote in message
news:1173726806.656979.305660@8g2000cwh.googlegroups.com...
> Thank you for the input. This leaves me with 2 questions:
> 1.  If my input is 2**35, wouldn't this still render the Integer type
> inaccurate? The goal is to be able to use 64 bit integers... I'm
> referring to your example of UNSIGNED_LONG_INT := UNSIGNED_LONG_INT
> (Integer'(X));

The real problem here is that you are trying to use modular types in a
checked way. As others have noted, they're not really intended for that.
It's best to avoid using modular types such that they'll raise an exception.

Ada has signed, checked integers, and unsigned, unchecked integers. It
doesn't have unsigned, checked integers. That omission is only a problem if
you need checked, maximum range unsigned integers; usually, you should just
use an appropriate signed integer type:

type Really_Big_Unsigned_Int is range 0 .. 2**63-1;

This type will have the overflow and range checking you've looking for. Even
better is to replace the upper bound by something that is appropriate for
your application:

   Max_Widgets : constant := 2**40;
   type Widget_Count is range 0 .. Max_Widgets;

The only problem here is that your upper bound has to be somewhat smaller
than the maximum allowed. Given the immense range of 64-bit integers, I have
a hard time imagining a case where that would be a problem (it's rarely a
problem even for 32-bit integers).

Note that this sort of declaration will work on any Ada compiler that
supports 64-bit numbers, and if it doesn't, you'll get a convenient error
message.

                         Randy.





  reply	other threads:[~2007-03-12 19:22 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-03-12 15:07 Unsigned Integer Restraint Errors frikk
2007-03-12 16:27 ` Georg Bauhaus
2007-03-12 17:17 ` Adam Beneschan
2007-03-12 17:23 ` Adam Beneschan
2007-03-12 18:11   ` frikk
2007-03-12 20:00     ` frikk
2007-03-12 20:07       ` Adam Beneschan
2007-03-12 18:00 ` Dmitry A. Kazakov
2007-03-12 19:00   ` Martin Krischik
2007-03-12 21:13     ` Dmitry A. Kazakov
2007-03-12 19:13   ` frikk
2007-03-12 19:22     ` Randy Brukardt [this message]
2007-03-13  3:13       ` Jeffrey R. Carter
2007-03-13  3:00         ` Randy Brukardt
2007-03-13 12:09           ` frikk
2007-03-13 14:58             ` frikk
2007-03-13 15:31               ` frikk
2007-03-13 15:59                 ` Robert A Duff
2007-03-13 16:18                 ` Dmitry A. Kazakov
2007-03-13 16:21                 ` Jeffrey R. Carter
2007-03-13 16:04               ` Adam Beneschan
2007-03-13 16:41                 ` Adam Beneschan
2007-03-13 16:42                   ` Adam Beneschan
2007-03-14 14:06                     ` frikk
2007-03-13 17:23                 ` Dmitry A. Kazakov
2007-03-13 17:31                   ` Adam Beneschan
2007-03-14  0:54                   ` Jeffrey R. Carter
2007-03-16 13:38                   ` frikk
2007-03-13 16:16           ` Jeffrey R. Carter
2007-03-12 21:04     ` Dmitry A. Kazakov
replies disabled

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