Calculating 1/2 on Python [duplicate] - python

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

Related

How to calculate Two's (2's) Complement in Python [duplicate]

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

Python : Is a number too big? [duplicate]

This question already has answers here:
Why does integer division yield a float instead of another integer?
(4 answers)
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 1 year ago.
I've learnt that python supports very large numbers with int itself.
But in this case :
print(int(12630717197566440579/10))
My answer is
1263071719756644096
and not
1263071719756644057
As it's supposed to be.
Can someone tell me why?

How do I use infinity in python? [duplicate]

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").

printing a number and writing in ipython interpreter gives different outputs [duplicate]

This question already has answers here:
Python floating-point math is wrong [duplicate]
(2 answers)
Closed 6 years ago.
t is array of two float64 numbers.
On typing t in Ipython 2.7, it is giving following output:
array([ 60.211127, 71.08120185])
print t gives
[ 60.211127, 71.08120185]
print t[0] gives
60.211127
but...
t[0] gives
60.211126999999998
as an output.
P.S.
from decimal import *
Decimal(t[0])
gives
Decimal('60.21112699999999762212610221467912197113037109375')
as output.Why is it happening so?
The issue I think you are having is because there is no way to approximate some values in some data formats. (the same way you can't show 1/3 because you would just have .3333333333333... forever) There is more info here
a useful function might be repr() more info here

Python returns 0 for all my divisions (even when using float()) [duplicate]

This question already has answers here:
How can I force division to be floating point? Division keeps rounding down to 0?
(11 answers)
Closed 7 years ago.
I'm using Spyder (Python 2.7)
Any division that would return a value below 1 returns 0.
When I use float(5/10) it returns 0.0
When I use:
'%.11f'%a after defining a = 10/20 it still returns 0.000000000..
I'm really new to Python, I'm sorry if this is a dumb question.
How do I fix this? Thank you
Very noob.
For this to work one must just import:
from __future__ import division

Categories

Resources