This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 7 years ago.
I have the following piece of code which I run with python 2.7.9:
import math
print 0.6/0.05
print math.floor(0.6/0.05)
print int(0.6/0.05)
The output is:
12.0
11.0
11
Why is it turning my 12.0 to an 11.0 when I use floor or int? round() will work but does not suit my use case.
I have this running with 0.55,0.60,0.65,0.70... and everything works fine except for 0.6.
Any idea?
If you know the required resolution in advance, you can multiply your input numbers accordingly and then make the required calculations safely.
Let's say that the resolution is 0.01. Then:
# original inputs
n = 0.6
d = 0.05
# intermediate values
n1 = int(n*100)
d1 = int(d*100)
# integer calculation
r = n1 // d1
# or
# floating point calculation
r = float(n1) / float(d1)
Related
This question already has answers here:
Integer division in Python 2 and Python 3
(4 answers)
Closed 3 years ago.
I have defined a function in Python 2.7.15 which takes two inputs (b,v), but I have noticed that f(50,0.1) produces very different results to those of f(50.0,0.1). This is the function:
def f(b,v):
h=b*v/math.sqrt(1-v**2)
def dJ_dp(J,p):
return [J[1],-J[0]+3.0/2*J[0]**2+1/(2*h**2)]
J0 = [0.0000001,1/b]
ps = np.linspace(0,15,50)
Js = odeint(dJ_dp, J0, ps)
us = Js[:,0]
return (ps,1/us)
I've needed to define dJ_dp inside f(b,v) because it needs the value h. Why are the outputs so different? Why are they different at all?
My hypotheses was that something went wrong when defining h but that doesn't seem to be the case.
The problem is probably here: J0 = [0.0000001,1/b].
If b is the int 50, 1/b will be done using integer division, and result in 0. If b is the floating point 50.0 it will be done with floating point division and will result in 0.02.
You could use 1.0 instead of 1 to force floating point arithmetic:
J0 = [0.0000001, 1.0/b]
# Here -----------^
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:
round() doesn't seem to be rounding properly
(20 answers)
Closed 6 years ago.
Consider:
def välgu_kaugus(aeg):
kiirus = 300 / 1000
valem = aeg * kiirus
return valem
print(välgu_kaugus(float(input("Mitu sekundid kulus välgu nägemiseks müristamise kuulmiseni? "))))
This is my little shitty program. When I input 15 it gives me 4.5, but I want it to round 4.5 to 5, but using the round command it rounds my 4.5 to 4 for some reason. How can I make it to round to 5?
Use round(). For example:
>>> round(4.5) # Your number, rounded to ceil value
5.0
>>> round(4.3) # rounded to floor value
4.0
>>> round(4.7) # rounded to ceil value
5.0
Solution to your problem is to use ceiling.
import math
print math.ceil(4.5) // 5
Here are some references on how to round a number in python:
https://www.tutorialspoint.com/python/number_round.htm
How do you round UP a number in Python?
This question already has answers here:
Round to nearest 0.05 using Python
(9 answers)
Closed 8 years ago.
I am trying to round up the right hand side (fractional part) of a float.
I want to round it up to 5
x = 0.43
expected outcome
0.45
I can convert the float into an int and then split the string by "." and then round the right hand side of the decimal point but I dont think this is the best method available.
Is there a function available for this type of task?
Thanks
This function will give you the desired output.
def round_to(n, precision=0.5):
correction = 0.5 if n >= 0 else -0.5
return int(n/precision+correction)*precision
If you call it:
>>> round_to(0.43)
0.45
I got this function from Python - Round to nearest 05