This question already has answers here:
What is the best way to compare floats for almost-equality in Python?
(18 answers)
Closed 3 years ago.
I have two numbers, one I get by calculating it and the other one I bring it from the database.
calculated = 2.183333333333333
database = 2.18333333333333
But when I compare them to know if they are the same, I return False when it should be True.
There is some way to limit the number of periodic numbers, but not to affect decimals that are not periodic, for example:
2.1748888888888 -> 2.1748
1.23333333 -> 1.23
You could use the math.isclose method:
>>> from math import isclose
>>> calculated = 2.183333333333333
>>> database = 2.18333333333333
>>> isclose(calculated, database)
True
This allows for setting the relative tolerance and minimum absolute tolerance as well refer to the docs for more explanation.
Related
This question already has answers here:
How can I use "e" (Euler's number) and power operation?
(7 answers)
Closed 3 months ago.
What is the easiest/most optimal way of finding the exponential of a number, say x, in Python? i.e. how can I implement e^x?
The easiest and most optimal way to do e^x in Python is:
from math import exp
print(exp(4))
Output
>>> 54.598150033144236
You can use the math.exp() function from the math module (read the docs).
>>> import math
>>> x = 4
>>> print(math.exp(x))
54.598150033144236
This question already has answers here:
Limiting floats to two decimal points
(35 answers)
Closed last year.
I have two float values 1000.4 and 700.7
The result of 1000.4 - 700.7 returns me 299.69999999999993. I did my research,Decimal would be a good option to do the calculation here print(Decimal('1000.4') - Decimal('700.7')) and it returns 299.7
I have a question. what if I have a value 14.5 how can I print it as 14.50?
I tried getcontext().prec = 2 and it didn't help and would made the print(Decimal('1000.4') - Decimal('700.7')) returns 3.0E+2 which isn't what I want.
You can use f-strings to custom the leading zeros/limit the precision.
n=14.5
print(f"{n:.2f}")
Here, it would print only the first two decimals. (14.50).
This can be done with formatting for both floats and Decimal.
In [1]: print("{:.2f}".format(1.234))
1.23
In [2]: from decimal import Decimal
In [3]: print("{:.2f}".format(Decimal(1.234)))
1.23
This question already has answers here:
Python round up integer to next hundred
(10 answers)
Closed 2 years ago.
The built-in function round() will round a value down but I want to know how to round a value up.
i know that this is possible with math.ceil() but the thing is that round() has the keyword argument "ndigits" and math.ceil() doesn't. so for example:
>>> round(1024, ndigits=-3)
1000
but i want 1100.
Is there a solution for this?
import math
def round(number, n):
return math.ceil(number * math.pow(10, n+1))*(math.pow(10,-(n+1)))
print(round(1024, -3))
# 1100.0
A simple function like this would suffice, multiply by 10^(n+1), find the ceiling of that number, then multiply by 10^-n-1 (equivalent to dividing by 10^n+1).
This question already has answers here:
Negative integer division surprising result
(5 answers)
Closed 7 years ago.
How python calculate this division?
>>>-3/10
-1
Looks like python rounds the answer to the lower value.
>>> -3/4
-1
>>> -3/4.
-0.75
>>> -3/10.
-0.3
>>> -3/10
-1
This is just my guess.
Python 2, like many languages, uses integer division. Dividing two integers returns a integer (the nearest integer to the answer rounded down.)
To get a floating point result, you need to force one or more of the terms to be a float.
float(-3)/10
This question already has answers here:
How to truncate float values?
(31 answers)
Closed 9 years ago.
I've found dozen of answers, but non of them is what I'm looking for, I don't want to round up or down, I know that I can round numbers as follow:
>>> print('%.3f' % 15.555555)
15.556
>>> round(15.555555, 3)
15.666
But I need to get 15.555. Should I use regex?
Cheeky solution:
numstring = str(15.555555)
num = float(numstring[:numstring.find('.')+4])
My solution involving int abuse. int rounds towards the nearest 0. I multiply it by 10**3 to affect rounding. After using int, I divide it by 10**3 to get actual results.
It's safer, as it does work with e notation.
int(15.55555 * 10**3) / 10.0**3