comp.lang.ada
 help / color / mirror / Atom feed
From: Marin David Condic <condicma@bogon.pwfl.com>
Subject: Re: Modular type. What is it and why?
Date: 1999/04/05
Date: 1999-04-05T00:00:00+00:00	[thread overview]
Message-ID: <3708F1E4.4082DC78@pwfl.com> (raw)
In-Reply-To: 7ean3c$79m$1@eol.dd.chalmers.se

Staffan Dittmer wrote:
> 
> This might be a simple one but i can't figure it out.
> What is a modular type, as opposed to the integer?
> When is it useful?
> Since ada relies so heavily on types I guess it's not by accident that the standard library includes modular_io.
> But why?
> Could someone please enlighten me on this subject.
> An example of usage would be nice.
> I've checked the RM but to be perfectly honest I didn't understand it.
> 

Integer types are pretty much what you would expect: a means of
representing integer values both positive and negative. A typical
integer may, for example, be used to represent numbers between
-32768..32767. Ada gives you predefined integer types known as Integer,
Long_Integer, Short_Integer and (at the discretion of the
implementation) there may be others as well.

Modular types are also a means of representing integer values, but only
non-negative values. For example, a declaration such as:

type My_Modular_Type is mod 2**16 ;

would be able to take on values between 0..65535. Ada does not define
any standard Modular types - unless you want to count what you may find
in the packages Interfaces and Interfaces.C (See appendix B.2 and B.3 of
the ARM) to be "standard". The real interesting usage of Modular types
has to do with the operations on them. Arithmetic has "wrap around"
semantics - that is:

X : My_Modular_Type := 65535 ;

...

X := X + 1 ; -- X now = 0.

Also you get logical "and" "or" and "not" between two modular objects.
If you count what is in the package Interfaces (see appendix B.2 of the
ARM) you also get shift and rotate operations on modular types. (Its
just that the types and sizes are going to be implementation defined,
rather than guaranteed sizes by virtue of the language standard.)

Modular types were invented to fill a void in Ada83 - the need that many
embedded systems have for dealing with unsigned integers on which you
could perform bitwise operations. They also prove useful for other
things where the wraparound math is handy.

Hope this helps.

-- 
Marin David Condic
Real Time & Embedded Systems, Propulsion Systems Analysis
United Technologies, Pratt & Whitney, Large Military Engines
M/S 731-95, P.O.B. 109600, West Palm Beach, FL, 33410-9600
***To reply, remove "bogon" from the domain name.***




      parent reply	other threads:[~1999-04-05  0:00 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-04-05  0:00 Modular type. What is it and why? Staffan Dittmer
1999-04-05  0:00 ` David C. Hoos, Sr.
1999-04-06  0:00   ` Mike Silva
1999-04-06  0:00     ` Ole-Hjalmar Kristensen
1999-04-06  0:00     ` Marin David Condic
1999-04-06  0:00       ` Mike Silva
1999-04-06  0:00         ` bglbv
1999-04-07  0:00         ` Ole-Hjalmar Kristensen
1999-04-07  0:00           ` Mike Silva
1999-04-07  0:00             ` Stephen Leake
1999-04-08  0:00             ` Ole-Hjalmar Kristensen
1999-04-06  0:00     ` Mike Silva
1999-04-07  0:00       ` Robert Dewar
1999-04-07  0:00         ` Bret
1999-04-11  0:00         ` Florian Weimer
1999-04-05  0:00 ` Marin David Condic [this message]
replies disabled

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