Python List Internal Computation Question

Asked 2 years ago, Updated 2 years ago, 26 views

I tried to run a code that returns a list of numbers with a value as the median value, b intervals, and in the middle, I found that there were different values than I expected, such as 27.700000000000003. I think it will be easy to solve, but I'm asking you because I'm curious about why this phenomenon occurs. I'd appreciate it if you could answer!

a = 28.1
b = 0.1

li = [a-5*b, a-4*b, a-3*b, a-2*b, a-1*b, a, a+1*b, a+2*b, a+3*b, a+4*b, a+5*b]

print(li)

python

2022-09-22 18:17

1 Answers

Python says there is a limit to the accuracy of decimal division. I think the document says it's an error caused by floating point operations.

See Floating Point Arithmetic: Issues and Limitations.

https://docs.python.org/3/tutorial/floatingpoint.html#floating-point-arithmetic-issues-and-limitations


2022-09-22 18:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.