Page 1 of 1

Div, mod, quot and rem differences

Posted: 25 Jun 2015 8:36
by Peter Muraya
What are the differences between the following operators:-
- div and quot ?
- mod and rem ?

Posted: 25 Jun 2015 12:58
by Thomas Linder Puls
The difference is in handling of negative numbers. See: Integral division.

Posted: 26 Jun 2015 7:14
by Peter Muraya
Thanks. I'm working with positive numbers and I note that div and quot have the same effect.

Posted: 26 Jun 2015 11:43
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.

Modulus with never negative result

Posted: 7 Jul 2015 12:39
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)).

Forcing real arguments of periodical functions in a beloved range

Posted: 1 Aug 2015 4:13
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