Discussions related to Visual Prolog
Peter Muraya
VIP Member
Posts: 147
Joined: 5 Dec 2012 7:29

Div, mod, quot and rem differences

Unread post by Peter Muraya »

What are the differences between the following operators:-
- div and quot ?
- mod and rem ?
Mutall Data Management Technical Support
User avatar
Thomas Linder Puls
VIP Member
Posts: 1398
Joined: 28 Feb 2000 0:01

Unread post by Thomas Linder Puls »

The difference is in handling of negative numbers. See: Integral division.
Regards Thomas Linder Puls
PDC
Peter Muraya
VIP Member
Posts: 147
Joined: 5 Dec 2012 7:29

Unread post by Peter Muraya »

Thanks. I'm working with positive numbers and I note that div and quot have the same effect.
Mutall Data Management Technical Support
User avatar
Thomas Linder Puls
VIP Member
Posts: 1398
Joined: 28 Feb 2000 0:01

Unread post by Thomas Linder Puls »

Yes, for positive numbers there is no difference.

div has the mathematical sensible property:

Code: Select all

B div C = (B-C) div C - 1
I.e. the result becomes one less if you subtract the divisor from the dividend.

quot does not preserve this property: the point where the result should go from 0 to -1 it will stay once more at 0. I am not really sure when you want this behavior.

We have introduced quot/rem to maintain access to the behavior div/mod used to have.
Regards Thomas Linder Puls
PDC
User avatar
Ferenc Nagy
VIP Member
Posts: 215
Joined: 24 Apr 2007 12:26

Modulus with never negative result

Unread post by Ferenc Nagy »

Code: Select all

predicates % 2015.07.07. Modulus with real arguments and results>=0.     realModU:(real Divided, ureal Divisor) -> ureal Modulus.     realModR:(real Divided, real Divisor) -> ureal Modulus.   clauses % 2015.07.07. Modulus with real arguments and results>=0.     realModU(Divided,Divisor) = Divided-Divisor*floorToReal(Divided/Divisor).     realModR(Divided,Divisor) = Divided-abs(Divisor)*floorToReal(Divided/abs(Divisor)).
TIA, Regards,
Frank Nagy
User avatar
Ferenc Nagy
VIP Member
Posts: 215
Joined: 24 Apr 2007 12:26

Forcing real arguments of periodical functions in a beloved range

Unread post by Ferenc Nagy »

Corrected version

Code: Select all

predicates % 2015.07.07. Modulus with real arguments and results>=0.     realModP:(real Divided, positive Divisor) -> ureal Modulus. % 2015.07.19.     realModU:(real Divided, ureal Divisor) -> ureal Modulus.     realModR:(real Divided, real Divisor) -> ureal Modulus.   clauses % 2015.07.19. Modulus with real arguments and results>=0.     realModP(Divided,Divisor) = Divisor*CorrectedFraction :-         Fraction=fraction(Divided/Divisor),         if Fraction<0 then             CorrectedFraction=1+Fraction %<=== ESSENCE OF CORRECTION         else             CorrectedFraction=Fraction         end if.       realModU(Divided,Divisor) =  Divisor*CorrectedFraction :-         Fraction=fraction(Divided/Divisor),         if Fraction<0 then             CorrectedFraction=1+Fraction         else             CorrectedFraction=Fraction         end if.       realModR(Divided,Divisor) = realModU(Divided,abs(Divisor)).
The essence of the correction is the

Code: Select all

        if Fraction<0 then             CorrectedFraction=1+Fraction         else             CorrectedFraction=Fraction         end if.
insertion.
Example usage

Code: Select all

 
TIA, Regards,
Frank Nagy
Post Reply