How python calculate this division? [duplicate] - python

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

Related

Integer division problems [duplicate]

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.

How to round a value up WITH ndigits [duplicate]

This question already has answers here:
Python round up integer to next hundred
(10 answers)
Closed 2 years ago.
The built-in function round() will round a value down but I want to know how to round a value up.
i know that this is possible with math.ceil() but the thing is that round() has the keyword argument "ndigits" and math.ceil() doesn't. so for example:
>>> round(1024, ndigits=-3)
1000
but i want 1100.
Is there a solution for this?
import math
def round(number, n):
return math.ceil(number * math.pow(10, n+1))*(math.pow(10,-(n+1)))
print(round(1024, -3))
# 1100.0
A simple function like this would suffice, multiply by 10^(n+1), find the ceiling of that number, then multiply by 10^-n-1 (equivalent to dividing by 10^n+1).

Why round(4.5) == 4 and round(5.5) == 6 in Python 3.5? [duplicate]

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.

Why does Python 3.4 give the wrong answer for division of large numbers, and how can I test for divisibility? [duplicate]

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

Remove decimal places to certain digits without rounding [duplicate]

This question already has answers here:
How to truncate float values?
(31 answers)
Closed 9 years ago.
I've found dozen of answers, but non of them is what I'm looking for, I don't want to round up or down, I know that I can round numbers as follow:
>>> print('%.3f' % 15.555555)
15.556
>>> round(15.555555, 3)
15.666
But I need to get 15.555. Should I use regex?
Cheeky solution:
numstring = str(15.555555)
num = float(numstring[:numstring.find('.')+4])
My solution involving int abuse. int rounds towards the nearest 0. I multiply it by 10**3 to affect rounding. After using int, I divide it by 10**3 to get actual results.
It's safer, as it does work with e notation.
int(15.55555 * 10**3) / 10.0**3

Categories