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.
Related
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
This question already has answers here:
Is floating point math broken?
(31 answers)
Why are floating point numbers inaccurate?
(5 answers)
Closed 2 years ago.
I've written some script to help me out finding if something is divisible by other thing, and it fails with larger numbers:
print(x, d)
90744169766518547761620274468374441720233236940 10
print(x/d)
9.074416976651854e+45
print(x / (x/d))
10.0
print(x % (x/d))
2.535301200456459e+30
Since 10.0 is clearly lacking decimal part I don't undertand why % is giving me this trash output?
Does this do what you expect?
>>> print(x//d)
9074416976651854776162027446837444172023323694
>>> print(x // (x//d))
10
>>> print(x % (x//d))
0
The difference is that / in Python 3 always produces a floating point result, even if the operands are integers. In this it differs from C. If you want integer division you need to use //.
This question already has answers here:
Why does Python return 0 for simple division calculation?
(6 answers)
Closed 6 years ago.
So a list from 0 to 100 is created and I apply the formula in line two to make list B. The issue is that it's rounded to an int and I have been trying how to get it out to 3 decimals.
A = range(0,101)
B = [(x-10)/3 for x in A]
Ex:
If A = 11
(11-10)/3 = 0.333 instead of (11-10)/3 = 0.
Thanks!
You can use the function round(B, 3)
this should work
from __future__ import division
A = range(0,101)
B = [(x-10)/3 for x in A]
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)
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