Yes, useful. But watch out for their quirks. They don't always produce
the answers that mathematics says they should. As long as you are
dividing a positive number by a positive number or a negative number
by a negative, there is no problem.
So
11 mod 3 = 2 : 11 \ 3 = 3
-11 mod -3 = -2 : -11 \ -3 = 3
as expected. But when you mix positives and negatives things don't
quite work out the same as they should.
Mathematics says
11 mod -3 = -1 : 11 \ -3 = -4
-11 mod 3 = 1 : -11 \ 3 = -4
but QBASIC (not to mention a bunch of other computer languages) says
11 mod -3 = 2 : 11 \ -3 = -3
-11 mod 3 = -2 : -11 \ 3 = -3
This can lead to odd effects or wrong answers when you try to use
QBASIC to solve problems without taking its quirks into account. So
tread carefully.
Cheers
Derek
You seem here to be assuming that MOD refers to the mathematical
operation 'modulus', but it doesn't. Despite its name, MOD (as
usually implemented in programming languages) means 'remainder after
integer division' not 'modulus' (in the mathematical sense).
The only 'rule' that must apply is this one:
A = (A DIV B) * B + (A MOD B)
and you will notice that *all* the results you quote follow this rule.
Once you have defined how DIV (or \) works, then the way that MOD (or
%) works follows. The convention adopted by most programming
languages is that DIV, when giving a negative result, truncates
towards zero *not* towards minus infinity. This leads to the results
you quote for QBASIC.
There is more detail here:
http://mathforum.org/library/drmath/view/52343.html
Richard.
http://www.rtrussell.co.uk/
Absolutely. And that is why I said to watch out for "quirks". I did
not say that the QBASIC behaviour was wrong. I just said that it
didn't behave as you would expect. I have had to fix bugs introduced
as a result of programmers misunderstanding its behaviour in the past,
so I thought that it was worth raising the issue for those unfamiliar
with mod and \ who could quite easily miss this subtle point.
Cheers
Derek
Cheers
Derek
# Thanks for info.
I notice that -
(1) According to HELP in QBasic, MOD rounds off the divisor to nearest whole
number before dividing - but a test shows it will round off 6.5 to lower
integer.
(2) MOD formula in Excel won't recognise any =MOD(B6,C6), but only pure
numeric =MOD(400,60)?
(3) I assume that DIV and %, if not math operators in BASIC, are so in other
languages, or can be used by normal keyboard in some way?
(4) Is there a ROUND command, or some simple way of rounding off decimals
to, say, two decimal places? [I can achieve it by *100, using INT, then
/100] MOD obviously does it, but can such process be "intercepted"?
I guess it depends on the values of cells B6 and C6. =MOD(B6,C6) resolves
to 1 when B6 = 10 and C6 = 3 in Excel 2003 and 2010. That's what I'd
expect. It also worked fine when B6 and C6 were calculated based on other
cells. It looks pretty good to me.
If B6 = "fred", it dies in the arse. That's also what I'd expect.
What are you doing and what were you expecting?
--
Lawrence
"Swallow, come!" - Sea Man - 21 April 2010
# Yes, I should have persisted, instead of generalising on one instance.
After some initial seeming anomalies, I got my Office 2003 version of
Excel to work, even if MOD division by a decimal number gives inaccurate
result.
It is OK if all components are integers.