comp.lang.ada
 help / color / mirror / Atom feed
* Naturals and discrete types
@ 2001-11-01 22:05 Clueless
  2001-11-01 22:35 ` Matthew Heaney
                   ` (2 more replies)
  0 siblings, 3 replies; 16+ messages in thread
From: Clueless @ 2001-11-01 22:05 UTC (permalink / raw)


Recently I've been monkeying with the Ada.Command_Line package.

Now I'm running into trouble reading the argument count.

The Ada.Command_Line package returns a Natural type, however I've been
attempting to count the arguments using the Range attribute which expects
a discrete type.
What I've been doing is converting the Natural to an Integer using the
Value attribute. Sometimes this works, and sometimes I get an Exception.

Is it possible to get the 'Range function to accept a Natural type? Or am
I trekking down the wrong path entirely? It seems there has to be a better
way to do this.

Also, I've been checking my Ada docs to find out if the Natural type is
considered to be a Discrete or Real type by the GNAT compiler, but I
havent found any specific info yet. Although the compiler messages
certainly seem to indicate that Natural is a subtype of Real.

Forgive me if this question seems mundane. I've never really had to worry
about the difference back when I was doing C. Heh.

Any clarification, or pointers to the pertinent docs, would be helpful.

Clueless
McDoobie
chris@dont.spam.me



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

* Re: Naturals and discrete types
  2001-11-01 22:05 Naturals and discrete types Clueless
@ 2001-11-01 22:35 ` Matthew Heaney
  2001-11-01 22:59 ` Clueless
  2001-11-02 17:24 ` Ted Dennison
  2 siblings, 0 replies; 16+ messages in thread
From: Matthew Heaney @ 2001-11-01 22:35 UTC (permalink / raw)



"Clueless" <chris@dont.spam.me> wrote in message
news:oGjE7.221593$K6.106487221@news2...
> The Ada.Command_Line package returns a Natural type, however I've been
> attempting to count the arguments using the Range attribute which expects
> a discrete type.

The 'Range attribute describes the range of values in a scalar type.  If you
want to know you many command line arguments there are, call Argument_Count.

> What I've been doing is converting the Natural to an Integer using the
> Value attribute. Sometimes this works, and sometimes I get an Exception.

Huh?  Natural is already an Integer (it's a subtype).


> Is it possible to get the 'Range function to accept a Natural type? Or am
> I trekking down the wrong path entirely? It seems there has to be a better
> way to do this.

You seem to be confused...

> Also, I've been checking my Ada docs to find out if the Natural type is
> considered to be a Discrete or Real type by the GNAT compiler, but I
> havent found any specific info yet. Although the compiler messages
> certainly seem to indicate that Natural is a subtype of Real.

It's a discrete type.







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

* Re: Naturals and discrete types
  2001-11-01 22:05 Naturals and discrete types Clueless
  2001-11-01 22:35 ` Matthew Heaney
@ 2001-11-01 22:59 ` Clueless
  2001-11-02 15:05   ` Marin David Condic
  2001-11-02 17:24 ` Ted Dennison
  2 siblings, 1 reply; 16+ messages in thread
From: Clueless @ 2001-11-01 22:59 UTC (permalink / raw)


In article <oGjE7.221593$K6.106487221@news2>, "Clueless"
<chris@dont.spam.me> wrote:

> Recently I've been monkeying with the Ada.Command_Line package.
> 
> Now I'm running into trouble reading the argument count.
> 
> The Ada.Command_Line package returns a Natural type, however I've been
> attempting to count the arguments using the Range attribute which
> expects a discrete type.
> What I've been doing is converting the Natural to an Integer using the
> Value attribute. Sometimes this works, and sometimes I get an Exception.
> 
> Is it possible to get the 'Range function to accept a Natural type? Or
> am I trekking down the wrong path entirely? It seems there has to be a
> better way to do this.
> 
> Also, I've been checking my Ada docs to find out if the Natural type is
> considered to be a Discrete or Real type by the GNAT compiler, but I
> havent found any specific info yet. Although the compiler messages
> certainly seem to indicate that Natural is a subtype of Real.
> 
> Forgive me if this question seems mundane. I've never really had to
> worry about the difference back when I was doing C. Heh.
> 
> Any clarification, or pointers to the pertinent docs, would be helpful.
> 
> Clueless
> McDoobie
> chris@dont.spam.me

Alright...I've got the Command_Line package figured out.

Basically, I stopped trying to rewrite what was already there. Heh.

However, I'm still a little fuzzy on the best way to convert between real
and discrete types.

Thanks for any pointers.

McDoobie
Clueless
chris@dont.spam.me



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

* RE: Naturals and discrete types
@ 2001-11-01 23:06 Beard, Frank
  2001-11-01 23:20 ` Clueless
  0 siblings, 1 reply; 16+ messages in thread
From: Beard, Frank @ 2001-11-01 23:06 UTC (permalink / raw)
  To: 'comp.lang.ada@ada.eu.org'


-----Original Message-----
From: Clueless [mailto:chris@dont.spam.me]

> However, I'm still a little fuzzy on the best way to convert between real
> and discrete types.

      I : integer := 0;
      Y : float := 3.14;
   begin  
      I := integer(Y);  -- now I = 3;


or

      I : integer := 9;
      Y : float := 0.0;
   begin  
      Y := float(I);  -- now Y ~ 9.0;




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

* RE: Naturals and discrete types
  2001-11-01 23:06 Beard, Frank
@ 2001-11-01 23:20 ` Clueless
  2001-11-02  3:05   ` DuckE
  0 siblings, 1 reply; 16+ messages in thread
From: Clueless @ 2001-11-01 23:20 UTC (permalink / raw)


In article <mailman.1004656086.22510.comp.lang.ada@ada.eu.org>, "Beard,
Frank" <beardf@spawar.navy.mil> wrote:

> -----Original Message-----
> From: Clueless [mailto:chris@dont.spam.me]
> 
>> However, I'm still a little fuzzy on the best way to convert between
>> real and discrete types.
> 
>       I : integer := 0;
>       Y : float := 3.14;
>    begin
>       I := integer(Y);  -- now I = 3;
> 
> 
> or
> 
>       I : integer := 9;
>       Y : float := 0.0;
>    begin
>       Y := float(I);  -- now Y ~ 9.0;

Everything is so damn simple isnt it? ;->

And I remember covering this subject early on in my textbook.
I'm a typical greenhorn I guess, making everything more complicated than
it has to be.

Thanks for your patience.

Clueless
chris@dont.spam.me



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

* Re: Naturals and discrete types
  2001-11-01 23:20 ` Clueless
@ 2001-11-02  3:05   ` DuckE
  0 siblings, 0 replies; 16+ messages in thread
From: DuckE @ 2001-11-02  3:05 UTC (permalink / raw)


One other point (especially if you're coming from C).  When you convert a
floating point value to an integer value using Integer( value ), Ada rounds,
it does not trucate.

Example:

WITH Ada.Integer_Text_Io;
WITH Ada.Text_Io;

PROCEDURE Conversions IS
BEGIN
  Ada.Text_Io.Put( "Integer(3.4) = " );
  Ada.Integer_Text_Io.Put( Integer( 3.4 ), 2 );
  Ada.Text_Io.New_Line;
  Ada.Text_Io.Put( "Integer(3.6) = " );
  Ada.Integer_Text_Io.Put( Integer( 3.6 ), 2 );
  Ada.Text_Io.New_Line;
END Conversions;

Output:
  Integer(3.4) =  3
  Integer(3.6) =  4

If you need to trucate, you can use Integer( Float'Truncation( value ) )

SteveD

> Everything is so damn simple isnt it? ;->
>
> And I remember covering this subject early on in my textbook.
> I'm a typical greenhorn I guess, making everything more complicated than
> it has to be.
>
> Thanks for your patience.
>
> Clueless
> chris@dont.spam.me





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

* Re: Naturals and discrete types
  2001-11-01 22:59 ` Clueless
@ 2001-11-02 15:05   ` Marin David Condic
  0 siblings, 0 replies; 16+ messages in thread
From: Marin David Condic @ 2001-11-02 15:05 UTC (permalink / raw)


It depends on what you want to do and what behavior you need from the
conversion.

Between your basic Integer and Float, you can use a standard type
conversion:

Integer_Thing    := Integer (Float_Thing) ;

However, depending on how you want to handle rounding/truncation, etc you
might want some other behavior. Look at Annex K - there are attributes that
will do just about anything you want. (Really! Annex K is your friend!
*LOTS* of powerful stuff in there. Read it thoroughly.)

MDC
--
Marin David Condic
Senior Software Engineer
Pace Micro Technology Americas    www.pacemicro.com
Enabling the digital revolution
e-Mail:    marin.condic@pacemicro.com
Web:      http://www.mcondic.com/


"Clueless" <chris@dont.spam.me> wrote in message
news:EtkE7.221650$K6.106545662@news2...
>
> However, I'm still a little fuzzy on the best way to convert between real
> and discrete types.
>






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

* Re: Naturals and discrete types
  2001-11-01 22:05 Naturals and discrete types Clueless
  2001-11-01 22:35 ` Matthew Heaney
  2001-11-01 22:59 ` Clueless
@ 2001-11-02 17:24 ` Ted Dennison
  2001-11-02 18:19   ` Preben Randhol
  2 siblings, 1 reply; 16+ messages in thread
From: Ted Dennison @ 2001-11-02 17:24 UTC (permalink / raw)


In article <oGjE7.221593$K6.106487221@news2>, Clueless says...
>Also, I've been checking my Ada docs to find out if the Natural type is
>considered to be a Discrete or Real type by the GNAT compiler, but I
>havent found any specific info yet. Although the compiler messages
>certainly seem to indicate that Natural is a subtype of Real.

Boy are you confused!

Natural is a subtype of Integer with a range of 0 to Integer'last. It is defined
in package Standard (see
http://www.ada-auth.org/~acats/arm-html/RM-A-1.html#I4539 ). Thus you do *not*
need a conversion to use Natural where Integer is usable. However, you *do* need
a conversion ( "Float(My_Natural)" ) to use it where a Float is expected. There
is no type named "Real" (unless you count the one in Interfaces.Fortran), but
there are a class of types called "real", which includes floating-point and
fixed point types. The only predefined floating-point types is "Float" (and
"Long_Float" on some systems), and the only predefined fixed-point type is
"Duration".

This is all pretty basic stuff. If it is confusing you, you probably ought to go
read a good Ada book. If you don't want to shell out cash for one, there's one
online at http://www.it.bton.ac.uk/staff/je/adacraft/ . At least read through
chapter 5 there (http://www.it.bton.ac.uk/staff/je/adacraft/ch05.htm)

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html

No trees were killed in the sending of this message. 
However a large number of electrons were terribly inconvenienced.



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

* Re: Naturals and discrete types
  2001-11-02 17:24 ` Ted Dennison
@ 2001-11-02 18:19   ` Preben Randhol
  2001-11-02 18:51     ` Marin David Condic
  0 siblings, 1 reply; 16+ messages in thread
From: Preben Randhol @ 2001-11-02 18:19 UTC (permalink / raw)


On Fri, 02 Nov 2001 17:24:40 GMT, Ted Dennison wrote:
> In article <oGjE7.221593$K6.106487221@news2>, Clueless says...
>>Also, I've been checking my Ada docs to find out if the Natural type is
>>considered to be a Discrete or Real type by the GNAT compiler, but I
>>havent found any specific info yet. Although the compiler messages
>>certainly seem to indicate that Natural is a subtype of Real.
> 
> Boy are you confused!
> 
> Natural is a subtype of Integer with a range of 0 to Integer'last. 

Which again comes from basic mathematics. :-)

Preben Randhol
-- 
Please, stop bombing civilians in Afghanistan. One cannot write off
killing innocent children and other civilians as "collateral damage".
A civilian is a civilian whether he or she is American or from another
country in the world.           http://web.amnesty.org/11september.htm



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

* Re: Naturals and discrete types
  2001-11-02 18:19   ` Preben Randhol
@ 2001-11-02 18:51     ` Marin David Condic
  2001-11-03 18:01       ` Nick Roberts
  2001-11-04 11:39       ` Preben Randhol
  0 siblings, 2 replies; 16+ messages in thread
From: Marin David Condic @ 2001-11-02 18:51 UTC (permalink / raw)


Except that when I was in gradeschool and "The New Math" was popular, they
called those "Whole" numbers with "Natural" numbers being from 1..Infinity
(Or Integer'Last, if you just want to get the job done...)

When did Natural numbers start including zero? Was I cutting class that day?

MDC
--
Marin David Condic
Senior Software Engineer
Pace Micro Technology Americas    www.pacemicro.com
Enabling the digital revolution
e-Mail:    marin.condic@pacemicro.com
Web:      http://www.mcondic.com/


"Preben Randhol" <randhol+abuse@pvv.org> wrote in message
news:slrn9u5sp5.v0j.randhol+abuse@kiuk0156.chembio.ntnu.no...
> >
> > Natural is a subtype of Integer with a range of 0 to Integer'last.
>
> Which again comes from basic mathematics. :-)
>






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

* Re: Naturals and discrete types
  2001-11-02 18:51     ` Marin David Condic
@ 2001-11-03 18:01       ` Nick Roberts
  2001-11-04  4:18         ` tmoran
  2001-11-04 11:39       ` Preben Randhol
  1 sibling, 1 reply; 16+ messages in thread
From: Nick Roberts @ 2001-11-03 18:01 UTC (permalink / raw)


> "Preben Randhol" <randhol+abuse@pvv.org> wrote in message
> news:slrn9u5sp5.v0j.randhol+abuse@kiuk0156.chembio.ntnu.no...
> > >
> > > Natural is a subtype of Integer with a range of 0 to Integer'last.
> >
> > Which again comes from basic mathematics. :-)

"Marin David Condic" <dont.bother.mcondic.auntie.spam@[acm.org> wrote in
message news:9rupv9$o5h$1@nh.pace.co.uk...
> Except that when I was in gradeschool and "The New Math" was popular, they
> called those "Whole" numbers with "Natural" numbers being from 1..Infinity
> (Or Integer'Last, if you just want to get the job done...)
>
> When did Natural numbers start including zero? Was I cutting class that
day?

I have it on fairly good authority (a leader in finite mathematics research)
that, as far as mathematicians are concerned, the set of natural numbers
(denoted by a funny N), constitutes the integers from 1 upwards (to what
they call Aleph 0, because they just have to have a fancy name for infinity
;-) They use a funny Z with a plus and 0 for what Ada calls Natural.

Which just goes to show that we're better off not taking too much notice of
mathematicians anyway. They'd say Ada has "too much notation."

:-)


--
Nick Roberts






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

* Re: Naturals and discrete types
  2001-11-03 18:01       ` Nick Roberts
@ 2001-11-04  4:18         ` tmoran
  2001-11-05 15:29           ` Marin David Condic
  0 siblings, 1 reply; 16+ messages in thread
From: tmoran @ 2001-11-04  4:18 UTC (permalink / raw)


> > When did Natural numbers start including zero? Was I cutting class that day?
> that, as far as mathematicians are concerned, the set of natural numbers
> (denoted by a funny N), constitutes the integers from 1 upwards (to what
  "Natural numbers.  The numbers 1, 2, 3, 4, etc.  The same as positive
integers."  Mathematics Dictionary, James & James, Van Nostrand, 1959.



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

* Re: Naturals and discrete types
  2001-11-02 18:51     ` Marin David Condic
  2001-11-03 18:01       ` Nick Roberts
@ 2001-11-04 11:39       ` Preben Randhol
  2001-11-05  2:01         ` Clueless
  1 sibling, 1 reply; 16+ messages in thread
From: Preben Randhol @ 2001-11-04 11:39 UTC (permalink / raw)


On Fri, 2 Nov 2001 13:51:30 -0500, Marin David Condic wrote:
> Except that when I was in gradeschool and "The New Math" was popular, they
> called those "Whole" numbers with "Natural" numbers being from 1..Infinity
> (Or Integer'Last, if you just want to get the job done...)
> 
> When did Natural numbers start including zero? Was I cutting class that day?

Oops! I was a bit too fast in my reply. Of course Natural numbers
doesn't include 0, but I see Ada does. The Ada equivalent of Natural
numbers is Positive. But that Natural numbers are integers and not real
is from mathematics. :-)

Preben Randhol
-- 
"i too once thought that when proved wrong that i lost somehow"
                               - i was hoping, alanis morisette



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

* Re: Naturals and discrete types
  2001-11-04 11:39       ` Preben Randhol
@ 2001-11-05  2:01         ` Clueless
  2001-11-05  2:37           ` Larry Kilgallen
  0 siblings, 1 reply; 16+ messages in thread
From: Clueless @ 2001-11-05  2:01 UTC (permalink / raw)


In article <slrn9uae3o.18q.randhol+abuse@kiuk0156.chembio.ntnu.no>,
"Preben Randhol" <randhol+abuse@pvv.org> wrote:

> On Fri, 2 Nov 2001 13:51:30 -0500, Marin David Condic wrote:
>> Except that when I was in gradeschool and "The New Math" was popular,
>> they called those "Whole" numbers with "Natural" numbers being from
>> 1..Infinity (Or Integer'Last, if you just want to get the job done...)
>> 
>> When did Natural numbers start including zero? Was I cutting class that
>> day?
> 
> Oops! I was a bit too fast in my reply. Of course Natural numbers
> doesn't include 0, but I see Ada does. The Ada equivalent of Natural
> numbers is Positive. But that Natural numbers are integers and not real
> is from mathematics. :-)
> 
> Preben Randhol

Which explains my mixup.

I started counting from 0 instead of 1. I keep forgetting that this is
Ada and not C. Heh.

Thanks.

Clueless
McDoobie
chris@dont.spam.me



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

* Re: Naturals and discrete types
  2001-11-05  2:01         ` Clueless
@ 2001-11-05  2:37           ` Larry Kilgallen
  0 siblings, 0 replies; 16+ messages in thread
From: Larry Kilgallen @ 2001-11-05  2:37 UTC (permalink / raw)


In article <VpmF7.232018$K6.110685248@news2>, "Clueless" <chris@dont.spam.me> writes:

> I started counting from 0 instead of 1. I keep forgetting that this is
> Ada and not C. Heh.

Which you start with in Ada depends on whether you are using
Natural or Positive.  It is best to start with Natural'first
or Positive'first if that is what you mean.  Then many years
later you (or your successor) will understand the purpose.

Even better, when appropriate, is My_Array_Name'first.



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

* Re: Naturals and discrete types
  2001-11-04  4:18         ` tmoran
@ 2001-11-05 15:29           ` Marin David Condic
  0 siblings, 0 replies; 16+ messages in thread
From: Marin David Condic @ 2001-11-05 15:29 UTC (permalink / raw)


O.K. So seeing as how I was not just in the springtime of my senility, and
Natural numbers are what Ada calls "Positive", then what's the story? Was
there some kind of justification for misnaming Whole numbers to be Natural
numbers? Or did someone just manage to drop the ball?

MDC
--
Marin David Condic
Senior Software Engineer
Pace Micro Technology Americas    www.pacemicro.com
Enabling the digital revolution
e-Mail:    marin.condic@pacemicro.com
Web:      http://www.mcondic.com/


<tmoran@acm.org> wrote in message
news:Fk3F7.18654$wj5.9153785@news1.rdc1.sfba.home.com...
> > > When did Natural numbers start including zero? Was I cutting class
that day?
> > that, as far as mathematicians are concerned, the set of natural numbers
> > (denoted by a funny N), constitutes the integers from 1 upwards (to what
>   "Natural numbers.  The numbers 1, 2, 3, 4, etc.  The same as positive
> integers."  Mathematics Dictionary, James & James, Van Nostrand, 1959.





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

end of thread, other threads:[~2001-11-05 15:29 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-11-01 22:05 Naturals and discrete types Clueless
2001-11-01 22:35 ` Matthew Heaney
2001-11-01 22:59 ` Clueless
2001-11-02 15:05   ` Marin David Condic
2001-11-02 17:24 ` Ted Dennison
2001-11-02 18:19   ` Preben Randhol
2001-11-02 18:51     ` Marin David Condic
2001-11-03 18:01       ` Nick Roberts
2001-11-04  4:18         ` tmoran
2001-11-05 15:29           ` Marin David Condic
2001-11-04 11:39       ` Preben Randhol
2001-11-05  2:01         ` Clueless
2001-11-05  2:37           ` Larry Kilgallen
  -- strict thread matches above, loose matches on Subject: below --
2001-11-01 23:06 Beard, Frank
2001-11-01 23:20 ` Clueless
2001-11-02  3:05   ` DuckE

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