How do I use infinity in python? [duplicate] - python

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

Related

Cant multiply sequence by Float for Python [duplicate]

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.

python print error, math incorrect (1/10=0) [duplicate]

This question already has answers here:
Script printing all zeros [duplicate]
(2 answers)
Basic python arithmetic - division
(6 answers)
Closed 5 years ago.
when I startup python, I don't import anything and I just enter 1/10, it returns 0. Any idea how to fix this?

Calculating 1/2 on Python [duplicate]

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

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

why float number operate is so strange in python? [duplicate]

This question already has answers here:
Python rounding error with float numbers [duplicate]
(2 answers)
Python floating-point math is wrong [duplicate]
(2 answers)
Closed 9 years ago.
In my python,you can see:
>>> 0.6+0.8
1.4
>>> 1.6+0.8
2.4000000000000004
why the result is so strange?
I believe this is a problem with caluclating floats with binary rather than python,
http://docs.python.org/2/library/decimal.html explains it better than I could, in short
import decimal
num1 = decimal.Decimal("1.6")
num2 = decimal.Decimal("0.8")
num1 + num2
Writing a function to decimal your stuff for you will be easy enough.
This is because of floating point rounding error.
Read basic here: http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
And the solution for Python for your case:
http://floating-point-gui.de/languages/python/

Categories

Resources