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
Related
This question already has answers here:
Floor division with negative number
(4 answers)
Closed 7 days ago.
Is it a bug, or there's some official document that says, that this was supposed to happen??
Because my C program worked very well.
In the Python line, you're using floor division, which rounds down. In C, division truncates the decimal values.
>>> -10/8 # standard division
-1.25
>>> -10//8 # floor division
-2
The / and // behave differently as you noticed.
With the floor division your result gets rounded to the next smallest number which is in negative numbers -2.
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:
Python 3.x rounding behavior
(13 answers)
Closed 5 years ago.
Looks like both 4.5 and 5.5 have exact float representations in Python 3.5:
>>> from decimal import Decimal
>>> Decimal(4.5)
Decimal('4.5')
>>> Decimal(5.5)
Decimal('5.5')
If this is the case, then why
>>> round(4.5)
4
>>> round(5.5)
6
?
In Python 3, exact half way numbers are rounded to the nearest even result. This behavior changed in Python 3
The round() function rounding strategy and return type have changed. Exact halfway cases are now rounded to the nearest even result instead of away from zero. (For example, round(2.5) now returns 2 rather than 3.) round(x[, n]) now delegates to x.round([n]) instead of always returning a float. It generally returns an integer when called with a single argument and a value of the same type as x when called with two arguments.
Python 3 uses Bankers Rounding, which rounds .5 values to the closest even number.
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:
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)