473,398 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

(Numeric) should -7 % 5 = -2 ?

Is the following behavior expected, or have I broken my Numeric
installation somehow?

$python
Python 2.2.2 (#1, Mar 21 2003, 23:01:54)
[GCC 3.2.3 20030316 (Debian prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import Numeric
Numeric.__version__ '23.0' -7 % 5 3 Numeric.array(-7) % 5 -2 Numeric.remainder(-7, 5)

-2

--
Stan
Jul 18 '05 #1
8 2132
Stan Heckman wrote:
Is the following behavior expected, or have I broken my Numeric
installation somehow?

$ python
import Numeric
Numeric.__version__ '23.0' -7 % 5 3 Numeric.array(-7) % 5 -2 Numeric.remainder(-7, 5)

-2


looks like Numeric implements C semantics, which is different
from how Python does it.

</F>


Jul 18 '05 #2
"Louis M. Pecora" <pe****@anvil.nrl.navy.mil> wrote:
Fredrik Lundh <fr*****@pythonware.com> wrote:
> >>> -7 % 5
> 3
> >>> Numeric.array(-7) % 5
> -2
> >>> Numeric.remainder(-7, 5)
> -2


looks like Numeric implements C semantics, which is different
from how Python does it.


Hmmm... "remainder" makes sense. But "%" is mod, right. IIRC from my
abstract algebra days (only 30 yrs ago :-) ) The "X mod n" function
maps onto the postive integers from 0 to n-1. So sounds like numeric
contradicts the math texts. Not good since it's a math module.


That's a bit harsh. The problem is that there is no universal agreement in
the world of computer science as to what the semantics of the modulo
operator should be when presented with a negative operand. Contradicting
Fredrik, something I do with great reluctance, the C standard specifies
that the behavior is implementation-defined, so in fact BOTH answers
"implement C semantics".

Fortran, on the other hand, defines A mod P as A-INT(A/P)*P, which is
exactly what Numeric produces. Since folks interested in numerical
programming often have a strong Fortran background, it is not terribly
surprising that Numeric should follow Fortran's lead.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 18 '05 #3

"Tim Roberts" <ti**@probo.com> wrote in message news:3g********************************@4ax.com...
Contradicting Fredrik, something I do with great reluctance, the C standard
specifies that the behavior is implementation-defined, so in fact BOTH answers
"implement C semantics".


Only if you're looking at an old version of the standard; it's not implementation
dependent in C99.
Jul 18 '05 #4
In article <3g********************************@4ax.com>, Tim Roberts
<ti**@probo.com> wrote:
Hmmm... "remainder" makes sense. But "%" is mod, right. IIRC from my
abstract algebra days (only 30 yrs ago :-) ) The "X mod n" function
maps onto the postive integers from 0 to n-1. So sounds like numeric
contradicts the math texts. Not good since it's a math module.


That's a bit harsh.


You may be right. I got to work and checked my old Abstract Algebra
book. The defintion is,

We write a=b mod m if m divides (a-b) (i.e. no remeinder).

The defintion does not say how to compute the mod, rather it is an
expression of a relationship between a and b. Hence, writing -2=-7 mod
5 appears to be OK.

The "uniqueness" comes in when we recogize that mod m defines an
equivalence relation on the integers and so for a given m every integer
falls into a unique class (or subset of integers). The set of m
subsets is equivalent to the positive integers 0 to m-1.

So it appears that the translation between math and computer science is
not as clear as I thought. In math (well, number theory) mod is a
relation, not an operation. In computer science it is an operation.

Waddayathink?

--
Lou Pecora
- My views are my own.
Jul 18 '05 #5
At 11:19 AM 7/1/2003 -0400, Louis M. Pecora wrote:
In article <3g********************************@4ax.com>, Tim Roberts
<ti**@probo.com> wrote:
Hmmm... "remainder" makes sense. But "%" is mod, right. IIRC from my
abstract algebra days (only 30 yrs ago :-) ) The "X mod n" function
maps onto the postive integers from 0 to n-1. So sounds like numeric
contradicts the math texts. Not good since it's a math module.


That's a bit harsh.


You may be right. I got to work and checked my old Abstract Algebra
book. The defintion is,

We write a=b mod m if m divides (a-b) (i.e. no remeinder).

The defintion does not say how to compute the mod, rather it is an
expression of a relationship between a and b. Hence, writing -2=-7 mod
5 appears to be OK.
[snip]


To quote from "Number Theory and its History" by Oystein Ore, page 213f:
"When an integer a is divided by another m, one has
a = km + r
where the remainder is some positive integer less than m. Thus for any
number a there exists a congruence
a (is congruent to) r (mod m)
where r is a unique one among the numbers 0, 2, 1, .... m-1"

Bob Gailer
bg*****@alum.rpi.edu
303 442 2625
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.492 / Virus Database: 291 - Release Date: 6/24/2003

Jul 18 '05 #6
"Louis M. Pecora" wrote:
We write a=b mod m if m divides (a-b) (i.e. no remeinder).

The defintion does not say how to compute the mod, rather it is an
expression of a relationship between a and b. Hence, writing -2=-7
mod
5 appears to be OK.


Right. Equivalences modulo m are really alternate numerical spaces in
which arithmetic is done; in mathematics, the modulo is not strictly an
operator. In those cases, you don't really have to pick a unique
residue when doing arithmetic (mod m), since it's all equivalence
relation anyway.

In computer science, where modulo is an operator that must return a
unique value, it's not really specified whether (-n % m) (m, n positive)
should be negative or not. Some languages/systems chose negative, some
don't. The choice is never "wrong" unless it's done inconsistently
within a particular language/system.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ People are taught to be racists.
\__/ Jose Abad
Jul 18 '05 #7
In article <02***********************@anvil.nrl.navy.mil>,
Louis M. Pecora wrote:
In article <ma**********************************@python.org >, Bob
Gailer <bg*****@alum.rpi.edu> wrote:
"When an integer a is divided by another m, one has
a = km + r
where the remainder is some positive integer less than m.
Maybe I'm misunderstanding this, but what about -7 divided by 5? We
get k=-1 and r=-2.


-7 = (-2) * 5 + 3

So actually, k = -2 and r = 3

m can be negative. Maybe your quote was for the positive integers only.


True, that definition does not make sense when m is negative. When I came
across the subject in introductory group theory they said "positive divisor".

Julian
Jul 18 '05 #8
In article <02***********************@anvil.nrl.navy.mil>,
Louis M. Pecora wrote:
In article <ma**********************************@python.org >, Bob
Gailer <bg*****@alum.rpi.edu> wrote:
"When an integer a is divided by another m, one has
a = km + r
where the remainder is some positive integer less than m.
Maybe I'm misunderstanding this, but what about -7 divided by 5? We
get k=-1 and r=-2.


-7 = (-2) * 5 + 3

So actually, k = -2 and r = 3

m can be negative. Maybe your quote was for the positive integers only.


True, that definition does not make sense when m is negative. When I came
across the subject in introductory group theory they said "positive divisor".

Julian
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kyler Laird | last post by:
Python 2.3.3 (#2, Jan 4 2004, 12:24:16) on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import Numeric >>> import MLab >>> import Scientific.Statistics...
0
by: Travis Oliphant | last post by:
Numarray is making great progress and is quite usable for many purposes. An idea that was championed by some is that the Numeric code base would stay static and be replaced entirely by Numarray. ...
2
by: Johannes Nix |Johannes.Nix | last post by:
Hi, I have a tricky problem with Numeric. Some time ago, I have generated a huge and complex data structure, and stored it using the cPickle module. Now I want to evaluate it quickly again on a...
4
by: Gezeala 'Eyah' Bacu\361o II | last post by:
hey guys..need your help on this.. i have a plpgsql function where in i compute numeric values for my php scripts.. my problem is my function just won't round some numbers properly.. what i...
6
by: M.A. Oude Kotte | last post by:
Hi All, I hope this is the correct mailing list for this question. But neither postgresql.org nor google could help me out on this subject. I did find one disturbing topic on the mailing list...
10
by: Bryan | last post by:
hi, what is the difference among numeric, numpy and numarray? i'm going to start using matplotlib soon and i'm not sure which one i should use. this page says, "Numarray is a...
7
by: Sheldon | last post by:
Hi, I have the following loop that I think can be written to run faster in Numeric. I am currently using Numeric. range_va = main.xsize= 600 main.ysize= 600 #msgva is an (600x600) Numeric...
0
by: robert | last post by:
just a note - some speed comparisons : 0.60627370238398726 0.42836673376223189 0.36965815487747022 0.016557970357098384 0.15692469294117473 0.01951756438393204
15
by: W. Watson | last post by:
For some reason Python 2.2.4 cannot find the Numeric module. It's been suggested that I should re-install the Numeric file. How do that? Also the PIL. The three install files are: python-2.4.4.msi...
13
by: nishit.gupta | last post by:
Is their any fuction available in C++ that can determine that a string contains a numeric value. The value cabn be in hex, int, float. i.e. "1256" , "123.566" , "0xffff" Thnx
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.