floating point arithmetic and numpy remainder function [duplicate] - python

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

How to print Decimal object with specified precision [duplicate]

This question already has answers here:
Limiting floats to two decimal points
(35 answers)
Closed last year.
I have two float values 1000.4 and 700.7
The result of 1000.4 - 700.7 returns me 299.69999999999993. I did my research,Decimal would be a good option to do the calculation here print(Decimal('1000.4') - Decimal('700.7')) and it returns 299.7
I have a question. what if I have a value 14.5 how can I print it as 14.50?
I tried getcontext().prec = 2 and it didn't help and would made the print(Decimal('1000.4') - Decimal('700.7')) returns 3.0E+2 which isn't what I want.
You can use f-strings to custom the leading zeros/limit the precision.
n=14.5
print(f"{n:.2f}")
Here, it would print only the first two decimals. (14.50).
This can be done with formatting for both floats and Decimal.
In [1]: print("{:.2f}".format(1.234))
1.23
In [2]: from decimal import Decimal
In [3]: print("{:.2f}".format(Decimal(1.234)))
1.23

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.

How python calculate this division? [duplicate]

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

How can I stop printing a float 3 spaces after decimal? [duplicate]

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

Remainder on Float in Python [duplicate]

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.

Categories