top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to simulate C style integer division in Python3

+2 votes
656 views

I wanna simulate C style integer division in Python3.

So far what I've got is:

# a, b = 3, 4

import math
result = float(a) / b
if result > 0:
 result = math.floor(result)
else:
 result = math.ceil(result)

I found it's too laborious. Any quick way?

posted Jan 21, 2016 by Abhay

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

In Python3 you have to use the floor division operator '//'
For example:

result=a//b
answer Jan 21, 2016 by Alok Sharma
Similar Questions
+1 vote

The change in integer division seems to be the most insidious source of silent errors in porting code from python2 - since it changes the behavior or valid code silently.

I wish the interpreter had an instrumented mode to detect and report such problems.

+2 votes
win32api.keybd_event(code,0,0,0)
time.sleep(2)
win32api.keybd_event(code,0,win32con.KEYEVENTF_KEYUP,0)

Above code is simulating single click on button but not press Key Hold but I want to hold the until key up event is called
eg: for key 'a' down it have to simulate key continous set until keyUp is called but not like "a"

Please specify any python API to simulate continues keyDown until it receives keyUp event

+2 votes

Below instruction will start CPU stress with maximum of 100%,

while(1);

Below instructions will start CPU stress with near to 0%.

while(1)
     sleep(1);

Is there any way to control this? I mean what if i want to start CPU stress of >100% and what if want to start CPU stress with particular number?

Can any one help in programming that? I am looking for C/C++/Python solution on Linux...

0 votes

The last few days I've been working on a script to manipulate some scientific data. One thing I would like to be able to do is find relative maxima in a data set.
I'm using numpy in python3 (which I think I can't do without because of utf16 encoding of my data source) and a series of np.arrays. When looking around the web and some forums I came across the scipy function argrelextrema, which seemed to do just what I wanted. The problem is that I can't get the function to work, probably because scipy in python3 does not yet support the argrelextrema function. I can however, not find a reference to this really being the problem, and was wondering if someone here could maybe help me out.
The code I used is shown below. The function to return the maximum values is called by a different script. Then, when running, it returns an error like the one below the code.

Script:
import numpy as np
import scipy as sp

def max_in_array_range(MaxArray, Column, LowBound):

 "Finding the max value an array with a predefined in a certain column and from a threshold index.
 The complete array is loaded through Max Array. The column is first selected. 
 The, the LowBound is called (the data is only interesting from a certain point onward."
 TempArray = MaxArray[:, Column] # Creation of Temporary Array to ensure 1D Array and specification of the LowBound in the next line.
 return sp.argrelextrema(TempArray[LowBound:], np.greater)

Error message:
Traceback (most recent call last):
 File "MyScript.py", line 15, in 
 Varrr = FD.max_in_array_range(CalcAndDiffArray, 5 ,MyBound)
 File "/MyPath/Script.py", line 82, in max_in_array_range
 return sp.argrelmax(TempArray[LowBound:], np.greater)
AttributeError: 'module' object has no attribute 'argrelmax'
...