comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <jrcarter@acm.org>
Subject: Re: Positive Floating Range?
Date: Sun, 22 Apr 2001 01:14:03 GMT
Date: 2001-04-22T01:14:03+00:00	[thread overview]
Message-ID: <3AE23051.23F043A6@acm.org> (raw)
In-Reply-To: 817E6.2056$DW1.94361@iad-read.news.verio.net

Dr Nancy's Sweetie wrote:
> 
> I want to specify a subtype for floating point numbers which will
> only allow positive numbers to be entered.  I can do something like
> this:
>       subtype Foo is Float range 0.0..Float'Last;
> 
> and then test for zero myself whenever somebody enters a number,
> but that feels an awful lot like a kludge.

The PragmAda Reusable Components use

   subtype Positive_Real is Real range  Real'Model_Small ..
Real'Safe_Last;

You might find it useful to review the language-defined attributes in
the ARM.

> Also, while I can get a Float, the compiler claims that there's no
> such thing as a "Double".  At first I figured maybe Ada used the
> double all the time (like Perl does), but Float'Machine_Radix is 2,
> and Float'Machine_Mantissa is 24; that's single precision on this
> machine.  How do I get what C calls `double'?

Unlike most other languages, in which you use the numeric types the
language provides, in Ada you generally tell the compiler what your
application needs, and it creates the appropriate numeric type. There is
a predefined floating-point type named "Float" with a guaranteed
precision of at least 6 decimal digits (if that's possible on the
target). There may be other predefined floating-point types, but they're
not guaranteed to exist for all compilers, and so are not portable.

To portably get the maximum precision possible with any compiler (it may
differ from compiler to compiler, but will be the maximum available with
each compiler), you can use the constant System.Max_Digits:

with System;
...
   type Big is digits System.Max_Digits;

> 
> I found a reference to things like this:
> 
>         type foo is delta 0.000001 digits 20;

This is a fixed-point type, not a floating-point type. Floating-point
types are defined using

   type _name_ is digits _integer_number_ [range
_real_range_constraint_] ;

> 
> But it's not clear what happens if I specify something which is too
> precise (or too many digits) for the machine's natural double-word size.

If the compiler cannot comply with a type definition, you will get a
compiler error.

-- 
Jeff Carter
"I waggle my private parts at your aunties."
Monty Python & the Holy Grail



  parent reply	other threads:[~2001-04-22  1:14 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-04-21  3:21 Positive Floating Range? Dr Nancy's Sweetie
2001-04-21  7:25 ` tmoran
2001-04-21 14:57 ` Samuel T. Harris
2001-04-22  1:14 ` Jeffrey Carter [this message]
2001-04-23 14:13 ` Marin David Condic
2001-05-01 20:40 ` Georg Bauhaus
replies disabled

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