Issue35658
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) * ![]() |
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:09 | admin | set | github: 79839 |
| 2019-01-04 13:01:24 | steven.daprano | set | status: open -> closed nosy:
+ steven.daprano resolution: not a bug |
| 2019-01-04 12:17:38 | Bart van den Donk | create | |
