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.
Related
This question already has answers here:
round down to 2 decimal in python
(8 answers)
Closed 3 years ago.
I tired to round 3.666666 for two digit and get 3.66. But Round() function give me 3.67.
Is there a way to solve this problem with the round function without converting it to string type?
a=round(3.666666,2)
How about using
import math
x = math.floor(x * 100) / 100
Use math.floor() instead of round. floor rounds down, ceil rounds up, round rounds mathematically, either up or down.
To round to two characters after the decimal point, you can multiply it with 10^2 (100) before rounding and then divide it afterwards by the same number.
Here is an example:
import math
math.floor(value * 100) / 100
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:
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:
Python 3 gives wrong output when dividing two large numbers?
(3 answers)
Closed 3 years ago.
In my program, I'm using division to test if the result is an integer, I'm testing divisibility. However, I'm getting wrong answers. Here is an example:
print(int(724815896270884803/61))
gives 11882227807719424.
print(724815896270884803//61)
gives the correct result of 11882227807719423.
Why is the floating point result wrong, and how can I test whether the large number is divisible by 61? Do I really need to do integer division and then multiply it back and see if it's equal?
Instead of dividing, you should compute the modulus (%):
print(724815896270884803 % 61)
This is similar to doing an integer division and returning the remainder (think back to elementary school long division). A remainder of 0 means it is divisible.
The floating-point result is wrong because dividing two ints with / produces a float, and the exact result of your division cannot be represented exactly as a float. The exact result 11882227807719423 must be rounded to the nearest representable number:
In [1]: float(11882227807719423)
Out[1]: 1.1882227807719424e+16