#project:skill-path-projects

Hi everyone! I just finished the Reggie’s Linear Regression project but I ran into an unexpected behavior from the solution I came with.

I’m on part 2 of the project and I’m confused as to why the code I wrote doesn’t create the list

I expected?.

Here’ what I get [-10, -9.9, -9.8, -9.700000000000001, -9.600000000000001, -9.500000000000002, -9.400000000000002, -9.300000000000002 […]]

I was expecting [-10, -9.9, -9.8, -9,7, -9.6, -9.5, -9.4, […]]

Anyone understands what’s up with the decimals?

(I came up with a working solution but I would appreciate an explaination as to why this solution doesn’t work)

Thanks!!

possible_ms = [-10]
while possible_ms[-1] < 10:
possible_ms.append(possible_ms[-1] + 0.1)
print(possible_ms)

That’s a limitation of using floating point numbers (and computers doing everything in binary);

since computers use a limited amount of space to store these numbers, the decimals may not match what we expect to an infinite number of decimal places.

For example, 0.2 would require an infinite number of binary decimal places to represent properly in binary, much like the the fraction 1/3 = 0.33333… would require an infinite number of decimal places in our base-10 number system.

Also, similar computations on floating point numbers may end up being different by a few bits due to the way the computer processes those computations internally,

so `3.0 / 10`

may give you something that does not look identical to `3.0 * 0.1`

So −9.700000000000001 should be considered the same as −9.7

and 3.9999999999999998 should be considered the same as 4.0

1 Like