forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Precision question

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General
View previous topic :: View next topic  
Author Message
Zach



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

PostPosted: Tue Mar 28, 2023 11:07 pm    Post subject: Precision question Reply with quote

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.
Back to top
View user's profile Send private message Send e-mail
mecej4



Joined: 31 Oct 2006
Posts: 1885

PostPosted: Wed Mar 29, 2023 12:04 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
Zach



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

PostPosted: Wed Mar 29, 2023 2:02 am    Post subject: Reply with quote

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?
Back to top
View user's profile Send private message Send e-mail
PaulLaidler
Site Admin


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

PostPosted: Wed Mar 29, 2023 6:36 am    Post subject: Reply with quote

amount = 1234567.25D0
Back to top
View user's profile Send private message AIM Address
Zach



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

PostPosted: Wed Mar 29, 2023 7:17 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message Send e-mail
Kenneth_Smith



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

PostPosted: Wed Mar 29, 2023 9:18 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Visit poster's website
Zach



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

PostPosted: Wed Mar 29, 2023 10:03 am    Post subject: Reply with quote

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!
Back to top
View user's profile Send private message Send e-mail
wahorger



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

PostPosted: Fri Mar 31, 2023 8:55 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message Visit poster's website
Zach



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

PostPosted: Fri Mar 31, 2023 10:03 pm    Post subject: Reply with quote

What I am on about is displaying something at a certain position on the screen. As to age, I am 86 Sad

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);
}
Back to top
View user's profile Send private message Send e-mail
wahorger



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

PostPosted: Fri Mar 31, 2023 11:10 pm    Post subject: Reply with quote

I'll reply in the appropriate thread.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group