Python 1.9 - 1 ทำไมได้ 0.89999999999 ครับ??? แล้วแก้ยังไง

ตามเรื่องเลยครับพอดีฟึกเขียน Pyhton แล้ว ให้มัน คำนวน เลข คือ 1.9-1 แล้วมันก็ออกมาเป็น 0.899999999
มันมีวิธีแก้ยัไงครับ
คำตอบที่ได้รับเลือกจากเจ้าของกระทู้
ความคิดเห็นที่ 4
-  ใช้ Decimal
-  ใช้ round
https://paiza.io/projects/tNYryTQLkRQKommceP0qhQ

How To Stop Floating Point Arithmetic Errors in Python
https://medium.com/code-85/how-to-stop-floating-point-arithmetic-errors-in-python-a98d3a63ccc8

สาเหตุคือ binary floating point ไม่สามารถแสดง decimal floaint point ได้แบบตรง ๆ ในบางค่า
บางภาษาจึงเลือกที่จะปัดให้เหลือหลักที่ 17  ...ซึ่งรวมถึง python รุ่นเก่า ๆ ด้วย

https://docs.python.org/3/tutorial/floatingpoint.html
Instead of displaying the full decimal value, many languages (including older versions of Python), round the result to 17 significant digits

ลืมบอกไปว่า python ถือตัวเลขทศนิยมเป็น double precision floating point    [Spoil] คลิกเพื่อดูข้อความที่ซ่อนไว้
แสดงความคิดเห็น
โปรดศึกษาและยอมรับนโยบายข้อมูลส่วนบุคคลก่อนเริ่มใช้งาน อ่านเพิ่มเติมได้ที่นี่