Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

? How to have a FORTRAN function emulate flops for desired digits

3 views
Skip to first unread message

Cheng Cosine

unread,
Sep 5, 2000, 10:21:22 AM9/5/00
to

Hi ALL:

In FORTRAN, there are two types of real floating number, single

and double precision. Single precision has 6 significant digits

while double precision has 16. ( when represented in 10 based )

From what I learned from some numerical analysis books, the

floating point system is machine dependent. Say there are other

system used in Cray and IBM mainframe that are other than the

IEEE SP/DP system.

Now how can I write a FORTRAN90 code that works in lower significant

number only. Say only works for 3, 4, or 10 digits?

Thanks,

by Cheng Cosine
Sep/05/2k UT


Richard Maine

unread,
Sep 6, 2000, 11:50:47 AM9/6/00
to
Cheng Cosine <aco...@yahoo.com> writes:

> In FORTRAN, there are two types of real floating number, single
> and double precision.

That's true in f77. F90/f95 has at least 2, but may have more, depending
on the compiler.

> Single precision has 6 significant digits
> while double precision has 16.

That depends on the compiler. These values are not correct for all compilers.

> From what I learned from some numerical analysis books, the
> floating point system is machine dependent. Say there are other
> system used in Cray and IBM mainframe that are other than the
> IEEE SP/DP system.

Correct.

> Now how can I write a FORTRAN90 code that works in lower significant
> number only. Say only works for 3, 4, or 10 digits?

In f90, you can use the selected_real_kind intrinsic to ask for a particular
precision as minimum. For example, to declare something with at least 10
decimal digits of precision, you could do

integer, parameter :: my_kind = selected_real_kind(10)
...
real(my_kind) :: something

(You'd probably have the parameter statement in a module in practice).
This will typically get you double precision on a 32-bit computer and
single precision on a 64-bit one.

Note, however, that this intrinsic only selects from among the kinds
that the compiler supports, which in turn are typically going to be
only those precisions that the hardware natively supports. The
precision you specify is the minimum acceptable - not the exact one.
Thus, specifying that you need at least 10 digits will probably get
you about 15-16. Specifying 3 digits will probably get you the same
was whatever single precision on the system is - typically 6-7 decimal
digits on a 32-bit system, and 15-16 decimal digits on a 64-bit one.

If you actually require a specific precision that isn't supported by
the compiler, then you'd have to write your own code to do the
arithmetic in that precision. The compiler won't do it for you. (F90
at least provides some hooks to connect the code to, but you still
have to write the code). Its a fair amount of bother, and will
certainly be *A LOT* slower. Count on a factor of about 100 decrease
in speed compared to the hardware-supported precisions. Presumably
one wouldn't do this unless there was a substantial need.

--
Richard Maine
ma...@altair.dfrc.nasa.gov

Cheng Cosine

unread,
Sep 5, 2000, 12:13:58 PM9/5/00
to

Richard Maine wrote:

> Cheng Cosine <aco...@yahoo.com> writes:
>
> > In FORTRAN, there are two types of real floating number, single
> > and double precision.
>
> That's true in f77. F90/f95 has at least 2, but may have more, depending
> on the compiler.
>

Would you give some examples about other precisions that some compilers

can offer other than the standard IEEE SP/DP?

>
> > Single precision has 6 significant digits
> > while double precision has 16.
>
> That depends on the compiler. These values are not correct for all compilers.
>

For Lahey FORTRAN, that seems to be. Can you give the values for other compilers?

> > Now how can I write a FORTRAN90 code that works in lower significant
> > number only. Say only works for 3, 4, or 10 digits?
>
> In f90, you can use the selected_real_kind intrinsic to ask for a particular
> precision as minimum. For example, to declare something with at least 10
> decimal digits of precision, you could do
>
> integer, parameter :: my_kind = selected_real_kind(10)
> ...
> real(my_kind) :: something
>
> (You'd probably have the parameter statement in a module in practice).
> This will typically get you double precision on a 32-bit computer and
> single precision on a 64-bit one.
>

I know that declaration, but that is not what I want. I'd like to show my student

about how limited significant digits can effect the flops. So I need exectly 3, 4,
or

10, or whatever digits that the hardware does not support though.

Can you give me a FORTRAN90 code as an example?

Thanks,

by Cheng Cosine
Sep/06/2k UT

Richard Maine

unread,
Sep 6, 2000, 5:07:30 PM9/6/00
to
Cheng Cosine <aco...@yahoo.com> writes:

> Would you give some examples about other precisions that some compilers
> can offer other than the standard IEEE SP/DP?

Whatever the machine offers. Some support the IEEE 80-bit and 128-bit
formats. Then there are the VAX formats. And the IBM mainframe ones.
And the Cray ones. That's all among machines currently in fairly
common use. No need to even bring up the old CDC 60-bit formats,
and the 36- and 48-bit formats used on some other old machines - you
aren't likely to find f90 compilers on those machines (you'd have a
hard enough time finding the machines at all).

The IEEE formats are certainly the most common on current machines,
but they aren't universal. I'm a little puzzled by your even asking.
Seems to me that you (correctly) gave two examples yourself in...

> From what I learned from some numerical analysis books, the
> floating point system is machine dependent. Say there are other
> system used in Cray and IBM mainframe that are other than the
> IEEE SP/DP system.

Yes, those are two of the examples.

> > > Single precision has 6 significant digits
> > > while double precision has 16.
> > That depends on the compiler. These values are not correct for all compilers.
> For Lahey FORTRAN, that seems to be. Can you give the values for other compilers?

Frankly not worth my trouble. See first para. The 64-bit systems like the
Crays are likely to have numbers more like the 16 for single precision and
something bigger (I don't know what) for double.

> I know that declaration [selected_real_kind], but that is not what I


> want. I'd like to show my student about how limited significant
> digits can effect the flops. So I need exectly 3, 4, or 10, or
> whatever digits that the hardware does not support though.

I'm not sure how significant digits effect flops (perhaps I need to
take your course - I thought a flop was a floating point operation in whatever
precision it was in), but in any case..

If the hardware doesn't support it, then the compiler isn't likely to
either. Its certainly possible in principle, but it's really quite a
big job. A compiler vendor is unlikely to take that on without
substantial incentive (i.e. lots of sales dependent on it).

> Can you give me a FORTRAN90 code as an example?

No way. Its also a big job to do as a user. Perhaps someone might have
already done such a thing that they are willing to share. That's possible.
But there's no way I'm likely to try to do it as an answer to a usenet
posting. You'd have to go through and define each operation (including
extensions of any intrinsics that you wanted to use). As long as the
precision you wanted was less than the max hardware-supported one,
it would probably be easiest to use the hardware-supported operations
and then truncate the results.

You could look at Walt's big_integer (or however its spelled) module for
an example of how to define operations for a custom numeric type. But
I'm afraid a floating point one would be harder. If you are just looking
for something to use for illustration in a course, then I'd guess the
investment required would be far too high unless someone already has
something you could use. (If someone does, perhaps they'll reply in this
thread - I don't).

--
Richard Maine
ma...@altair.dfrc.nasa.gov

Matthew C Roberts

unread,
Sep 6, 2000, 5:27:36 PM9/6/00
to
The only example I know of implementing different precision levels in
software is Alan Miller, who has a quad precision package from
http://www.ozemail.com.au/~milleraj/ but I have no idea how this relates to
flop counts, or anything else.

Matthew C Roberts
Graduate Student
Department of Economics
NC State University

iday_...@DIESPAMDIEhotmail.com
% TO REPLY TO THIS MESSAGE
% REMOVE ALL CAPITAL LETTERS
% FROM THE EMAIL ADDRESS

"Richard Maine" <ma...@altair.dfrc.nasa.gov> wrote in message
news:ue8zt52...@altair.dfrc.nasa.gov...

Cheng Cosine

unread,
Sep 5, 2000, 2:32:42 PM9/5/00
to

Richard Maine wrote:

> Cheng Cosine <aco...@yahoo.com> writes:
>
> > I know that declaration [selected_real_kind], but that is not what I
> > want. I'd like to show my student about how limited significant
> > digits can effect the flops. So I need exectly 3, 4, or 10, or
> > whatever digits that the hardware does not support though.
>
> I'm not sure how significant digits effect flops (perhaps I need to
> take your course - I thought a flop was a floating point operation in whatever
> precision it was in), but in any case..

For example:

PROGRAM ttt
REAL :: a, b

a = 100000.0
b = 0.0001

WRITE(*,*) a+b
WRITE(*,'(1x,f29.20)') a+b


STOP
END PROGRAM ttt

This will output :

100000.
10000.00000000000000000000

Because the SP floting number have only 6 significant digits,

100000.+0.100000E-03 = 100000.

the SP system can not express 100000.0001

Now what I'd like to have is similar to the above but I need to be able to have

the significant digits defined by me, say 3, 4, 10, or whatever.

Richard Maine

unread,
Sep 6, 2000, 9:07:49 PM9/6/00
to
Cheng Cosine <aco...@yahoo.com> writes:
> Richard Maine wrote:
> > Cheng Cosine <aco...@yahoo.com> writes:
> > > I'd like to show my student about how limited significant
> > > digits can effect the flops....
> > I'm not sure how significant digits effect flops...
> For example: [elided]

Oh. I see. Sorry that I misunderstood your usage. I've never seen
the term flops used except in the context of counting the number of
floating point operations (and most commonly as an acronym for
Floating-point Operations Per Second). I couldn't see how cutting the
precision was going to show anything about the number of floating
point operations it took to do some computation or the number that
could be done in a second. Yes, its clear to me that precision has an
(important) effect on floating point operations.

Alas, understanding what you meant by that doesn't help me come up
with a simple solution to your request. My comment about it being
a lot of work still applies.

--
Richard Maine
ma...@qnet.com

Catherine Rees Lay

unread,
Sep 7, 2000, 8:35:11 AM9/7/00
to
In article <m23djdm...@vega.qnet.com>, Richard Maine
<ma...@qnet.com> writes
If you only need it for an example, and can choose your example
appropriately, it's relatively easy to cut off at a given number of
decimal places. For n places, multiply by 10**n, convert to integer,
divide by 10**n. If you can live with an example whose largest values
are around 1, this might help.

Catherine.
--
Catherine Rees Lay

Herman D. Knoble

unread,
Sep 7, 2000, 2:30:13 PM9/7/00
to
One technique to illustrate to students how significant digits can affect
computations is to use examples which break down very quickly
and drastically. (Worse yet are ones which break down more subtly!)

Back in '93 John R. Ehrman (then at IBM Santa Teresa Labs) and
I did a back to back presentation at Share 75 (See Proceedings of
Share 75) on this topic. John presented an excellent "Floating-point
Numbers and Arithmetic Tutorial showing the various models and
implementations and how arithmetic really breaks down. My presentation,
there was "Reality of REAL Arithmetic's Really Wrong Results". The
following example is from that presentation and has been for
us, a fairly good teaching example that you don't need to
limit how many digits participate in the model to make a point.

!
!=== Example 1: Numeric breakdown for IEEE Arithmetic
! (or worse yet for IBM 360 Floating-point arithmetic).
! For example try: A=1000000., X=0.0000001
! and see P computed as: 4534650. in Single Precision
! or: 4768372. in Double Precision.
! Note that P is algebraically equal to 1.0 for all A and X<>0.
!
! Penn State University Center for Academic Computing
! H. D. Knoble, August 1979.
REAL (KIND=1) P,A,X
DO I=1,99999
WRITE(6,*) 'Please enter A and X (or press Enter to Quit):'
WRITE(6,*) '(Try values 10000., 0.0001, then 0.0001, 10000.)'
READ(5,*,END=99) A,X
IF (A.EQ.0 .OR. X.EQ.0) GOTO 99
P=((A+X)**2 - A**2 - 2.*A*X)/X**2
WRITE(6,100) P,A,X
100 FORMAT(' P=',G14.7,' A=',G14.7,' X=',G14.7)
ENDDO
99 WRITE(6,*) '** Example 1 Ended **'
PAUSE
STOP
END

Interesting enough, the bigger the "spread" of A and X in general the bigger
the "wrong" answer. However essentially a decent result is computed even
with a very large spread between A and X if the rolls of A and X are
reversed (X large, A, small). The problem here of course is subtracting nearly
equal quantities which are formed by converting decimal numbers that have
infinite binary expansions; when subtracting nearly equal quantities, the
most significant digits cancel during the subtraction, thus shifting the "noise"
toward the most significant digits. Then dividing by X**2 is like multiplying
the "noise" by a very large number (scaling it up). "Really wrong results"
here illustrate with very little arithmetic so-called "significance loss".

A second part to a student exercise studying such an example (like the one
above or one using the so-called "calculator formula" for Standard Deviation
which subtracts two sums that will be nearly equal for a small Standard Deviation)
is to make a set of "recommendations" to help avoid numerical computational
difficulties. My undergrad students, working as a team, back in 1993 came up with:

1) Use Proven (refereed or commercial) library programs that are as robust
(give the right result or an error message if not) as possible. For example,
IMSL, NAG mathematical statistical libraries. Avoid blindly taking numerical
code from textbooks and magazines unless they are refereed or presented
by a proven expert in numerical computations.

2) Study (know) the possible domain of values that each program input
quantity will take on and test each algorithm thoroughly, especially on
the limits of that domain. (This of course is an art in itself).

3) Use at least Double Precision Arithmetic. (In more critical cases use
multiple or quadruple precision arithmetic; see http://www.nersc.gov/~dhbailey/
and http://www.ozemail.com.au/~milleraj/ respectively).

4) For a given mantissa precision (computer) do not input or output numbers
in ASCII (or EBCDIC) format with more decimal digits than can be handled by the
conversion mapping algorithms for that precision (computer) unless you
document that that's what you are doing.

5) Use relative comparisons in place of absolute comparisons of real
quantities. (Also note that you can also use "fuzzy" comparisons. See
http://ftp.cac.psu.edu/pub/ger/fortran/hdk/eps.for and extfloor.for )

6) Avoid computations which are algebraically undefined (or singular) or which
develop resul;ts whose magnitudes approach or exceed machine limits.E.g.,
Blindly setting results of underflows to zero can yield "Really Wrong Results".

7) Avoid mixing Fortran INTEGER and REAL quantities without knowing how they
will be compiled. E.g., X=X+I**(-J) does not increment REAL typed X except when
INTEGER typed I=1 or INTEGER typed J=0 and I<0.

8) Do not program a numerical model beyond your competence to either avoid
significance loss or monitor it. Be especially careful when less than a pre-specified
number of mantissa digits participate in a sum or when all but a few mantissa
digits cancel during a subtraction. You are responsible for disasters caused
by other people using your code!

I'm sure your students can come up with more and better here, especially
along the ethics implied by the last sentence in (8) above.

Good luck with it.
Skip Knoble, Penn State


On Tue, 05 Sep 2000 22:21:22 +0800, Cheng Cosine <aco...@yahoo.com> wrote:

-|
-|Hi ALL:
-|
-| In FORTRAN, there are two types of real floating number, single
-|
-|and double precision. Single precision has 6 significant digits
-|
-|while double precision has 16. ( when represented in 10 based )
-|
-| From what I learned from some numerical analysis books, the
-|
-|floating point system is machine dependent. Say there are other
-|
-|system used in Cray and IBM mainframe that are other than the
-|
-|IEEE SP/DP system.
-|
-| Now how can I write a FORTRAN90 code that works in lower significant
-|
-|number only. Say only works for 3, 4, or 10 digits?
-|
-| Thanks,
-|
-| by Cheng Cosine
-| Sep/05/2k UT
-|


Herman D. (Skip) Knoble, Research Associate
Mailto:h...@psu.edu
Web: http://www.personal.psu.edu/hdk
Center for Academic Computing
Penn State University
214C Computer Building
University Park, PA 16802-2101
Phone:+1 814 865-0818 Fax:+1 814 863-7049

R. Vowels

unread,
Sep 7, 2000, 7:50:22 PM9/7/00
to
Cheng Cosine <aco...@yahoo.com> writes: >
> Hi ALL:

>
> In FORTRAN, there are two types of real floating number, single
>
> and double precision. Single precision has 6 significant digits
>
> while double precision has 16. ( when represented in 10 based )
>
> From what I learned from some numerical analysis books, the
>
> floating point system is machine dependent. Say there are other
>
> system used in Cray and IBM mainframe that are other than the
>
> IEEE SP/DP system.

>
> Now how can I write a FORTRAN90 code that works in lower significant
>
> number only. Say only works for 3, 4, or 10 digits?

Use double precision, or use SELECTED_REAL_KIND:

INTEGER, PARAMETER :: P = SELECTED_REAL_KIND(10)
REAL (KIND=P) :: X

However, bear in mind that the number of digits you specify map to the
available precisions (single or double, and maybe an extended precision).

0 new messages