forums.silverfrost.com
Welcome to the Silverfrost forums

Author Message
Zach

Joined: 13 Mar 2023
Posts: 85
Location: Groningen, Netherlands

 Posted: Tue Mar 28, 2023 11:07 pm    Post subject: Precision question 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.
mecej4

Joined: 31 Oct 2006
Posts: 1839

 Posted: Wed Mar 29, 2023 12:04 am    Post subject: 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.
Zach

Joined: 13 Mar 2023
Posts: 85
Location: Groningen, Netherlands

 Posted: Wed Mar 29, 2023 2:02 am    Post subject: 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?
PaulLaidler

Joined: 21 Feb 2005
Posts: 7767
Location: Salford, UK

 Posted: Wed Mar 29, 2023 6:36 am    Post subject: amount = 1234567.25D0
Zach

Joined: 13 Mar 2023
Posts: 85
Location: Groningen, Netherlands

 Posted: Wed Mar 29, 2023 7:17 am    Post subject: 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.
Kenneth_Smith

Joined: 18 May 2012
Posts: 682
Location: Hamilton, Lanarkshire, Scotland.

Posted: Wed Mar 29, 2023 9:18 am    Post subject:

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!
 Code: 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

Last edited by Kenneth_Smith on Wed Mar 29, 2023 10:30 am; edited 1 time in total
Zach

Joined: 13 Mar 2023
Posts: 85
Location: Groningen, Netherlands

 Posted: Wed Mar 29, 2023 10:03 am    Post subject: 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!
wahorger

Joined: 13 Oct 2014
Posts: 1153
Location: Morrison, CO, USA

 Posted: Fri Mar 31, 2023 8:55 pm    Post subject: 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.
Zach

Joined: 13 Mar 2023
Posts: 85
Location: Groningen, Netherlands

 Posted: Fri Mar 31, 2023 10:03 pm    Post subject: 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) { COORD pos = {x, y}; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); }
wahorger

Joined: 13 Oct 2014
Posts: 1153
Location: Morrison, CO, USA

 Posted: Fri Mar 31, 2023 11:10 pm    Post subject: I'll reply in the appropriate thread.
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First
 All times are GMT + 1 Hour Page 1 of 1