The texts on precision make my head spin. Please, how do I declare a variable that has max 8 number positions before the decimal point and two after, plus additional positions for obligatory .'s and ,'s ? Thank you, Zach.
Precision question
Fortran does not support fixed point real numbers. It supports floating point arithmetic. For the range of numbers that you specified, you will find that double precision provides sufficient precision most of the time.
If you add up a column of numbers that represent amounts of money in Euros or Dollars, however, be prepared for the last cent to be in error now and then as a result of using binary floating point.
program test double precision::amount amount = 1234567.25 write(*,100)amount 100 format(F13.2) end program test
Up until seven positions before the decimal point, the decimals (cents) are nicely displayed, however, add an eighth position and '00' cents are displayed. If I code (F20.2) the cents are back again. Am I missing something in my ignorance?
The compiler says: warning 1226 - A REAL value has been truncated with possible loss of precision - maybe a KIND is required
Could you please amend my little example to cure the problem?
amount = 1234567.25D0
Bingo. Thank you. Though my do-what requirement re this issue is limited, since I will only attempt financial applications, the absence of do-why teases.
You may find that the results obtained from running this program which simply sums a series of numbers gives you some insight into single vs. double precision. In single precision the four different methods of summation gives three different answers!
program roundoff
implicit none
integer, parameter :: n = 10000000
integer i
real array1(1:n), sum1
double precision array2(1:n), sum2
! Single precision
forall (i=1:n) array1(i) = 1.0/real(i) ! 1, 1/2, 1/3, 1/4, 1/5 ...... 1/n
sum1 = 0.0
do i = 1, n, 1
sum1 = sum1 + array1(i) ! Sum array from largest 1, to smallest 1/n
end do
print*, 'Single precision'
print*, 'Summation in decreasing order: ', sum1
sum1 = 0.0
do i = n, 1, -1
sum1 = sum1 + array1(i) ! Sum array from smallest 1/n to largest 1
end do
print*, 'Summation in increasing order: ', sum1
print*, 'Summation using intrinsic SUM: ', sum(array1)
print*, 'Summation using KSUMSP: ', ksumsp(array1,n)
! Double precision
forall (i=1:n) array2(i) = 1.0/dble(i)
sum2 = 0.d0
do i = 1, n, 1
sum2 = sum2 + array2(i) ! Sum array from largest 1, to smallest 1/n
end do
print*
print*, 'Double precision'
print*, 'Summation in decreasing order: ', sum2
sum2 = 0.d0
do i = n, 1, -1
sum2 = sum2 + array2(i) ! Sum array from smallest 1/n to largest 1
end do
print*, 'Summation in increasing order: ', sum2
print*, 'Summation using intrinsic SUM: ', sum(array2)
print*, 'Summation using KSUMDP: ', ksumdp(array2,n)
contains
real function ksumsp(x,n)
integer n
real x(n), t, y, c
ksumsp = x(1)
c = 0.0
do i = 2, n, 1
y = x(i) - c
t = ksumsp + y
c = (t - ksumsp) - y
ksumsp = t
end do
end function ksumsp
double precision function ksumdp(x,n)
integer n
double precision x(n), t, y, c
ksumdp = x(1)
c = 0.d0
do i = 2, n, 1
y = x(i) - c
t = ksumdp + y
c = (t - ksumdp) - y
ksumdp = t
end do
end function ksumdp
end program roundoff
PS. See the following link for explanation of ksumsp and ksumdp https://en.wikipedia.org/wiki/Kahan_summation_algorithm
I understand from your impressive example that summating a large population of real numbers in different ways, will result in different outcomes, whereas if you do exactly the same, but with double precision numbers, the outcomes will be identical. So in other words, using double precision numbers is the way to go. Thank you for the impressive example!
Do not make the mistake that if you use double precision, all your results will be as you expect. Even double precision has limits to its ability to precisely represent a number accurate AND precise to 2 decimals when subject to some undefined number of arithmetic operations. The imprecision will eventually cause differences. Not so bad for personal usage (do you really care if you are one penny off?), but unacceptable for commercial work
Many years ago, a company I worked for used accounting software written in FORTRAN. There were a LOT of places in the calculations where the floating point format for the machine (pre-dated IEEE standards) was tweaked; they went from FORTRAN directly into assembly language, bit manipulated the results to more closely guarantee precision of the results, then reverted to FORTRAN again to continue.
If you were lucky enough to use IBM's packed decimal format (now I'm really showing my age), you get around all these precision issues.
What I am on about is displaying something at a certain position on the screen. As to age, I am 86 😦
Using Windows, C, and windows.h you can do this: (gratefully copied from someone else by the way)
void locate(short x, short y) ; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); }
I'll reply in the appropriate thread.