Python float number comparison [duplicate] - python

This question already has answers here:
What is the best way to compare floats for almost-equality in Python?
(18 answers)
Is floating point math broken?
(31 answers)
Closed 3 years ago.
Why when decreasing decimal part after 10.0000000000000008 the comparison returning True?
>>> 10 == 10.000000000000001
False
>>> 10 == 10.0000000000000001
True

Related

F-string in Python ":.3f" [duplicate]

This question already has answers here:
Limiting floats to two decimal points
(35 answers)
how to format float number in python? [duplicate]
(3 answers)
variable number of digit in format string
(3 answers)
Closed 1 year ago.
I'm reading this textbook called "Practical Statistics for Data Scientists" and this :.3f keeps getting used in almost every f-string. What does :.3f mean? My guess is it has something to do with floating point numbers.
Example:
{house_lm_factor.intercept_:.3f}
This is show you how many number are printing:
>>> import math
>>> flt = math.pi
>>> f'{flt:.3f}'
'3.142'
>>> f'{flt:.5f}'
'3.14159'
>>> f'{flt:.10f}'
'3.1415926536'

Python3 "math.sin" function returning incorrect result (the angle is in radians.) [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 1 year ago.
>>> import math
>>> math.sin(math.radians(180))
1.2246467991473532e-16
>>>
Isn't the sine of 180ยบ 0.0?
It is an approximation of pi. You can round it to 0.

How do I iterate through floating point numbers in python? [duplicate]

This question already has answers here:
How do I use a decimal step value for range()?
(34 answers)
Closed 3 years ago.
How do I iterate through floating point numbers in python?
for i in range(1,10,0.001):
print(i)
TypeError: 'float' object cannot be interpreted as an integer
One option:
for i in range(1000,10000):
print(i/1000)

How to convert String to Boolean Logic? [duplicate]

This question already has answers here:
Safely evaluate simple string equation
(4 answers)
Evaluating a mathematical expression in a string
(14 answers)
Closed 4 years ago.
I have sting input like:
12 == 21
(10 != 12) or (15 == 13)
True and (10 == 10)
And i need to return boolean value of this input. Can i convert it using exec() or bool()? I don't want to write big method to solve it .

Why does 1/len(list) return 0? [duplicate]

This question already has answers here:
Why does the division get rounded to an integer? [duplicate]
(13 answers)
Closed 5 years ago.
lets say I have a list a and a variable b = 1/len(a) but when I display the value of b it gives me 0
Because of integer divided by integer.
Try b=1.0/len(a)

Categories