看板 Python 關於我們 聯絡資訊
各位版上大大早上好。 我在float的加減運算遇到了一些問題。不囉嗦直接看Code跟結果。 Code for i in range(0,len(input_list),4): #將input_list 以四個為一組作分組運算 if input_list[i+1] != 0: #如果中心值不為0,就進行上下限的計算。 Upper=float(input_list[i+1])+float(input_list[i+2])#上限 Lower=float(input_list[i+1])-float(input_list[i+3])#下限 input_list列印結果如下 中心值 上限 下限 ['外徑', '4.1', '0.1', '0.1', '內徑', '', '', '', '沖孔', '', '', '', '高度', '', '', '', '板厚', '', '', ''] 這個List的內容是從Entrybox跟Label get來的。 執行結果 select * from ItemSpec where (外徑 <= 4.199999999999999 and 外徑 >= 3.9999999999999996) 我的問題是,我預期得到的結果是4.2跟4.0。 這個進到sqlit比對應該會有不一樣的結果 請問這是出了什麼問題呢?還是說我應該要怎麼解決? 煩請高手指導,感恩。 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.33.116.8 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1594949545.A.2E9.html ※ 編輯: yimean (114.33.116.8 臺灣), 07/17/2020 09:33:42 ※ 編輯: yimean (114.33.116.8 臺灣), 07/17/2020 09:34:04
OrzOGC: 自己取值,上面有篇類似的 07/17 09:35
yimean: 感謝,我最後用Dec解決問題 07/17 10:36
s860134: 這叫浮點數 07/17 20:39
yimean: 是的我用10進位浮點樹解決問題,感謝指導。 07/18 08:33
germun: 萬年問題 google 浮點數精確度 07/18 12:35