0

Python. Why error float?

a=0.005 b=5 print(b//a) Why 999.0?

21st Jan 2024, 10:44 AM
Коко Ша
Коко Ша - avatar
10 ответов
+ 6
Коко Ша , The floor division operator // produces a float if either operand is a float or both operands are floats. The only time it produces int is if both operands are int. Don't believe the comments in that part of the Introduction to Python course that make the wrong assumption that since / produces float, // must produce int. They're dead wrong, and the lesson never says that. Get the definitive truth from the source below. Scroll down to the table and read the footnotes about //. https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex
21st Jan 2024, 12:53 PM
Rain
Rain - avatar
+ 4
Коко Ша , Ah. I can't be sure, but I suspect the reason has to do with tiny binary floating point errors. I've been told by other people here that tiny floating point errors are a real problem with all binary processors and that there are libraries written for various languages to try to work around it. Probably somewhere in the chain of processor operations leading to the answer, the value happened to be something like this because of a tiny error. 999.9999999999994 That then got rounded toward negative infinity to 999.0. Here's what python.org has to say about the problem in general. https://docs.python.org/3/tutorial/floatingpoint.html#floating-point-arithmetic-issues-and-limitations
21st Jan 2024, 6:03 PM
Rain
Rain - avatar
+ 2
0.5 is a power of two (2 to the power of negative 1) and can be stored without loss. To be more precise, 0.5 has a finite number of digits in base 2, or binary number system. Even more precise, the number of digits is finite and fewer than can be stored in 32 (64) bits.
22nd Jan 2024, 8:33 AM
Gordie
Gordie - avatar
+ 2
Коко Ша , The link in my second post explained how binary is capable of perfectly expressing any fraction composed of negative powers of 2 (given enough bits). Similar to what Gordie said. Negative powers of any other primes are impossible to express in binary and can only be approximated. Take this binary floating-point number. 111.111 From the leftmost place to the rightmost place, these are the equivalent values of each in decimal. 2^(2) = 4/1 = 4 2^(1) = 2/1 = 2 2^(0) = 1/1 = 1 . 2^(-1) = 1/2 = 0.5 2^(-2) = 1/4 = 0.25 2^(-3) = 1/8 = 0.125 Add them up, and you get 7 and 7/8 or 7.875. That can be expressed perfectly in binary.
22nd Jan 2024, 10:51 AM
Rain
Rain - avatar
+ 2
Maybe this post gives you inspiration. https://www.sololearn.com/Discuss/3249016/?ref=app
22nd Jan 2024, 12:39 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
I might be wrong but i believe the question is why 5/0.005 is 999 and not 1000...
21st Jan 2024, 4:01 PM
Gordie
Gordie - avatar
0
Gordie , you are right
21st Jan 2024, 4:29 PM
Коко Ша
Коко Ша - avatar
0
Rain , I asked why answer is 999.0 Not 1000.0?
21st Jan 2024, 4:51 PM
Коко Ша
Коко Ша - avatar
0
Rain Why if a=0.5 Result will 10.0? not 9.0
22nd Jan 2024, 7:24 AM
Коко Ша
Коко Ша - avatar
0
Esta bien chica la app
23rd Jan 2024, 4:17 AM
freddy Juan
freddy Juan - avatar