-3

I am writing a program that deals with adding floats to each other and found something weird.

Examples

---INPUT---
print(1+0.1)
print(1-0.1)
---OUTPUT---
1.10000000000001
0.99999999999999

Why is this happening and how do I stop it?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Dan Howe
  • 9
  • 10
  • Check this https://stackoverflow.com/questions/20651169/how-to-correctly-add-floating-numbers-in-python – Roy May 11 '20 at 10:42
  • @roy that does not help me at alli already looked at it before i posted – Dan Howe May 11 '20 at 10:47
  • 6
    Why don't you mention that in your question, then? *Show your research.* The canonical dupe is https://stackoverflow.com/q/588004/3001761. – jonrsharpe May 11 '20 at 10:49
  • The answer to `print(1+0.1)` is basically correct, within the limits of floating point accuracy. The answer to `print(1-0.1)` is completely wrong. Can you go back and check this, please? – r3mainer May 11 '20 at 10:50
  • @jonrsharpe thank you! reading this has led me to an answer! – Dan Howe May 11 '20 at 10:51
  • Of course it is still strange to get `1-0.1` to print `0.9999...`, but perhaps that was a copy+paste mistake (e.g, actually you computed `1.1 - 0.1`?) – chtz May 11 '20 at 10:55
  • @chtz yea i didnt copy and paste anything but i was kind of exagerating to get my point across – Dan Howe May 11 '20 at 10:57
  • 3
    Re “i didnt copy and paste anything but i was kind of exagerating to get my point across”: Presenting incorrect data in a question is not a proper technique for getting correct answers and ruins the possibility the question will serve any other readers in the future. Stack Overflow is intended to serve as a durable repository of questions and answers available to everybody, not as your personal teaching service. – Eric Postpischil May 11 '20 at 11:29
  • @EricPostpischil mate tbh i think people get the point here – Dan Howe May 11 '20 at 14:01
  • 1
    @DanHowe: We get questions and readers at all levels of knowledge. Incorrect data may be a sign the questioner has a mistaken concept, that they have misreported data, that they have misinterpreted data, or other problems. Readers presented with incorrect data may not have the knowledge required to recognize and compensate for it. If you do not hold value correctness and present incorrect data, expect to be voted down. – Eric Postpischil May 11 '20 at 15:31
  • @EricPostpischil 1) It was closed because there was a duplicate, if it really matters to them theyll see that one first. 2) The value they get will be weird and thats hat this post demonstrates, it doesnt matter what the actual value was. – Dan Howe May 11 '20 at 16:19
  • @DanHowe The point is that some "weird" results are due to floating point rounding error, a well understood and studied issue. Other results are not due to rounding, and need to be analyzed to find the actual cause. The value of the output does matter, because it helps distinguish those cases. Your second output, if it were accurately reported, would be an indication of a bug that needs to be analyzed. – Patricia Shanahan May 11 '20 at 17:17

1 Answers1

-2

To do this instead of using floats, I used a list where each value of the list represented by each part of the float that i need. This is not always the best soluton but for the wider application that I need it in this is the best solution!

e.g.

10 1 0.1 0.01
[5, 4, 0, 1]
=> 54.01
Dan Howe
  • 9
  • 10