程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python: is the built-in function round() rounded or rounded?

編輯:Python

Reading today " Python introduction - The road to master ( Deep wisdom , 2018)" When you see round() Examples of functions , The function of this function is called rounding , I always thought round() It's round , But sometimes it's rounding , Sometimes it's rounding , If there is such a misunderstanding in numerical calculation , It is possible to accumulate small differences into large differences and get wrong results , for example :

round(0.4) # 4 House
0

round(0.5) # 5 House
0

round(0.6) # 6 Enter into
1

As can be seen from this example round() It's not really rounding , But the following example is a face rounding statement :

round(0.47) # 4 House
0

round(0.57) # 5 Enter into
1

round(0.67) # 6 Enter into
1

This example shows that sometimes it is rounded , Sometimes it's rounding , What is the reason ?

In fact, this is because the computer is a binary numerical system , It can store integers accurately , But floating point numbers cannot be stored accurately , It stores only approximate values , It is the error of this approximation that makes round


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved