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.
first = float(input("Enter first number: "));
second = float(input("Enter second number: "));
avg = float((first + second) / 2);
print(str(avg));
Using the numbers 1.1 and 1.3 as inputs, the expected output is 1.2. However, the result I'm receiving is 1.2000000000000002. I understand that this is related to Python and it's datatypes.
However, I'm unsure of how to evaluate this correctly, or why this specific result is achieved.
EDIT: Python 3.2
Use decimals:
import decimal
first = decimal.Decimal('1.1')
second = decimal.Decimal('1.3')
avg = (first + second) / 2
print(avg)
Related
This question already has answers here:
Python int to binary string?
(36 answers)
Closed last year.
How can i loop through a decimal number and print out a binary figure for that decimal number.
This is the code i tried using python
dec_Num = 1200
for i in dec_Num;
print i
Because the goal is to print we can generate a string
def DecimalToBinary(num):
strin=""
while num >= 1:
strin+=str(num%2)
num=num // 2
return strin[::-1]
This question already has answers here:
How can I force division to be floating point? Division keeps rounding down to 0?
(11 answers)
Closed 5 years ago.
I have the following program
def F_inf(a,b):
x1=a.numerator/a.denominator
x2=b.numerator/b.denominator
if x1<x2:
print "a<b"
elif x1>x2:
print "a>b"
else: print "a=b"
a=Fraction(10,4)
b=Fraction(10,4)
F_inf(a, b)
When I execute it,x1 receive just the integer value of the fraction, for exemple if I have to compute 2/4 x1 is equal to 0 not 0.5.
What should I do ?
Thanks
It sounds like you're using Python2. The best solution would be to switch to Python 3 (not just because of the division but because "Python 2.x is legacy, Python 3.x is the present and future of the language").
Other than that you have a couple of choices.
from __future__ import division
# include ^ as the first line in your file to use float division by default
or
a = 1
b = 2
c = a / (1.0*b) # multiplying by 1.0 forces the right side of the division to be a float
#c == 0.5 here
This question already has answers here:
Python round up integer to next hundred
(10 answers)
Closed 6 years ago.
I am trying to round numbers up in python 3. In my existing code, the number either round up to the nearest 10 or down. For example, 67 goes to 70 and 64 goes to 60. I would like the number to always round up to the nearest multiple of 10, so that 67-->70 and 64-->70. Here is my code for rounding so far:
##ROUNDING SumOfUsrinput TO NEAREST 10##
SumOfUsrinput=int(input("Please enter the sum: "))
SumRounded=round(SumOfUsrinput,-1)
print (SumRounded)
I would appreciate it if you could answer simple and explain how it works.
One way of rounding up would be to use integer division to go down to the precision you want and then multiplying back up. e.g.,:
Sumrounded = SumOfusrinput // (-10) * (-10)
This question already has answers here:
I need to convert the interest rate to a decimal value
(1 answer)
Why does the division get rounded to an integer? [duplicate]
(13 answers)
Closed 6 years ago.
I am trying to get a float value but it keeps on giving me a decimal value as an answer.
import math
p = int(raw_input("Please enter deposit amount: \n"))
r = int(raw_input("Please input interest rate: \n"))
t = int(raw_input("Please insert number of years of the investment: \n"))
interest = raw_input("Do you want a simple or compound interest ? \n")
A = p*(1+r*t)
B = p*(1+r)^t
if interest == "simple":
print (float(A/100))
else:
print(float(B/100))
float(A/100) first calculates A/100, which are both ints, so the result is an int, and only then converts to float. Instead you could use:
float(A)/100
or:
A/100.
Here is the problem. In python2 the division between integers gives another integer (this is not true in python3).
42 / 100 # return 0
The solution is to keep one to float.
42 / 100.0 #return 0.42
This question already has answers here:
Rounding a number in Python but keeping ending zeros
(6 answers)
Closed 6 years ago.
So let's say I have this code:
num = 1.29283
round(num, 2)
That rounds to 1.29, but if I do this:
num = 1.30293
round(num, 2)
That rounds to 1.3. I want to know if there is a way to have it round to 1.30; I know it is the same number, but I need it to print 1.30.
You can use string formatting for this. A number in python does not have such a thing as trailing zeros. So your question only make sense for strings.
Example:
>>> num = 1.30293
>>> "{:.2f}".format(num)
'1.30'
The .2f says that this is a float (f) and that you want two digits after the point .2. Read more about string formatting here