Can someone please explain this to me?
I was writing a def (program) to convert text into numbers and wanted to ensure if it was just ints, the representation was ints and if floats, floats. If a mix, it defaulted to floats. Testing it on summations produced some interesting things. As I tested more, it got stranger still.
If it was always one way of the other, maybe I could address, but it's inconsistent as far as I can see. I've heard of this being a concern and there being libs that address the desired state (decimal types), but why does this happen? This kind of thing concerns me. Should I be?
Examples below range from "yep, that makes sense" to "huh?" to "how in the ???". And these happen within numbers in close proximity. I mean when it's 5.8 vs. 6.8 and you get that delta in the result. WT???
TIA for any insights. I'm sure this is old news somewhere :)
All run from the prompt although it's the same from code. Using Python 3.8.2 Some examples:
-2 + 4.5 => 2.5 "yep, that makes sense"
-6.8 + 8 => 1.2000000000000002 "huh?"
-2+3.8 => 1.7999999999999998 "how in the ???"
-5.8+8 => 2.2
-7.8+8 => 0.20000000000000018
-8.8+8 => -0.8000000000000007
-4.8+8 => 3.2
-4-3.8+8 => 0.20000000000000018
-4+3.8 => -0.20000000000000018
-3+3.8 => 0.7999999999999998
-1+3.8 => 2.8