This question already has answers here:
Python modulo on floats [duplicate]
(3 answers)
Closed 9 years ago.
I just want to show you the results of the operations in python. I cannot explain.
>>> 1.0%1.0
0.0 (OK)
>>> 1.0%0.1
0.09999....
>>> 1.0%0.001
0.00999....
>>> 1.0 %0.0001
0.000999...
... and so on. I need something that allows me to understand whether the remainder of 'x%y' is 0.0, namely 'y' divides 'x' exactly N times, where N is an integer.
Due to the previous behavior I don't know how to set a possible tolerance to determine if the remainder is next to 0.
Any help?
As this (long) response says, use decimal module:
>>> from decimal import Decimal
>>> Decimal('3.5') % Decimal('0.1')
Decimal('0.0')
>>> print(Decimal('3.5') % Decimal('0.1'))
0.0
>>> (Decimal(7)/2) % (Decimal(1)/10)
Decimal('0.0')
The problem is essentially due to the representation of floats in the system, you can read stuff about that everywhere on the Internet, and in the response linked.
Related
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:
Python modulo on floats [duplicate]
(3 answers)
Closed 9 years ago.
I just want to show you the results of the operations in python. I cannot explain.
>>> 1.0%1.0
0.0 (OK)
>>> 1.0%0.1
0.09999....
>>> 1.0%0.001
0.00999....
>>> 1.0 %0.0001
0.000999...
... and so on. I need something that allows me to understand whether the remainder of 'x%y' is 0.0, namely 'y' divides 'x' exactly N times, where N is an integer.
Due to the previous behavior I don't know how to set a possible tolerance to determine if the remainder is next to 0.
Any help?
As this (long) response says, use decimal module:
>>> from decimal import Decimal
>>> Decimal('3.5') % Decimal('0.1')
Decimal('0.0')
>>> print(Decimal('3.5') % Decimal('0.1'))
0.0
>>> (Decimal(7)/2) % (Decimal(1)/10)
Decimal('0.0')
The problem is essentially due to the representation of floats in the system, you can read stuff about that everywhere on the Internet, and in the response linked.
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:
How to display a float with two decimal places?
(13 answers)
Pad python floats
(4 answers)
Closed 8 years ago.
I'm sorry, I know this must be a duplicate, I can't find where else it's posted. Please feel free to link me to the original question and mark this as duplicate.
I would like to print a 3 digits of a number AFTER the decimal point in it.
For example:
number = 523.637382
I would like to print: 523.637
I have a feeling I can use something similar to this
print(str(number)[:7])
>>>523.637
However, this will not work if the number before the decimal is not 3 decimals.
Bonus points:
Would this be easy?
number = 500.220
#magic
>>>500.22
number = 500.2000003
#magic
>>>500.2
A (built-in) function that could do this is round:
>>> number = 523.637382
>>> rounded = round(number, 3) # 3 decimal places, for example
>>> rounded
523.637
This has already been answered for example here.
The good news, to answer the second part of your question, is that the round function automatically removes trailing zeroes. It's much harder to retain the zeros if you're defining a new variable: you need the decimal module; but it looks that that isn't necessary here.
>>> number = 523.60000001
>>> rounded = round(number, 3)
>>> rounded
523.6
print("%.3f" % number)
or, using the new-style formatting,
print("{0:.3f}".format(number))
If you're printing a str like above you can use string interpolation:
number = 33.33333
print("{0:.3f}".format(number))
#=> 33.333
This question already has answers here:
Most Pythonic way to print *at most* some number of decimal places [duplicate]
(3 answers)
Closed 9 years ago.
I am trying to round a floating point number in python to zero decimal places.
However, the round method is leaving a trailing 0 every time.
value = 10.01
rounded_value = round(value)
print rounded_value
results in 10.0 but I want 10
How can this be achieved? Converting to an int?
Pass the rounded value to int() to get rid of decimal digits:
>>> value = 10.01
>>> int(round(value))
10
>>> value = 10.55
>>> int(round(value))
11
10.0 and 10 are the same float value. When you print that value, you get the string 10.0, because that's the default string representation of the value. (The same string you get by calling str(10.0).)
If you want a non-default representation, you need to ask for it explicitly. For example, using the format function:
print format(rounded_value, '.0f')
Or, using the other formatting methods:
print '{:.0f}'.format(rounded_value)
print '%.0f' % (rounded_value,)
The full details for why you want '.0f' are described in the Format Specification Mini-Language, but intuitively: the f means you want fixed-point format (like 10.0 instead of, say, 1.0E2), and the .0 means you want no digits after the decimal point (like 10 instead of 10.0).
Meanwhile, if the only reason you rounded the value was for formatting… never do that. Leave the precision on the float, then trim it down in the formatting:
print format(value, '.0f')
Casting to an int would certainly be the easiest way. If you are hell-bent on keeping it a float here's how to do it courtesy of Alex Martelli:
print ('%f' % value).rstrip('0').rstrip('.')
You'll find a function number_shaver() that cuts trailing zeros of numbers in the EDIT 2 of this post.
Another post in the same thread explains how the regex in number_shaver() works.
I improved the regex in another thread some days later.