comp.lang.ada
 help / color / mirror / Atom feed
* simple question on long_float/short_float
@ 2010-09-30  6:17 Nasser M. Abbasi
  2010-09-30  6:58 ` J-P. Rosen
  2010-10-02  9:11 ` Nasser M. Abbasi
  0 siblings, 2 replies; 22+ messages in thread
From: Nasser M. Abbasi @ 2010-09-30  6:17 UTC (permalink / raw)


Ada experts:

Is Ada's long_float like Fortran double precision? and short_float is 
like Fortran single precision (just called real in Fortran)?

Just making sure, as googling this seems not clear, I thought I ask the 
source.

thanks,

--Nasser



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

* Re: simple question on long_float/short_float
  2010-09-30  6:17 simple question on long_float/short_float Nasser M. Abbasi
@ 2010-09-30  6:58 ` J-P. Rosen
  2010-09-30  8:31   ` Nasser M. Abbasi
  2010-10-02  9:11 ` Nasser M. Abbasi
  1 sibling, 1 reply; 22+ messages in thread
From: J-P. Rosen @ 2010-09-30  6:58 UTC (permalink / raw)


Le 30/09/2010 08:17, Nasser M. Abbasi a �crit :
> Ada experts:
> 
> Is Ada's long_float like Fortran double precision? and short_float is
> like Fortran single precision (just called real in Fortran)?
There is nothing in the standard that guarantees this. Float and
Long_Float are just floating point types, with greater accuracy for
Long_Float.

On the other hand, package Interfaces.Fortran provides types Real and
Double_Precision, which are required to match the corresponding Fortran
types.


-- 
---------------------------------------------------------
           J-P. Rosen (rosen@adalog.fr)
Adalog a d�m�nag� / Adalog has moved:
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
Tel: +33 1 45 29 21 52, Fax: +33 1 45 29 25 00



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

* Re: simple question on long_float/short_float
  2010-09-30  6:58 ` J-P. Rosen
@ 2010-09-30  8:31   ` Nasser M. Abbasi
  2010-09-30  8:45     ` Nasser M. Abbasi
                       ` (4 more replies)
  0 siblings, 5 replies; 22+ messages in thread
From: Nasser M. Abbasi @ 2010-09-30  8:31 UTC (permalink / raw)


On 9/29/2010 11:58 PM, J-P. Rosen wrote:
> Le 30/09/2010 08:17, Nasser M. Abbasi a �crit :
>> Ada experts:
>>
>> Is Ada's long_float like Fortran double precision? and short_float is
>> like Fortran single precision (just called real in Fortran)?


> There is nothing in the standard that guarantees this. Float and
> Long_Float are just floating point types, with greater accuracy for
> Long_Float.
>
> On the other hand, package Interfaces.Fortran provides types Real and
> Double_Precision, which are required to match the corresponding Fortran
> types.
>
>

Thanks. Are you saying that in Ada I can't make double precision 
variables and single precision variables? (as in Fortran?)

Is this for real? :)


So, what is Ada's Float? 32 bit or 64 bits?

--Nasser



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

* Re: simple question on long_float/short_float
  2010-09-30  8:31   ` Nasser M. Abbasi
@ 2010-09-30  8:45     ` Nasser M. Abbasi
  2010-09-30  9:59       ` Mark Lorenzen
  2010-09-30 13:30       ` Peter C. Chapin
  2010-09-30  8:46     ` AdaMagica
                       ` (3 subsequent siblings)
  4 siblings, 2 replies; 22+ messages in thread
From: Nasser M. Abbasi @ 2010-09-30  8:45 UTC (permalink / raw)


On 9/30/2010 1:31 AM, Nasser M. Abbasi wrote:
> On 9/29/2010 11:58 PM, J-P. Rosen wrote:
>> Le 30/09/2010 08:17, Nasser M. Abbasi a �crit :
>>> Ada experts:
>>>
>>> Is Ada's long_float like Fortran double precision? and short_float is
>>> like Fortran single precision (just called real in Fortran)?
>
>

>> There is nothing in the standard that guarantees this. Float and
>> Long_Float are just floating point types, with greater accuracy for
>> Long_Float.
>>
>> On the other hand, package Interfaces.Fortran provides types Real and
>> Double_Precision, which are required to match the corresponding Fortran
>> types.
>>

I just looked it up. So, if I use Interfaces.Fortran Reals and 
Double_Precision I think that would be just fine.

I have always thought that long_float and short_float where the same as 
Fortran's double and single precision. This is confusing.

So Ada has

Float,
long_float,
short_float,
Interfaces.Fortran.Reals
Interfaces.Fortran.Double_Precision


--Nasser





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

* Re: simple question on long_float/short_float
  2010-09-30  8:31   ` Nasser M. Abbasi
  2010-09-30  8:45     ` Nasser M. Abbasi
@ 2010-09-30  8:46     ` AdaMagica
  2010-09-30 10:02     ` Georg Bauhaus
                       ` (2 subsequent siblings)
  4 siblings, 0 replies; 22+ messages in thread
From: AdaMagica @ 2010-09-30  8:46 UTC (permalink / raw)


> Thanks. Are you saying that in Ada I can't make double precision
> variables and single precision variables? (as in Fortran?)

Nonsense.

Why don't you have a look in the RM, especially 3.5.7. There you can
find the following definitions:

{Float} In an implementation that supports floating point types with 6
or more digits of precision, the
requested decimal precision for Float shall be at least 6.

{Long_Float} If Long_Float is predefined for an implementation, then
its requested decimal precision
shall be at least 11.

You can define your own float types with the precision you need.



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

* Re: simple question on long_float/short_float
  2010-09-30  8:45     ` Nasser M. Abbasi
@ 2010-09-30  9:59       ` Mark Lorenzen
  2010-09-30 13:30       ` Peter C. Chapin
  1 sibling, 0 replies; 22+ messages in thread
From: Mark Lorenzen @ 2010-09-30  9:59 UTC (permalink / raw)


On 30 Sep., 10:45, "Nasser M. Abbasi" <n...@12000.org> wrote:
>
> I just looked it up. So, if I use Interfaces.Fortran Reals and
> Double_Precision I think that would be just fine.
>
> I have always thought that long_float and short_float where the same as
> Fortran's double and single precision. This is confusing.
>
> So Ada has
>
> Float,
> long_float,
> short_float,
> Interfaces.Fortran.Reals
> Interfaces.Fortran.Double_Precision
>
> --Nasser

You should generally not use the predefined types for anything other
than interfacing with other languages e.g. C or Fortran. Don't think
in terms of "Ada has these types..." but define the types you need.
Ada is not Fortran, so don't code Fortran in Ada.

- Mark L



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

* Re: simple question on long_float/short_float
  2010-09-30  8:31   ` Nasser M. Abbasi
  2010-09-30  8:45     ` Nasser M. Abbasi
  2010-09-30  8:46     ` AdaMagica
@ 2010-09-30 10:02     ` Georg Bauhaus
  2010-09-30 15:37     ` Jeffrey Carter
  2010-09-30 15:56     ` Adam Beneschan
  4 siblings, 0 replies; 22+ messages in thread
From: Georg Bauhaus @ 2010-09-30 10:02 UTC (permalink / raw)


On 30.09.10 10:31, Nasser M. Abbasi wrote:

> So, what is Ada's Float? 32 bit or 64 bits?

To be used only when their RM defined meaning is understood ;-)
Is anything wrong with taking control of floating point type
definitions, as always? And say, expressis verbis, what kind of
floating point numbers you need?



Georg



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

* Re: simple question on long_float/short_float
  2010-09-30  8:45     ` Nasser M. Abbasi
  2010-09-30  9:59       ` Mark Lorenzen
@ 2010-09-30 13:30       ` Peter C. Chapin
  1 sibling, 0 replies; 22+ messages in thread
From: Peter C. Chapin @ 2010-09-30 13:30 UTC (permalink / raw)


On 2010-09-30 04:45, Nasser M. Abbasi wrote:

> So Ada has
> 
> Float,
> long_float,
> short_float,
> Interfaces.Fortran.Reals
> Interfaces.Fortran.Double_Precision

A common recommendation is to avoid using the built-in types directly.
Instead define your own:

type My_Floating_Type is digits 12;

If the implementation has a floating point type with at least 12 decimal
digits of precision it will use it as the base type for My_Floating_Type
and all will be well. If the implementation can't handle that much
precision in any of its built-in types you will get a compile time
error. For example you could try

type My_Floating_Type is digits 350;

But I doubt if there is any implementation that will honor that.

The types in Interfaces.Fortran are intended for the case where you are
exchanging data with a corresponding Fortran compiler. It sounds like
you might, in fact, be doing that.

Peter



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

* Re: simple question on long_float/short_float
  2010-09-30  8:31   ` Nasser M. Abbasi
                       ` (2 preceding siblings ...)
  2010-09-30 10:02     ` Georg Bauhaus
@ 2010-09-30 15:37     ` Jeffrey Carter
  2010-09-30 18:22       ` Nasser M. Abbasi
  2010-09-30 21:21       ` Peter C. Chapin
  2010-09-30 15:56     ` Adam Beneschan
  4 siblings, 2 replies; 22+ messages in thread
From: Jeffrey Carter @ 2010-09-30 15:37 UTC (permalink / raw)


On 09/30/2010 01:31 AM, Nasser M. Abbasi wrote:
>
> Thanks. Are you saying that in Ada I can't make double precision
> variables and single precision variables? (as in Fortran?)
>
> Is this for real? :)

Are you saying that in Fortran you can only make floating-point variables with 
one of two precisions?

Is this for real?

In Ada, you can create floating-point types (and declare variables of those 
types) with any precision supported by the compiler, from 1 to System.Max_Digits 
decimal digits of precision.

> So, what is Ada's Float? 32 bit or 64 bits?

So in Fortran, you cannot make use of the 80-bit floating-point type supported 
by most PC FPUs?

In most languages (other than Ada), you have to use the numeric types provided 
by the language, and select the types that are the closest fit to the 
requirements of the problem. This is part of what is known as "translating the 
problem into the solution space", which makes the software harder to understand, 
since the reader has to understand both the problem and the translation chosen.

In Ada, one can declare numeric types to match the requirements of the problem. 
This is part of what is known as "modeling the problem in the software", which 
is intended to make the software easier to understand, since there is no 
translation from problem to solution space to understand.

Ada's Float is defined as "at least 6 decimal digits of precision". This 
definition is imprecise, presumably to discourage its use in favor of 
user-defined types. If the precision of a floating-point type is important to 
the problem, you won't use a predefined type, but will declare your own with the 
desired precision.

Thinking in terms of declaring numeric types based on the needs of the problem, 
rather than choosing from language-supplied types, is part of the Ada Mind Set(tm).

-- 
Jeff Carter
"I'm particularly glad that these lovely children were
here today to hear that speech. Not only was it authentic
frontier gibberish, it expressed a courage little seen
in this day and age."
Blazing Saddles
88



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

* Re: simple question on long_float/short_float
  2010-09-30  8:31   ` Nasser M. Abbasi
                       ` (3 preceding siblings ...)
  2010-09-30 15:37     ` Jeffrey Carter
@ 2010-09-30 15:56     ` Adam Beneschan
  4 siblings, 0 replies; 22+ messages in thread
From: Adam Beneschan @ 2010-09-30 15:56 UTC (permalink / raw)


On Sep 30, 1:31 am, "Nasser M. Abbasi" <n...@12000.org> wrote:

> Is this for real? :)

No, it's for float.  Ada doesn't use the REAL keyword.

> So, what is Ada's Float? 32 bit or 64 bits?

The language does not define this.  The language is designed to be
implemented on many processors, some of which have floats that are
neither 32 nor 64 bits.  Others have mentioned 80-bit floats.  ADI
SHARC has 40-bit floats.  Some older systems (Honeywell 600 series
e.g.) were based on 36-bit words and their floats were 36 bits.  No
idea what Crays define, but it wouldn't surprise me if there are
processors designed for heavy scientific computation that would
require floats of even greater precision than these.

Anyway, it was deliberate that the language did not define exactly
what "Float", "Long_Float", and "Short_Float" are supposed to look
like, so you shouldn't use those types if you care what kind of float
you're getting.  If you need the type to match a type in some other
language implemented on that same processor, then use one of the types
in Interfaces.Fortran or Interfaces.C or whatever.  If you need your
floating-point type to be specifically 32 or 64 or 57 bits for some
other reason, then either define your own, or (if you don't care about
portability) check to see what your particular compiler's definitions
for that particular processor are.  (Even on the same processor, the
definition of "Float" may be different between different vendors'
compilers, because the language doesn't define this.)  You can use
Float or Long_Float if you just want any floating-point type that
meets or exceeds the minimum standard in 3.5.7(14-15) and don't really
care how it's implemented.  But only then.

                                    -- Adam





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

* Re: simple question on long_float/short_float
  2010-09-30 15:37     ` Jeffrey Carter
@ 2010-09-30 18:22       ` Nasser M. Abbasi
  2010-09-30 21:21       ` Peter C. Chapin
  1 sibling, 0 replies; 22+ messages in thread
From: Nasser M. Abbasi @ 2010-09-30 18:22 UTC (permalink / raw)


On 9/30/2010 8:37 AM, Jeffrey Carter wrote:

> In most languages (other than Ada), you have to use the numeric types provided
> by the language, and select the types that are the closest fit to the
> requirements of the problem. This is part of what is known as "translating the
> problem into the solution space", which makes the software harder to understand,
> since the reader has to understand both the problem and the translation chosen.
>
> In Ada, one can declare numeric types to match the requirements of the problem.
> This is part of what is known as "modeling the problem in the software", which
> is intended to make the software easier to understand, since there is no
> translation from problem to solution space to understand.
>

This really opened my eyes to a very good point that I did not think of. 
You are 100% right.

Very well said.

--Nasser



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

* Re: simple question on long_float/short_float
  2010-09-30 15:37     ` Jeffrey Carter
  2010-09-30 18:22       ` Nasser M. Abbasi
@ 2010-09-30 21:21       ` Peter C. Chapin
  2010-10-01  0:29         ` Nasser M. Abbasi
  2010-10-02 19:57         ` Simon Wright
  1 sibling, 2 replies; 22+ messages in thread
From: Peter C. Chapin @ 2010-09-30 21:21 UTC (permalink / raw)


On 2010-09-30 11:37, Jeffrey Carter wrote:

> Are you saying that in Fortran you can only make floating-point
> variables with one of two precisions?
> 
> Is this for real?

I don't know about old Fortran but in Fortran 90 and above you can
specify the characteristics of your floating point type and ask the
compiler to find an underlying type that works for you.

Each compiler provides various "kinds" of floating point types.
Depending on the underlying machine there might be several. These kinds
are identified by compiler specific integer constants (kind 1, kind 2,
etc). Of course referring to them that way isn't portable... my floating
point type that is labeled as "kind 1" might be nothing like the
floating point type you are labeling as "kind 1." To get around this you
can use the intrinsic function SELECTED_REAL_KIND. It looks like this:

INTEGER, PARAMETER :: my_floating_kind = SELECTED_REAL_KIND(6, 30)

This asks the compiler to find a floating point type with at least 6
digits of precision and a range that covers 10**30. The resulting kind
(some number like 1, 2, 3, etc) is then given the name "my_floating_kind."

I declare variables like this

REAL(KIND = my_floating_kind) :: x, y, z

The kind value is like a type parameter. Thus REAL(KIND = 1) means you
want the first kind of floating point type, REAL(KIND = 2) means you
want the second kind. In the declaration above "my_floating_kind" is
used meaning that you want whatever kind is necessary to get the desired
precision and range. Maybe that's kind 2 on my compiler and kind 17 on
yours.

Fortran compilers are required (I think) to provide a double precision
type but internally that type is just a particular kind (maybe kind 2?).
In modern Fortran you never have to say "double precision" the term
exists for compatibility and convenience.

We now return to our regularly scheduled discussion of Ada. :)

Peter



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

* Re: simple question on long_float/short_float
  2010-09-30 21:21       ` Peter C. Chapin
@ 2010-10-01  0:29         ` Nasser M. Abbasi
  2010-10-01 11:13           ` Peter C. Chapin
  2010-10-02 19:57         ` Simon Wright
  1 sibling, 1 reply; 22+ messages in thread
From: Nasser M. Abbasi @ 2010-10-01  0:29 UTC (permalink / raw)


On 9/30/2010 2:21 PM, Peter C. Chapin wrote:
> On 2010-09-30 11:37, Jeffrey Carter wrote:
>
>> Are you saying that in Fortran you can only make floating-point
>> variables with one of two precisions?
>>
>> Is this for real?
>
> I don't know about old Fortran but in Fortran 90 and above you can
> specify the characteristics of your floating point type and ask the
> compiler to find an underlying type that works for you.
>
> Each compiler provides various "kinds" of floating point types.
> Depending on the underlying machine there might be several. These kinds
> are identified by compiler specific integer constants (kind 1, kind 2,
> etc). Of course referring to them that way isn't portable... my floating
> point type that is labeled as "kind 1" might be nothing like the
> floating point type you are labeling as "kind 1." To get around this you
> can use the intrinsic function SELECTED_REAL_KIND. It looks like this:
>
> INTEGER, PARAMETER :: my_floating_kind = SELECTED_REAL_KIND(6, 30)
>
> This asks the compiler to find a floating point type with at least 6
> digits of precision and a range that covers 10**30. The resulting kind
> (some number like 1, 2, 3, etc) is then given the name "my_floating_kind."
>
> I declare variables like this
>
> REAL(KIND = my_floating_kind) :: x, y, z
>
> The kind value is like a type parameter. Thus REAL(KIND = 1) means you
> want the first kind of floating point type, REAL(KIND = 2) means you
> want the second kind. In the declaration above "my_floating_kind" is
> used meaning that you want whatever kind is necessary to get the desired
> precision and range. Maybe that's kind 2 on my compiler and kind 17 on
> yours.
>
> Fortran compilers are required (I think) to provide a double precision
> type but internally that type is just a particular kind (maybe kind 2?).
> In modern Fortran you never have to say "double precision" the term
> exists for compatibility and convenience.
>
> We now return to our regularly scheduled discussion of Ada. :)
>
> Peter

Thanks Peter for this post, I did not know this about new Fortran.

I am finding that modern Fortran has become too complex, the language 
has so many new features now, I find even Ada to be simple compared to it :)

This trend to update computer languages every few years seems to result 
in a language first starting as simple, with the basic features, then it 
gets more and more complicated and bloated as more features are stuffed 
in it to make it more cool and modern. With time computer languages 
become too complex to understand by mere mortals, unless one has a PhD 
in computer science.

--Nasser




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

* Re: simple question on long_float/short_float
  2010-10-01  0:29         ` Nasser M. Abbasi
@ 2010-10-01 11:13           ` Peter C. Chapin
  0 siblings, 0 replies; 22+ messages in thread
From: Peter C. Chapin @ 2010-10-01 11:13 UTC (permalink / raw)


On 2010-09-30 20:29, Nasser M. Abbasi wrote:

> This trend to update computer languages every few years seems to result
> in a language first starting as simple, with the basic features, then it
> gets more and more complicated and bloated as more features are stuffed
> in it to make it more cool and modern. With time computer languages
> become too complex to understand by mere mortals, unless one has a PhD
> in computer science.

I agree there definitely is that trend. Of course part of the problem is
that old features can only be removed with extreme difficulty since most
language communities want backward compatibility. Thus the "size" of a
language tends to be a monotonically increasing function of time.

In fact, this is one reason why I appreciate Ada. Although the language
is large and complicated, my perception is that new features are added
to it conservatively and only after extensive deliberation. I'm not
saying other communities don't deliberate extensively as well, but it
seems like some languages (C++?) grow more quickly than is good for them.

Also Ada has a mechanism (pragma Restrictions) where one can selective
remove features from the language depending on one's needs. I don't know
of any other language that offers a standard way to make it smaller.

Finally there is SPARK. The language itself is actually quite simple and
basic. Of course fully using the annotations and doing all the
associated proofs introduces its own complexity, but much of that could
be skipped if desired.

Peter



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

* Re: simple question on long_float/short_float
  2010-09-30  6:17 simple question on long_float/short_float Nasser M. Abbasi
  2010-09-30  6:58 ` J-P. Rosen
@ 2010-10-02  9:11 ` Nasser M. Abbasi
  2010-10-02  9:48   ` Dmitry A. Kazakov
                     ` (3 more replies)
  1 sibling, 4 replies; 22+ messages in thread
From: Nasser M. Abbasi @ 2010-10-02  9:11 UTC (permalink / raw)


Ada experts;

I have a follow up.

It says here
http://www.pegasoft.ca/resources/boblap/17.html

"Gnat provides interfacing packages for languages besides C. 
Interfaces.Fortran contains types and subprograms to link Fortran 
language programs to your Ada programs. The GCC Fortran 77 compiler is g77.

As with gcc, most of the Fortran data types correspond identically with 
an Ada type. A Fortran real variable, for example, is the same as an Ada 
float, and a double precision variable is an Ada long_float. Other Ada 
compilers may not do this: if portability is an issue, always use the 
types of Interfaces.Fortran."


I wrote a small Ada program to print a value of a floating number which 
has the type Double_Precision, and printed the value to the screen.

I did the same in Fortran, same number, and printed the value to screen.
In Fortran, the value printed is that of IEEE754, and in Ada it also 
printed the same as Fortran. So I am happy to see that. Here is the code 
and the output:

---- Ada  v1 ----
with ada.text_io;
with Interfaces.Fortran; use Interfaces.Fortran;

procedure test is
   package my_float_IO is new Ada.Text_IO.float_IO(Double_Precision);
    use  my_float_IO;
   a : Double_Precision  := 2.3;
begin
  Put( item=>a,Fore=>25,Aft=>16) ;
end test;

---- fortran ----
program main
     implicit none
     double precision :: c=2.3D0

     write (*,'(F25.16)') c
end program main

---- output of the above in same order ----
2.2999999999999998E+00  ---- Ada
2.2999999999999998      ---- Fortran

Then I changed the Ada code to the following:

---- Ada v2 -----
with ada.text_io;
procedure test is
   type my_float_type is digits 16;
   package my_float_IO is new Ada.Text_IO.float_IO(my_float_type);
    use  my_float_IO;
   a : my_float_type := 2.3;
begin
  Put( item=>a,Fore=>25,Aft=>16) ;
end test;

and now the output is

2.3000000000000000E+00

------------------------------

1) So, it seems to me that Ada did use Fortran double in v1. since 
output is different than in v2. Unless I made a mistake in v2. Hence, I 
do not understand why the webpage above said that Gnat would use the 
same types. Can I change v2 to make it output the same as Fortran? I 
assume not, since it is not double to start with.

2) The reason I wanted to use Fortran double type, so I can compare the 
output of my Ada program to that of Fortran and some output in the textbook.

3) I need to read more about Ada Float type. Standard is IEEE754, so if 
Ada float does not use this, would this not make the analysis of 
floating point computation in Ada harder? Most textbooks and numerical 
stuff, assume IEEE754 computation done on floating points.

I also need to study more about Ada floats and how they work. What I do 
not understand yet, does Ada does use IEEE754 for its floats? Assuming 
it does, then it must do something additional at run-time to obtain the 
result it needs when one uses DIGITS nnn in the type definition for Ada 
float.

--Nasser



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

* Re: simple question on long_float/short_float
  2010-10-02  9:11 ` Nasser M. Abbasi
@ 2010-10-02  9:48   ` Dmitry A. Kazakov
  2010-10-02 20:09     ` Simon Wright
  2010-10-02  9:56   ` Nasser M. Abbasi
                     ` (2 subsequent siblings)
  3 siblings, 1 reply; 22+ messages in thread
From: Dmitry A. Kazakov @ 2010-10-02  9:48 UTC (permalink / raw)


On Sat, 02 Oct 2010 02:11:10 -0700, Nasser M. Abbasi wrote:

> I wrote a small Ada program to print a value of a floating number which 
> has the type Double_Precision, and printed the value to the screen.
> 
> I did the same in Fortran, same number, and printed the value to screen.
> In Fortran, the value printed is that of IEEE754, and in Ada it also 
> printed the same as Fortran. So I am happy to see that. Here is the code 
> and the output:
> 
> ---- Ada  v1 ----
> with ada.text_io;
> with Interfaces.Fortran; use Interfaces.Fortran;
> 
> procedure test is
>    package my_float_IO is new Ada.Text_IO.float_IO(Double_Precision);
>     use  my_float_IO;
>    a : Double_Precision  := 2.3;
> begin
>   Put( item=>a,Fore=>25,Aft=>16) ;
> end test;
> 
> ---- fortran ----
> program main
>      implicit none
>      double precision :: c=2.3D0
> 
>      write (*,'(F25.16)') c
> end program main
> 
> ---- output of the above in same order ----
> 2.2999999999999998E+00  ---- Ada
> 2.2999999999999998      ---- Fortran
> 
> Then I changed the Ada code to the following:
> 
> ---- Ada v2 -----
> with ada.text_io;
> procedure test is
>    type my_float_type is digits 16;
>    package my_float_IO is new Ada.Text_IO.float_IO(my_float_type);
>     use  my_float_IO;
>    a : my_float_type := 2.3;
> begin
>   Put( item=>a,Fore=>25,Aft=>16) ;
> end test;
> 
> and now the output is
> 
> 2.3000000000000000E+00
> 
> ------------------------------
> 
> 1) So, it seems to me that Ada did use Fortran double in v1. since 
> output is different than in v2. Unless I made a mistake in v2. Hence, I 
> do not understand why the webpage above said that Gnat would use the 
> same types. Can I change v2 to make it output the same as Fortran? I 
> assume not, since it is not double to start with.

Hmm, try this:

with Ada.Text_IO;        use Ada.Text_IO;
with Interfaces.Fortran; use Interfaces.Fortran;
procedure Test is
   type My_Float_Type   is digits 16;
   type Long_Float_Type is digits 18;
   A : My_Float_Type    := 2.3;
   B : Double_Precision := 2.3;
   C : Long_Float_Type  := 2.3;
begin
   Put_Line ("A'Size=" & Integer'Image (A'Size) & " A=" &
My_Float_Type'Image (A));
   Put_Line ("B'Size=" & Integer'Image (B'Size) & " B=" &
Double_Precision'Image (B));
   Put_Line ("C'Size=" & Integer'Image (C'Size) & " C=" &
Long_Float_Type'Image (C));
end Test;

On my Intel machine Double_Precision is 96 bits.

Then note that the output may differ not because the Ada type uses another
machine type. Even if they are same, since you have specified 16 *decimal*
digits, the output rounds the underlying binary representation to 16
decimal digits.

> 3) I need to read more about Ada Float type. Standard is IEEE754, so if 
> Ada float does not use this,

That depends on the machine. Float and Long_Float standard types are most
likely IEEE 754.

> would this not make the analysis of 
> floating point computation in Ada harder?

No, it makes it easier. It is advisable not to use IEEE 754 semantics, even
if the machine is IEEE 754. E.g. if you declare your type as

   type My_Float is new Float; -- Chances are high to get IEEE 754 here

To kill IEEE 754 semantics do it this way:

   type My_Float is new Float range Float'Range; -- Only numbers!

> Most textbooks and numerical 
> stuff, assume IEEE754 computation done on floating points.

I doubt it much. IEEE 754 is a normal floating-point + some non-numbers
like NaN. As the name suggests, numeric computations are performed on
*numbers*. IEEE 754 non-numbers might be useful for the languages with no
means to deal with numeric errors. But in Ada you don't need that because
Ada has numeric exceptions.

From the software design point of view, it is a very bad idea of IEEE 754
to postpone error handling till the end of computations or even forever.
You might compute something awfully long and complex just to get NaN in the
end, not knowing what (and where) was wrong. In a closed loop system you
might deliver NaN on actuators, what would they do?

Ada's numeric model follows the fundamental software design principle: you
shall detect errors *early*.

> I also need to study more about Ada floats and how they work. What I do 
> not understand yet, does Ada does use IEEE754 for its floats?

It likely uses IEEE 754 if the machine is IEEE 754, which is almost always.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: simple question on long_float/short_float
  2010-10-02  9:11 ` Nasser M. Abbasi
  2010-10-02  9:48   ` Dmitry A. Kazakov
@ 2010-10-02  9:56   ` Nasser M. Abbasi
  2010-10-02 10:45   ` cjpsimon
  2010-10-02 16:52   ` Jeffrey Carter
  3 siblings, 0 replies; 22+ messages in thread
From: Nasser M. Abbasi @ 2010-10-02  9:56 UTC (permalink / raw)


On 10/2/2010 2:11 AM, Nasser M. Abbasi wrote:

>
> ---- Ada v2 -----
> with ada.text_io;
> procedure test is
>     type my_float_type is digits 16;
>     package my_float_IO is new Ada.Text_IO.float_IO(my_float_type);
>      use  my_float_IO;
>     a : my_float_type := 2.3;
> begin
>    Put( item=>a,Fore=>25,Aft=>16) ;
> end test;
>
> and now the output is
>
> 2.3000000000000000E+00
>
> ------------------------------
>
> 1) So, it seems to me that Ada did use Fortran double in v1. since
> output is different than in v2. Unless I made a mistake in v2.

Yes, I made a mistake.  I did not use Ada float type. I thought doing 
digits 16 is that. that is wrong.  When I changed it to the following

------------------
with ada.text_io;

procedure test is
    package my_float_IO2 is new Ada.Text_IO.float_IO(long_float);
      use  my_float_IO2;
begin
   Put( item=>2.3,Fore=>25,Aft=>16) ;
end test;
----------------------

Now it printed

             2.2999999999999998E+00

So, the web page is correct !  gnat long_float is the same as Fortran 
double.

--Nasser





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

* Re: simple question on long_float/short_float
  2010-10-02  9:11 ` Nasser M. Abbasi
  2010-10-02  9:48   ` Dmitry A. Kazakov
  2010-10-02  9:56   ` Nasser M. Abbasi
@ 2010-10-02 10:45   ` cjpsimon
  2010-10-02 16:52   ` Jeffrey Carter
  3 siblings, 0 replies; 22+ messages in thread
From: cjpsimon @ 2010-10-02 10:45 UTC (permalink / raw)


On 2 oct, 11:11, "Nasser M. Abbasi" <n...@12000.org> wrote:
> Ada experts;
>
> I have a follow up.
>
> It says herehttp://www.pegasoft.ca/resources/boblap/17.html
>
> "Gnat provides interfacing packages for languages besides C.
> Interfaces.Fortran contains types and subprograms to link Fortran
> language programs to your Ada programs. The GCC Fortran 77 compiler is g77.
>
> As with gcc, most of the Fortran data types correspond identically with
> an Ada type. A Fortran real variable, for example, is the same as an Ada
> float, and a double precision variable is an Ada long_float. Other Ada
> compilers may not do this: if portability is an issue, always use the
> types of Interfaces.Fortran."
>
> I wrote a small Ada program to print a value of a floating number which
> has the type Double_Precision, and printed the value to the screen.
>
> I did the same in Fortran, same number, and printed the value to screen.
> In Fortran, the value printed is that of IEEE754, and in Ada it also
> printed the same as Fortran. So I am happy to see that. Here is the code
> and the output:
>
> ---- Ada  v1 ----
> with ada.text_io;
> with Interfaces.Fortran; use Interfaces.Fortran;
>
> procedure test is
>    package my_float_IO is new Ada.Text_IO.float_IO(Double_Precision);
>     use  my_float_IO;
>    a : Double_Precision  := 2.3;
> begin
>   Put( item=>a,Fore=>25,Aft=>16) ;
> end test;
>
> ---- fortran ----
> program main
>      implicit none
>      double precision :: c=2.3D0
>
>      write (*,'(F25.16)') c
> end program main
>
> ---- output of the above in same order ----
> 2.2999999999999998E+00  ---- Ada
> 2.2999999999999998      ---- Fortran
>
> Then I changed the Ada code to the following:
>
> ---- Ada v2 -----
> with ada.text_io;
> procedure test is
>    type my_float_type is digits 16;
>    package my_float_IO is new Ada.Text_IO.float_IO(my_float_type);
>     use  my_float_IO;
>    a : my_float_type := 2.3;
> begin
>   Put( item=>a,Fore=>25,Aft=>16) ;
> end test;
>
> and now the output is
>
> 2.3000000000000000E+00
>
> ------------------------------
>
> 1) So, it seems to me that Ada did use Fortran double in v1. since
> output is different than in v2. Unless I made a mistake in v2. Hence, I
> do not understand why the webpage above said that Gnat would use the
> same types. Can I change v2 to make it output the same as Fortran? I
> assume not, since it is not double to start with.
>
> 2) The reason I wanted to use Fortran double type, so I can compare the
> output of my Ada program to that of Fortran and some output in the textbook.
>
> 3) I need to read more about Ada Float type. Standard is IEEE754, so if
> Ada float does not use this, would this not make the analysis of
> floating point computation in Ada harder? Most textbooks and numerical
> stuff, assume IEEE754 computation done on floating points.
>
> I also need to study more about Ada floats and how they work. What I do
> not understand yet, does Ada does use IEEE754 for its floats? Assuming
> it does, then it must do something additional at run-time to obtain the
> result it needs when one uses DIGITS nnn in the type definition for Ada
> float.
>
> --Nasser

I think Long_Float representation is limited to digits 15. As you want
to
have digits 16 the compiler have to choose Long_Long_Float
representation
or to reject your code depending of the hardware.

Claude



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

* Re: simple question on long_float/short_float
  2010-10-02  9:11 ` Nasser M. Abbasi
                     ` (2 preceding siblings ...)
  2010-10-02 10:45   ` cjpsimon
@ 2010-10-02 16:52   ` Jeffrey Carter
  2010-10-02 20:01     ` Georg Bauhaus
  3 siblings, 1 reply; 22+ messages in thread
From: Jeffrey Carter @ 2010-10-02 16:52 UTC (permalink / raw)


On 10/02/2010 02:11 AM, Nasser M. Abbasi wrote:
>
> 1) So, it seems to me that Ada did use Fortran double in v1. since
> output is different than in v2. Unless I made a mistake in v2. Hence, I
> do not understand why the webpage above said that Gnat would use the
> same types. Can I change v2 to make it output the same as Fortran? I
> assume not, since it is not double to start with.

Sure. Try

type My_Float is digits Interfaces.Fortran.Double_Precision'digits;

or

type My_Float is digits Long_Float'digits;

or even

type My_Float is digits 15;

all of which give My_Float the same "digits 15" declaration as Double_Precision 
and Long_Float.

> I also need to study more about Ada floats and how they work. What I do
> not understand yet, does Ada does use IEEE754 for its floats? Assuming
> it does, then it must do something additional at run-time to obtain the
> result it needs when one uses DIGITS nnn in the type definition for Ada
> float.

Ada compilers typically use an underlying hardware floating-point type to 
implement floating-point type declarations. That is IEEE-754 on many platforms, 
but not all.

-- 
Jeff Carter
"So if I understand 'The Matrix Reloaded' correctly, the Matrix is
basically a Microsoft operating system--it runs for a while and
then crashes and reboots. By design, no less. Neo is just a
memory leak that's too hard to fix, so they left him in ... The
users don't complain because they're packed in slush and kept
sedated."
Marin D. Condic
65



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

* Re: simple question on long_float/short_float
  2010-09-30 21:21       ` Peter C. Chapin
  2010-10-01  0:29         ` Nasser M. Abbasi
@ 2010-10-02 19:57         ` Simon Wright
  1 sibling, 0 replies; 22+ messages in thread
From: Simon Wright @ 2010-10-02 19:57 UTC (permalink / raw)


"Peter C. Chapin" <chapinp@acm.org> writes:

> In modern Fortran you never have to say "double precision"

But preexisting libraries (eg BLAS, LAPACK) may do so.



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

* Re: simple question on long_float/short_float
  2010-10-02 16:52   ` Jeffrey Carter
@ 2010-10-02 20:01     ` Georg Bauhaus
  0 siblings, 0 replies; 22+ messages in thread
From: Georg Bauhaus @ 2010-10-02 20:01 UTC (permalink / raw)


On 10/2/10 6:52 PM, Jeffrey Carter wrote:

> Ada compilers typically use an underlying hardware floating-point type to implement floating-point type declarations. That is IEEE-754 on many platforms, but not all.

In addition to that, compilers may generate different code for what
only appears to be the "same platform": Using GNAT, if you add options
-mfpmath=sse or -mfpmath=387, the compiler will issue
instructions for a corresponding set of registers, respectively.
(What exactly this means WRT IEEE754 I don't know).


Georg



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

* Re: simple question on long_float/short_float
  2010-10-02  9:48   ` Dmitry A. Kazakov
@ 2010-10-02 20:09     ` Simon Wright
  0 siblings, 0 replies; 22+ messages in thread
From: Simon Wright @ 2010-10-02 20:09 UTC (permalink / raw)


"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> writes:

> I doubt it much. IEEE 754 is a normal floating-point + some
> non-numbers like NaN. As the name suggests, numeric computations are
> performed on *numbers*. IEEE 754 non-numbers might be useful for the
> languages with no means to deal with numeric errors. But in Ada you
> don't need that because Ada has numeric exceptions.

But GNAT doesn't raise numeric exceptions for floating-point
computations resuting in Inf or NaN ('Machine_Overflows is False).

You can use 'Valid, or you can define the range as Dmitry noted above
(range Float'Range, I think) in which case you get Constraint_Error on
overflow.



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

end of thread, other threads:[~2010-10-02 20:09 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-09-30  6:17 simple question on long_float/short_float Nasser M. Abbasi
2010-09-30  6:58 ` J-P. Rosen
2010-09-30  8:31   ` Nasser M. Abbasi
2010-09-30  8:45     ` Nasser M. Abbasi
2010-09-30  9:59       ` Mark Lorenzen
2010-09-30 13:30       ` Peter C. Chapin
2010-09-30  8:46     ` AdaMagica
2010-09-30 10:02     ` Georg Bauhaus
2010-09-30 15:37     ` Jeffrey Carter
2010-09-30 18:22       ` Nasser M. Abbasi
2010-09-30 21:21       ` Peter C. Chapin
2010-10-01  0:29         ` Nasser M. Abbasi
2010-10-01 11:13           ` Peter C. Chapin
2010-10-02 19:57         ` Simon Wright
2010-09-30 15:56     ` Adam Beneschan
2010-10-02  9:11 ` Nasser M. Abbasi
2010-10-02  9:48   ` Dmitry A. Kazakov
2010-10-02 20:09     ` Simon Wright
2010-10-02  9:56   ` Nasser M. Abbasi
2010-10-02 10:45   ` cjpsimon
2010-10-02 16:52   ` Jeffrey Carter
2010-10-02 20:01     ` Georg Bauhaus

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