Fixing up the mod function to produce only positive results?
Subject: Fixing up the mod function to produce only positive results?
From: Robert Jerrard
Date: Mon, 14 Nov 2005 15:04:08 -0700
On Tue, 2005-15-11 at 08:43 +1100, Alasdair McAndrew wrote:
> I've been doing some modular arithmetic with Maxima, and I don't like
> the default behaviour than mod(m,n) returns a value between -(n-1)/2
> and n/2 (roughly speaking). For example.
>
> mod(18,7);
>
> returns -3.
>
> But I want my values to be positive, in the range 0..n-1. How can I
> do this? Is there a local variable I can set, or do I have to edit a
> lisp file (which I'd rather not do)?
I have the same thoughts regarding mod but until the default behaviour
is modified or an option allows for that I use the divide command.
divide(18,7)
returns [2,4], the first value 2 is the whole part of the division and
the second number is the remainder after division by 7 which is the mod
result you are looking for. You could define your own mod function
mymod(a,b):=part(divide(a,b),2)
and now
mymod(18,7)
returns 4
For what it is worth, Bob
--
Dr. Robert J. Jerrard, Professor of Mathematics,
Concordia University College of Alberta,
7128 Ada Blvd., Edmonton, Alberta, T5B 4E4, Canada.
Phone: (780) 479-9291, Fax: (780) 474-1933.