From: Stephen Leake on
"Nasser M. Abbasi" <nma(a)12000.org> writes:

> I also wrote a FORTRAN equivalent of the small Ada function. Here is
> below the Ada code, and the FORTRAN code. Again, do not scream too
> much if it is not good code, I just learned this now, I am sure this
> can be improved a lot.

This is an interesting comparison. Perhaps, after people finish
polishing your code, you could publish it on
http://en.wikibooks.org/wiki/Ada_Programming

> -- gnatmake dft.adb
> --
> -- ./dft.exe
> -- ( 6.00000E+00, 0.00000E+00)
> -- (-1.50000E+00, 8.66026E-01)
> -- (-1.50000E+00,-8.66025E-01)
> -- $
>
> ======= FORTRAN code ===========
> ! dtf.f90, compiled with GCC 4.3.4
> ! under CYGWIN 1.7.5
> ! gfortran -Wall dft.f90
> ! ./a.exe
> ! ( 6.0000000 , 0.0000000 )
> ! ( -1.4999999 , 0.86602557 )
> ! ( -1.5000005 ,-0.86602497 )
> !

It would be good to explain the small differences here; something about
how the floating point options are set, I suspect.

It would be good to state the theoretically correct answer; I hope it's
1.5, not 1.499... :).

> Conclusion:
> I actually liked the Ada implementation more than FORTRAN because:
>
> 1. In Ada, I did not have to change the index of m and k in the
> summation to reflect the 1-off per the definition of DFT.
> DFT starts from 0 to N-1. In Ada, using 'Range and defining the arrays
> to go from 0 .. N-1 solved the problem.
>
> 2. In Ada, the compiler complained more times more about types being
> mixed up. I placed float() around the places it complained about.

It's interesting that you list this as a bonus; some people would list
it is a negative feature ("_obviously_ the compiler should do that
conversion for you!").

> 3. It actually took me less time to do the Ada function than the
> FORTRAN one, even though I am equally not familiar with both at this
> time :)

That is my general experience with Ada; it takes less time to get the
result I want.

--
-- Stephe
From: J-P. Rosen on
Stephen Leake a �crit :
>> -- gnatmake dft.adb
>> --
>> -- ./dft.exe
>> -- ( 6.00000E+00, 0.00000E+00)
>> -- (-1.50000E+00, 8.66026E-01)
>> -- (-1.50000E+00,-8.66025E-01)
>> -- $
>>
>> ======= FORTRAN code ===========
>> ! dtf.f90, compiled with GCC 4.3.4
>> ! under CYGWIN 1.7.5
>> ! gfortran -Wall dft.f90
>> ! ./a.exe
>> ! ( 6.0000000 , 0.0000000 )
>> ! ( -1.4999999 , 0.86602557 )
>> ! ( -1.5000005 ,-0.86602497 )
>> !
>
> It would be good to explain the small differences here; something about
> how the floating point options are set, I suspect.
In Ada, the value is rounded to the requested accuracy (A.10.9(25)). In
Fortran, the machine value is printed.

> It would be good to state the theoretically correct answer; I hope it's
> 1.5, not 1.499... :).
Note that they are mathematically equal (with an inifinite number of 9s)
--
---------------------------------------------------------
J-P. Rosen (rosen(a)adalog.fr)
Visit Adalog's web site at http://www.adalog.fr
From: Nasser M. Abbasi on
On 6/10/2010 12:23 AM, Stephen Leake wrote:

>
>> -- gnatmake dft.adb
>> --
>> -- ./dft.exe
>> -- ( 6.00000E+00, 0.00000E+00)
>> -- (-1.50000E+00, 8.66026E-01)
>> -- (-1.50000E+00,-8.66025E-01)
>> -- $
>>
>> ======= FORTRAN code ===========
>> ! dtf.f90, compiled with GCC 4.3.4
>> ! under CYGWIN 1.7.5
>> ! gfortran -Wall dft.f90
>> ! ./a.exe
>> ! ( 6.0000000 , 0.0000000 )
>> ! ( -1.4999999 , 0.86602557 )
>> ! ( -1.5000005 ,-0.86602497 )
>> !
>

> It would be good to explain the small differences here; something about
> how the floating point options are set, I suspect.
>

Yes, I noticed that too. Need to look more into. It could be just a
formating thing. I just used Print*, in Fortran becuase at the time was
too lazy to lookin up the other formating functions in Fortran. In Ada,
I used the predefined Put on Complex of float types.

> It would be good to state the theoretically correct answer; I hope it's
> 1.5, not 1.499... :).
>

Well, the extact answer is -1.5 for the real part of those terms.

Can be seen also by using a CAS such as Mathematica which allows one to
set an arbitrary precision. Here is one with 50 precision:

SetPrecision[Fourier[{1., 2., 3.}, FourierParameters ->{1, -1}],50]


{6.0000000000000000000000000000000000000000000000000+0.*10^-50 I,

-1.5000000000000000000000000000000000000000000000000 +
0.8660254037844385965340919530941476978114224039018 I,

-1.5000000000000000000000000000000000000000000000000
-0.8660254037844385965340919530941476978114224039018 I}

So, it is 1.5 not 1.499999... (can also be seen by expanding the sum
itself. and using Euler relation to convert exp() to trig functions and
looking at the resulting cosine and sin terms, one can see this term
comes out to be an exact -3/2).

well look into why Fortan prints -1.499999 and correct my code if it is
because I am not doing something right.


--Nasser
From: Yannick Duchêne (Hibou57) on
>> It would be good to state the theoretically correct answer; I hope it's
>> 1.5, not 1.499... :).
> Note that they are mathematically equal (with an inifinite number of 9s)
I'm not using so called Reals, so I am not at ease with details in this
area: is it specified by the Ada standard or by the IEEE standard ? (I
know Ada follows IEEE standard, while may be Ada adds some extra stuff
there)

Humor: where did you ever see an infinite sequence in a computer ? (except
in a never-ending loop)

--
There is even better than a pragma Assert: a SPARK --# check.
--# check C and WhoKnowWhat and YouKnowWho;
--# assert Ada;
-- i.e. forget about previous premises which leads to conclusion
-- and start with new conclusion as premise.
From: J-P. Rosen on
Yannick Duch�ne (Hibou57) a �crit :
>>> It would be good to state the theoretically correct answer; I hope it's
>>> 1.5, not 1.499... :).
>> Note that they are mathematically equal (with an inifinite number of 9s)
> I'm not using so called Reals, so I am not at ease with details in this
> area: is it specified by the Ada standard or by the IEEE standard ? (I
> know Ada follows IEEE standard, while may be Ada adds some extra stuff
> there)
For output, rounding is specified in the standard. And BTW, the standard
does not specify IEEE (fortunately, it did not make the same mistake as
Java!). It just makes sure that it is not incompatible with IEEE
arithmetic, and does provide some facilities for handling signed zeroes,
which are present in IEEE, but maybe also provided by other models.

> Humor: where did you ever see an infinite sequence in a computer ?
> (except in a never-ending loop)
>
I said "mathematically", because of the "..." that followed the last
'9'. But for computers, anybody doing some number crunching should know
that computations are not exact, and that 1.49* should be considered the
same as 1.5

--
---------------------------------------------------------
J-P. Rosen (rosen(a)adalog.fr)
Visit Adalog's web site at http://www.adalog.fr