forums.silverfrost.com
Welcome to the Silverfrost forums

Author Message
Carl_Mesaros

Joined: 28 Nov 2016
Posts: 6

 Posted: Thu May 04, 2017 7:06 pm    Post subject: concerning the MOD intrinsic function Dear Silverfrost community: I do not know how to use the MOD intrinsic function. Could you suggest ways that I can learn how to use this function? Thank you. Best regards, Carl Mesaros
JohnCampbell

Joined: 16 Feb 2006
Posts: 1774
Location: Sydney

Posted: Mon May 08, 2017 7:00 am    Post subject:

Try something like the following. This may show some of how it works.
 Code: integer i,j do i = -10,10   j = mod(i,4)   write (*,*) i,j, mod(i,-4) end do end

Note:
mod(I+p,p) = mod(I,p)
mod (-I,p) = - mod(I,p)
mod(I,-p) = mod(I,p) { which can be annoying when 0 < I < p, as mod(I+p,p) = mod(I,p) but mod(I-p,p) /= mod(I,p) }
mecej4

Joined: 31 Oct 2006
Posts: 732

Posted: Mon May 08, 2017 12:35 pm    Post subject: Re:

 JohnCampbell wrote: mod(I+p,p) = mod(I,p)

Not always!

Counterexample: mod(-2, 3) = -2, but mod(-2+3, 3) = 1

It is quite easy to make such slips if one or both the arguments to the mod() function, or its sister function modulo(), are negative. Modular arithmetic can be counter-intuitive if one is not used to it. For example, (a + b) mod c need not equal (a mod c) + (b mod c); consider a=1, b=1, c=2.

The rule is: Given two numbers (real or integer) A and P, of the same type and kind, find the integer K = INT(A/P) in the case of mod() and K = FLOOR(A/P) in the case of modulo(). The value of the function is A - K*P, and '/' has its mathematical meaning, not "integer division".

The functions can be used in a number of situations where one has to find the nearest from a set of discrete values. I use the mod() function frequently in an iterative algorithm to monitor the progress: IF (mod(ITER, PRNTINTVL) == 0) WRITE(*,*)...
JohnCampbell

Joined: 16 Feb 2006
Posts: 1774
Location: Sydney

 Posted: Tue May 09, 2017 12:33 am    Post subject: For reporting progress I try to use an alternative form to "IF (mod(ITER, PRNTINTVL) == 0) WRITE(*,*)..." IF ( ITER >= Next_PRNT_ITER ) THEN WRITE(*,*)... Next_PRNT_ITER = Next_PRNT_ITER + PRNTINTVL END IF This can create greater flexibility by varying PRNTINTVL or starting with a different Next_PRNT_ITER. The following all give a different initial report, which can be useful for confirming the initial cycling: Next_PRNT_ITER = -PRNTINTVL Next_PRNT_ITER = 0 Next_PRNT_ITER = PRNTINTVL
 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