This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Reggie_Linear_Regression_Solution.ipynb devide by 10 diff with multiply by .1
Type: behavior Stage: resolved
Components: Demos and Tools, Regular Expressions Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Bart van den Donk, ezio.melotti, mrabarnett, steven.daprano
Priority: normal Keywords:

Created on 2019-01-04 12:17 by Bart van den Donk, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg332973 - (view) Author: Bart van den Donk (Bart van den Donk) Date: 2019-01-04 12:17
possible_ms1 = [i*.1 for i in range(-100, 101, 1)] #your list comprehension here
print(possible_ms1)
possible_ms2 = [i/10 for i in range(-100, 101, 1)] #your list comprehension here
print(possible_ms2)

Multiply by .1 gives dirty results.
Divide by 10 gives clean results.
msg332975 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2019-01-04 13:01
Not a bug. 0.1 is a binary floating point value, please read the FAQs:

https://docs.python.org/3/faq/design.html#why-are-floating-point-calculations-so-inaccurate

1/10 = 0.1 in decimal cannot be represented *exactly* in binary floating point, so when you type 0.1 as a float, the actual value you get is the closest number you can get using 53 bits for the significant digits, 8 bits for the exponent and 1 bit for the sign (plus or minus). That is approximately 0.1000000000000000056 or so.

Unfortunately you cannot get any closer to 1/10 in binary floating point numbers, for the same reason you cannot get 1/3 exactly in decimal.

See also 

https://stackoverflow.com/questions/8215437/floating-point-accuracy-in-python

https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate

https://stackoverflow.com/questions/1089018/why-cant-decimal-numbers-be-represented-exactly-in-binary?noredirect=1&lq=1
History
Date User Action Args
2022-04-11 14:59:09adminsetgithub: 79839
2019-01-04 13:01:24steven.dapranosetstatus: open -> closed

nosy: + steven.daprano
messages: + msg332975

resolution: not a bug
stage: resolved
2019-01-04 12:17:38Bart van den Donkcreate