{"id":298,"date":"2009-04-06T13:34:00","date_gmt":"2009-04-06T05:34:00","guid":{"rendered":""},"modified":"2013-11-17T19:57:54","modified_gmt":"2013-11-17T11:57:54","slug":"math-%e4%b8%ad%e5%b8%b8%e7%94%a8%e7%9a%84%e6%95%b0%e5%ad%a6%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/298.html","title":{"rendered":"Math \u4e2d\u5e38\u7528\u7684\u6570\u5b66\u51fd\u6570"},"content":{"rendered":"<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n\/\/\u6574\u9664\u4e0e\u4f59\u6570: DivMod\r\nconst\r\na = 11;\r\nb = 3;\r\nvar\r\nx,y: Word;\r\nbegin\r\nShowMessage(IntToStr(a div b)); {\u6574\u9664\u5f97 3}\r\nShowMessage(IntToStr(a mod b)); {\u4f59\u6570 2}\r\n\r\nDivMod(a,b,x,y);\r\nShowMessage(IntToStr(x)); {3}\r\nShowMessage(IntToStr(y)); {2}\r\nend;\r\n\r\n\/\/\u8fd4\u56de\u6574\u6570: Ceil\u3001Floor\r\nvar\r\nd1,d2: Real;\r\nconst\r\nd = 1.2;\r\nbegin\r\n{\u5411\u5927\u8865\u5165}\r\nd1 := Ceil(d);\r\nd2 := Ceil(-d);\r\nShowMessage(FloatToStr(d1)); {2}\r\nShowMessage(FloatToStr(d2)); {-1}\r\n\r\n{\u5411\u5c0f\u820d\u5165}\r\nd1 := Floor(d);\r\nd2 := Floor(-d);\r\nShowMessage(FloatToStr(d1)); {1}\r\nShowMessage(FloatToStr(d2)); {-2}\r\nend;\r\n\r\n\/\/\u6700\u5927\u503c\u4e0e\u6700\u5c0f\u503c: Max\u3001Min\r\nvar\r\nd: Real;\r\nbegin\r\nd := Max(5.2, -2);\r\nShowMessage(FloatToStr(d)); {5.2}\r\n\r\nd := Min(5.2, -2);\r\nShowMessage(FloatToStr(d)); {-2}\r\nend;\r\n\r\n\/\/Double \u6570\u7ec4\u6c42\u548c: Sum\r\nvar\r\nd: Real;\r\nconst\r\narr: array&#x5B;0..3] of Double = (1, 2, 3, -4.5);\r\nbegin\r\nd := Sum(arr); {\u8981\u6c42\u5fc5\u987b\u662f Double \u6570\u7ec4}\r\nShowMessage(FloatToStr(d)); {1.5}\r\nend;\r\n\r\n\/\/Double \u6570\u7ec4\u6c42\u5e73\u5747\u503c: Mean\r\nvar\r\narr: array&#x5B;0..5] of Double;\r\nd: Double;\r\nbegin\r\narr&#x5B;0] := -1.2;\r\narr&#x5B;1] := 1.0;\r\narr&#x5B;2] := 2.5;\r\narr&#x5B;3] := 3.0;\r\narr&#x5B;4] := 4.5;\r\narr&#x5B;5] := 25.0;\r\n\r\nd := Mean(arr);\r\nShowMessage(FloatToStr(d)); {5.8}\r\nend;\r\n\r\n\/\/\u8ba1\u7b97\u4e00\u4e2a\u6570\u7684 n \u6b21\u65b9: Power\r\nvar\r\nd: Real;\r\nbegin\r\nd := Power(3,2);            {3 \u7684 2 \u6b21\u65b9}\r\nShowMessage(FloatToStr(d)); {9}\r\nend;\r\n\r\n\/\/\u5224\u65ad\u5728\u4e00\u4e2a\u8bef\u5dee\u8303\u56f4\u5185, \u4e00\u4e2a\u6570\u5b57\u662f\u5426\u4e3a0: IsZero\r\nconst\r\nd1 = 0.4;\r\nd2 = -0.1;\r\nd3 = 3;\r\nvar\r\nb: Boolean;\r\nbegin\r\nb := IsZero(d1, 0.4);      {\u53c2\u65702\u662f\u8bef\u5dee\u8303\u56f4}\r\nShowMessage(BoolToStr(b)); {True}\r\n\r\nb := IsZero(d2, 0.4);\r\nShowMessage(BoolToStr(b)); {True}\r\n\r\nb := IsZero(d3, 0.4);\r\nShowMessage(BoolToStr(b)); {False}\r\n\r\nb := IsZero(d3);           {\u53c2\u65702\u662f\u53ef\u9009\u7684, \u9ed8\u8ba4\u662f0}\r\nShowMessage(BoolToStr(b)); {False}\r\nend;\r\n\r\n\/\/\u6bd4\u8f83\u4e24\u4e2a\u53c2\u6570\u5728\u4e00\u5b9a\u8bef\u5dee\u8303\u56f4\u5185\u662f\u5426\u76f8\u7b49: SameValue\r\nvar\r\nb: Boolean;\r\nbegin\r\nb := SameValue(1.2, 1.21, 0.01); {\u8bef\u5dee\u662f 0.01}\r\nShowMessage(BoolToStr(b));       {True}\r\n\r\nb := SameValue(1.2, 1.21);       {\u8bef\u5dee\u9ed8\u8ba4\u662f 0}\r\nShowMessage(BoolToStr(b));       {False}\r\nend;\r\n\r\n\/\/\u5224\u65ad\u4e00\u4e2a\u6570\u7684\u6b63\u8d1f\r\nvar\r\nv: TValueSign; {-1..1}\r\nbegin\r\nv := Sign(22);\r\nShowMessage(IntToStr(v)); {\u8fd4\u56de 1 \u8868\u793a\u662f\u6b63\u6570}\r\n\r\nv := Sign(-22);\r\nShowMessage(IntToStr(v)); {\u8fd4\u56de -1 \u8868\u793a\u662f\u8d1f\u6570}\r\n\r\nv := Sign(0);\r\nShowMessage(IntToStr(v)); {\u8fd4\u56de 0 \u8868\u793a\u662f 0}\r\nend;\r\n\r\n\/\/\u820d\u5165\u4f4d\u6570: RoundTo\u3001SimpleRoundTo\r\nvar\r\nd: Real;\r\nconst\r\nd1 = 1234567;\r\nd2 = 1.234;\r\nd3 = 1.235;\r\nd4 = - 1.235;\r\nbegin\r\nd := RoundTo(d1,3);\r\nShowMessage(FloatToStr(d)); {123500}\r\nd := RoundTo(d2,-2);\r\nShowMessage(FloatToStr(d)); {1.23}\r\nd := RoundTo(d3,-2);\r\nShowMessage(FloatToStr(d)); {1.24}\r\nd := RoundTo(d4,-2);\r\nShowMessage(FloatToStr(d)); {-1.24}\r\n\r\nd := SimpleRoundTo(d1,3);\r\nShowMessage(FloatToStr(d)); {123500}\r\nd := SimpleRoundTo(d2,-2);\r\nShowMessage(FloatToStr(d)); {1.23}\r\nd := SimpleRoundTo(d3,-2);\r\nShowMessage(FloatToStr(d)); {1.24}\r\nd := SimpleRoundTo(d4,-2);\r\nShowMessage(FloatToStr(d)); {-1.24}\r\n\r\n{RoundTo \u4f7f\u7528\u94f6\u884c\u5bb6\u820d\u5165\u6cd5; SimpleRoundTo \u4f7f\u7528\u4e0d\u5747\u8861\u7b97\u6cd5\u820d\u5165, \u9002\u5408\u66f4\u5927\u7684\u503c}\r\nend;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/\/\u6574\u9664\u4e0e\u4f59\u6570: DivMod const a = 11; b = 3; var x,y: Word; beg [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-298","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/298","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=298"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/298\/revisions"}],"predecessor-version":[{"id":4782,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/298\/revisions\/4782"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=298"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=298"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=298"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}