Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

MOD, & Backslash

564 views
Skip to first unread message

DonH

unread,
Jan 12, 2011, 1:11:27 PM1/12/11
to
Have just discovered, via Windows "Calculator", and the ever-useful book
"QBasic by Example" (pg.114), that MOD, and Backslash (\), are
math.operators in QBasic. Otherwise you end up with decimal places all over
the screen.
MOD gives an integer Remainder; Backslash, division of Integer only.
Quite useful in base-to-base conversion.
(I assume "Lsh" button on "Calculator" applies only to binary numbers?)


Derek

unread,
Jan 12, 2011, 4:33:49 PM1/12/11
to

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

ne...@rtrussell.co.uk

unread,
Jan 13, 2011, 9:40:57 AM1/13/11
to
On Jan 12, 9:33 pm, Derek <derek...@yahoo.ca> wrote:
> But watch out for their quirks. They don't always produce
> the answers that mathematics says they should.

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/

Derek

unread,
Jan 13, 2011, 2:18:10 PM1/13/11
to
On Jan 13, 7:40 am, "n...@rtrussell.co.uk" <n...@rtrussell.co.uk>
wrote:

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

DonH

unread,
Jan 16, 2011, 1:44:18 PM1/16/11
to
"Derek" <dere...@yahoo.ca> wrote in message
news:d0b7a9e6-3bfb-4f48...@m35g2000vbn.googlegroups.com...

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"?


Lawrence�Logic

unread,
Jan 18, 2011, 12:39:24 AM1/18/11
to

"DonH" <donlhu...@bigpond.com> wrote in message
news:80HYo.7944$gM3....@viwinnwfe01.internal.bigpond.com...

>
> (2) MOD formula in Excel won't recognise any =MOD(B6,C6), but only pure
> numeric =MOD(400,60)?
>

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


DonH

unread,
Jan 18, 2011, 2:46:23 PM1/18/11
to
"Lawrence Logic" <lawrence-Omit...@amd-p.com> wrote in message
news:uI9Zo.8086$gM3....@viwinnwfe01.internal.bigpond.com...

# 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.


0 new messages