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 

concerning the MOD intrinsic function

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



Joined: 28 Nov 2016
Posts: 6

PostPosted: Thu May 04, 2017 7:06 pm    Post subject: concerning the MOD intrinsic function Reply with quote

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



Joined: 16 Feb 2006
Posts: 1723
Location: Sydney

PostPosted: Mon May 08, 2017 7:00 am    Post subject: Reply with quote

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



Joined: 31 Oct 2006
Posts: 652

PostPosted: Mon May 08, 2017 12:35 pm    Post subject: Re: Reply with quote

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(*,*)...
Back to top
View user's profile Send private message
JohnCampbell



Joined: 16 Feb 2006
Posts: 1723
Location: Sydney

PostPosted: Tue May 09, 2017 12:33 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support 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