Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing python math module behaviour for non-positive numbers division

Non-positive number division is quite different in c++ and python programming langugages:

//c++:
11 / 3 = 3
11 % 3 = 2
(-11) / 3 = -3
(-11) % 3 = -2
11 / (-3) = -3
11 % (-3) = 2
(-11) / (-3) = 3
(-11) % (-3) = -2

So, as you can see, c++ is minimizing quotient. However, python behaves like that:

#python
11 / 3 = 3
11 % 3 = 2
(-11) / 3 = -4
(-11) % 3 = 1
11 / (-3) = -4
11 % (-3) = -1
(-11) / (-3) = 3
(-11) % (-3) = -2

I can't code my own division function behaving like c++, because I'll use it for checking c++ calculator programs, and python does not support infix operators. Can I make python behaving like c++ while dividing integers in a simple way? For example, setting some flag or something like that?

like image 232
karlicoss Avatar asked May 21 '11 21:05

karlicoss


People also ask

How do you write division in Python?

In Python 3. x, slash operator ("/") does true division for all types including integers, and therefore, e.g. 3/2==1.5. The result is of a floating-point type even if both inputs are integers: 4 / 2 yields 2.0.

How do you do floor division in Python?

In Python, we can perform floor division (also sometimes known as integer division) using the // operator. This operator will divide the first argument by the second and round the result down to the nearest whole number, making it equivalent to the math. floor() function.

What is Fmod Python?

fmod() function is one of the Standard math library function in Python, which is used to calculate the Module of the specified given arguments. Syntax: math.fmod( x, y ) Parameters: x any valid number (positive or negative). y any valid number(positive or negative).

How do you divide without a remainder in Python?

Use the floor division operator // to divide without a remainder, e.g. result_1 = 25 // 4 . The floor division operator will always return an integer and is like using mathematical division with the floor() function applied to the result.


2 Answers

As Thomas K said, use math.fmod for modulo, or if you really want you can define it yourself:

def cmod(x, y):
    return abs(x) % abs(y) * (1 if x > 0 else -1)

And this function should emulate C-style division:

def cdiv(x, y):
    return abs(x) / abs(y) * cmp(x, 0) * cmp(y, 0)

You said that you must use the / and % operators. This is not possible, since you can't override the operator for built-ins. You can however define your own integer type and operator overload the __div__ and __mod__ operators.

like image 96
orlp Avatar answered Sep 30 '22 07:09

orlp


There is no flag you can set to make python division to act like c++.

You advised that you can't code your own division function, but if you change your mind you can do this:

def cpp_int_div(dividend, divisor):
    a, b = dividend, divisor
    sign = 1 if (a>0 and b>0) or (a<0 and b<0) else -1
    return (abs(a)/abs(b)) * sign

def cpp_int_mod(dividend, divisor): # or just use math.fmod  (from Thomas K)
    a, b = dividend, divisor
    sign = 1 if a>0 else -1
    return (abs(a)%abs(b)) * sign

This shows that it acts according to your specification:

print "11 / 3 = %d" % cpp_int_div(11,3)
print "11 %% 3 = %d" % cpp_int_mod(11,3)
print "(-11) / 3 = %d" % cpp_int_div(-11, 3)
print "(-11) %% 3 = %d" % cpp_int_mod(-11, 3)
print "11 / (-3) = %d" % cpp_int_div(11, -3)
print "11 %% (-3) = %d" % cpp_int_mod(11, -3)
print "(-11) / (-3) = %d" % cpp_int_div(-11, -3)
print "(-11) %% (-3) = %d" % cpp_int_mod(-11, -3)

Which gives:

11 / 3 = 3
11 % 3 = 2
(-11) / 3 = -3
(-11) % 3 = -2
11 / (-3) = -3
11 % (-3) = 2
(-11) / (-3) = 3
(-11) % (-3) = -2
like image 26
Steven Rumbalski Avatar answered Sep 30 '22 07:09

Steven Rumbalski