{"id":208326,"date":"2025-07-08T11:00:13","date_gmt":"2025-07-08T03:00:13","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208326\/"},"modified":"2025-07-08T11:00:13","modified_gmt":"2025-07-08T03:00:13","slug":"%e4%bd%bf%e7%94%a8%e7%9b%b8%e5%90%8c%e6%b5%ae%e7%82%b9%e5%b8%b8%e9%87%8f%e5%80%bc%e7%9a%84%e4%b8%8d%e5%90%8c%e6%a8%a1%e5%bc%8f%e4%bc%9a%e5%af%bc%e8%87%b4%e4%b8%8d%e5%90%8c%e7%9a%84%e7%bb%93%e6%9e%9c","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208326\/","title":{"rendered":"\u4f7f\u7528\u76f8\u540c\u6d6e\u70b9\u5e38\u91cf\u503c\u7684\u4e0d\u540c\u6a21\u5f0f\u4f1a\u5bfc\u81f4\u4e0d\u540c\u7684\u7ed3\u679c"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u4f7f\u7528\u76f8\u540c\u6d6e\u70b9\u5e38\u91cf\u503c\u7684\u4e0d\u540c\u6a21\u5f0f\u4f1a\u5bfc\u81f4\u4e0d\u540c\u7684\u7ed3\u679c<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-05-01 20:15:35<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p><span style=\"font-size: 15px\">\u5b66\u4e60\u77e5\u8bc6\u8981\u5584\u4e8e\u601d\u8003\uff0c\u601d\u8003\uff0c\u518d\u601d\u8003\uff01\u4eca\u5929\u5c0f\u7f16\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4f7f\u7528\u76f8\u540c\u6d6e\u70b9\u5e38\u91cf\u503c\u7684\u4e0d\u540c\u6a21\u5f0f\u4f1a\u5bfc\u81f4\u4e0d\u540c\u7684\u7ed3\u679c\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u6216\u51c6\u5907\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\uff0c\u5c31\u90fd\u4e0d\u8981\u9519\u8fc7\u672c\u6587\u5566~\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff0c\u80fd\u5e2e\u52a9\u5230\u4f60\u5c31\u66f4\u597d\u4e86\uff01<\/span><\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5728\u4e0b\u9762\u7684 go \u4ee3\u7801\u7247\u6bb5\u4e2d\uff0c\u6211\u5f88\u96be\u7406\u89e3\u4e3a\u4ec0\u4e48\u7ed3\u679c\u4e0d\u540c\uff1a<\/p>\n<pre>func main() {\n    a := -0.2; b := -0.1; \n    fmt.Println(a+b)\n    \/\/Outputs expected float value with rounding error  : -0.30000000000000004\n    c := (-0.2)+(-0.1)\n    fmt.Println(c)\n    \/\/Will ouput -0.3 (the actual exact constant).\n}<\/pre>\n<p>\u5230\u5e95\u53d1\u751f\u4e86\u4ec0\u4e48\uff0c\u5f53\u8fd9\u4e9b\u5e38\u91cf\u4e0d\u7528\u4e8e\u5b9e\u4f8b\u5316\u6d6e\u70b9\u6570\u65f6\uff0cgo \u662f\u5426\u4ee5\u67d0\u79cd\u65b9\u5f0f\u5c06 c \u64cd\u4f5c\u4f5c\u4e3a\u5e38\u91cf\u800c\u4e0d\u662f float64 \u64cd\u4f5c\u6267\u884c\uff1f \u5b8c\u6574\u5de5\u4f5c\u7248\u672c\uff1ahttps:\/\/play.golang.org\/p\/kuicdgfimvf<\/p>\n<p>\u5982\u6709\u4efb\u4f55\u89c1\u89e3\uff0c\u6211\u4eec\u5c06\u4e0d\u80dc\u611f\u6fc0\uff0c\u8c22\u8c22\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u6211\u5c1d\u8bd5\u4f7f\u7528java\uff0c\u7ed3\u679c\u662f\uff1a<\/p>\n<pre>public class StringTest {\n    public static void main(String[] args) {\n\n        double a = -0.2;\n        double b = -0.1;\n        System.out.println(a + b);\n       \/\/  -0.30000000000000004\n\n        double c = a + b;\n        System.out.println(c);\n      \/\/ -0.30000000000000004\n\n    }\n}\n<\/pre>\n<p>\u4f3c\u4e4e\u4efb\u4f55\u4f7f\u7528\u4e8c\u8fdb\u5236\u6d6e\u70b9\u6570\u7684\u7f16\u7a0b\u8bed\u8a00\u90fd\u4f1a\u6709\u8fd9\u4e2a\u95ee\u9898\u3002\u67d0\u4e9b\u8bed\u8a00\u7684 number \u7c7b\u578b\u4f7f\u7528 ieee754 \u6807\u51c6\u6765\u8868\u793a\u6570\u5b57\u3002 \u800cieee-745 float\u662f\u4ec0\u4e48\uff0c\u53ef\u4ee5\u770b\u5230\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u4f7f\u7528\u76f8\u540c\u6d6e\u70b9\u5e38\u91cf\u503c\u7684\u4e0d\u540c\u6a21\u5f0f\u4f1a\u5bfc\u81f4\u4e0d\u540c\u7684\u7ed3\u679c\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-208326","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208326","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=208326"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208326\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}