float deviation in python list [duplicate] - python

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Python float - str - float weirdness
I run the following code in python on codepad.org:
num = 1.6
print num
list = [num]
print list
num2 = list[0]
print num2
And I get the following output:
1.6
[1.6000000000000001]
1.6
Why the tiny deviation in the list?

list.__str__ calls repr on its elements, where as print calls str:
>>> str(1.6)
'1.6'
>>> repr(1.6)
'1.6000000000000001'
Since floating point numbers are not guaranteed to be exact (and cannot be exact for values that can't be expressed as a * 2b for integers a,b), both representations are correct, or, in other words:
>>> 1.6 == 1.6000000000000001
True

Related

is there a function in python to round off three digits after decimal but show all three digits even if zero [duplicate]

This question already has answers here:
Rounding a number in Python but keeping ending zeros
(6 answers)
Closed 3 months ago.
i want to round off number 7.00087 and output should be 7.000
I tried round() function but it eliminates zero.
This function gives the right output:
def my_round(x, decimals):
str_decimals = str(x % int(x))
return str(int(x)) + str_decimals[1 : 2 + decimals]
What does my_round() do:
1- It retrieves decimals in a string format as a variable named str_decimals
2- Concatenates rounded int(x) with desired decimals and return as output.
Let's apply it:
Input
x = 7.00087
y = my_round(x, 3)
print(y)
Ouput
>>> 7.000

Python3 does not truncate the decimals of automatically like python2 [duplicate]

This question already has answers here:
Integer division in Python 2 and Python 3
(4 answers)
Why does the division get rounded to an integer? [duplicate]
(13 answers)
Closed 2 years ago.
In python2, if you did
n = 12
n /= 10
n would become 1.
In python 2,
the above would cause n to be 1.2, even if it were passed in an integer parameter like
def foo(self, n: int) -> bool:
print (n / 10)
return True
The simple fix is to just cast it to an integer like so:
n = int(n/10)
But this is quite memory/time costly. Are there better alternatives in python3?
// is integer division, so it removes the decimal from the result.

limited float decimal point without rounding the number [duplicate]

This question already has answers here:
How to truncate float values?
(31 answers)
Closed 4 years ago.
I want convert a number to a float number with 3 decimal point but I want it without rounding.
For example:
a = 12.341661
print("%.3f" % a)
This code return this number:
12.342
but I need to original number,I need this:
12.341
I write a code that receive a number form user and convert it to a float number.
I have no idea that what is the number entered with user.
My first thought was to change print("%.3f" % a) to print("%.3f" % (a-0.0005)) but it does not quite work: while it outputs what you want for a=12.341661, if a=12.341 it outputs 12.340, which is obviously not right.
Instead, I suggest doing the flooring explicitly using int():
a = 12.341661
b = int(a*1000)/1000.
print(b)
This outputs what you want:
12.341
To get 3 decimals out even if the input has fewer, you can format the output:
a = 3.1
b = int(a*1000)/1000.
print("%.3f" % b)
Outputs:
3.100
try str and slicing
a = 12.341661
b = str(int(a))
c = str(a - int(a))[1:5]
d = b + c
print(d)

Converting a binary string to the equivalent base 10 value in python [duplicate]

This question already has answers here:
Convert base-2 binary number string to int
(10 answers)
Closed 7 years ago.
I want to convert a binary string (user input) to its base 10 equivalent value. I think I am somewhat close but just can't get it. This is what I have come up with so far. I need it to go through each individual number in the binary string and assign its base 10 equivalent value, then add all of those up for the entire base 10 number.
def getBaseTen(myString):
x = myString[0]
needAnswers = len(myString)
n = len(myString)
two = (2^n)
if (needAnswers >= 1):
return (int(x)*int(two))
The built-in int() can do this with an optional argument for the base:
>>> a = '11010'
>>> int(a)
11010
>>> int(a, 2)
26
Something like this: int(x, base=2) Link for more on this.
def getBaseTen(myString):
int(myString, 2)

How do remove trailing numbers in python [duplicate]

This question already has answers here:
Limiting floats to two decimal points
(35 answers)
Closed 8 years ago.
I am working on a python project and get a problem.
I have a for loop that makes a list with numbers. The problem is when I print out the list to check if the numbers are correct.
Some of the more simpler calculations are:
2.6 * 3 and 1.3 * 9
If you check with a calculator you should get 7.8 and 11.7, but I get 7.800000000000001 and 11.700000000000001
Which is not a big problem, but I don't like how it looks. (First world problem, I know)
Is there any way to fix it?
Use string format?
print "%.2f" % (2.6 * 3)
The %.2f means print a float to 2dp
You can use format to display the output you want:
>>> print(format(2.6 * 3, ".1f"))
7.8
Here ".1f" means "floating point number to one decimal place".
You can use format print.
res = 2.6*3
"%0.2f" % res
Here's a complete example with a list.
>>> n = [1.9, 7.8 , 9,3.4]
>>> print n
[1.8999999999999999, 7.7999999999999998, 9, 3.3999999999999999]
>>> twodecimals = ["%.2f" % v for v in n]
>>> print twodecimals
['1.90', '7.80', '9.00', '3.40']
>>> print twodecimals[0]
1.90
%.2f means prints only 2 decimal points. Similarly, .1f means 1 decimal point and so on.

Categories