This question already has answers here:
Two's Complement in Python
(20 answers)
Closed 8 months ago.
I am writing my own Servo Driver Program in Python and I struggle quite a bit with Two's (2's) Complement calculation.
Example: I want to have "-100" as an input and get "FF9C" or just "9C" which would also work for the output.
The following picture might help to clarify:
Thanks so much for the help. I ended up using this for my code for other people having the same difficulty.
n = -100
x = f'{n&(2**16-1):x}'
print(x) # -> ff9c
Related
This question already has answers here:
Determine precision and scale of particular number in Python
(11 answers)
Closed 2 years ago.
I want to know how many decimals an input has to then use that information to aproximate the result of an operation to the same number of decimals. For example:
example = float(input('n '))
Imagine the input is 1.99, how do I obtain the number of decimals (2) to then use it?
Here's a simple hack to it.
dec = len(str(example).split('.')[1])
This question already has answers here:
How can I represent an infinite number in Python?
(13 answers)
Closed 2 years ago.
I tried making this function to return infinity so I could use it in equations.
but I keep getting an error that says something about stack overflow, so that's why I'm asking you guys.
def returnInfinity(num = 1):
return returnInfinity(num + 1)
INFINITY = returnInfinity()
Use the math library's math.inf if you're on version 3.5 or above.
import math
print(math.inf)
Otherwise, you can do int("inf") or float("inf").
This question already has answers here:
Python array multiply
(3 answers)
Closed 5 years ago.
I saw this questions a couple other places but all the fixes dont work for me code. If someone could help that would be great.
import random
cvalues=[]
for i in range(50):
cvalues.append(random.randrange(0,16))
float_cvalues=[float(i) for i in cvalues]
print(float_cvalues)
nvalues=[((.4*(float_cvalues)-.8))]
print(nvalues)
Multiplying a sequence in Python is interpreted as an attempt to create multiple sequences, see this post.
You can instead use another list comprehension:
nvalues=[.4*i-.8 for i in float_cvalues]
Or for instance switch to numpy arrays.
This question already has answers here:
Why does the division get rounded to an integer? [duplicate]
(13 answers)
Closed 6 years ago.
That's just basic math, 1/2 = 0.5, but it seems on Python the result is 0?
I thought that the result is in int format, but I've also tried float(1/2) and the result is still the same. Does anyone know why is this happening?
(Sorry for the bad English, not my native language. Also, I just got learning Python:D)
try
float(1) / float(2)
Hope this helps
This question already has answers here:
How to create a range of numbers with a given increment
(6 answers)
Closed 8 years ago.
Does anybody know whether Python can do the same thing as for i= 1:2:5 in Matlab? So i=1,3,5.
I know I can use other approaches to do this, but I want to know the equivalent form in Python.
try:
for i in xrange(1,6,2):
print i
This print:
1
3
5
Use xrange instead of range if you are using python 2.x because it is more efficient as it generates an iterable object, and not the whole list.
Use the range function:
for i in range(1, 6, 2):
print(i)