免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
DELPHI XE11.1的幾個數(shù)學取整

Delphi下的四舍五入和取整,除了截取整數(shù)Trunc()之外,都是四舍六入五留雙,即銀行家算法。但是好像又不全是這么回事兒。要以實測為準。

例如3.15和3.25,修約時應分別得到3.2和3.2,而不是3.2和3.3。這個算法在大學物理實驗里也是這樣。但是也偶有爭議,說是不同的Delphi版本編譯器怎樣怎樣。我比較了Delphi7和Delphi XE11.1,結(jié)果是一樣的。Round()在四舍六入,而SimpleRoundTo()仍然在四舍五入。

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;


implementation
uses math;
{$R *.dfm}

function RoundClassic(R: Real): Int64;
begin
  Result:= Trunc(R);
  if Frac(R) >= 0.5 then
    Result:= Result + 1;
end;

function RoundThief(R: Real): Int64;
begin
  R:=R+0.0000000000001;
  Result:= Trunc(R);
  if Frac(R) >= 0.5 then
    Result:= Result + 1;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  a: integer;
  b: real;
begin
  a := Trunc(0.35 * 10);
  showmessage('3.5取整數(shù)部分' + inttostr(a));
  a := round(2.5);
  showmessage('Round(2.5)四舍六入五留雙,得到2而不是3:' + inttostr(a));
b := simpleroundto(2.5, 0);
  showmessage('2.5取整,SimpleRounTo又開始傳統(tǒng)四舍五入3:' + floattostr(b));
  b := simpleroundto(2.55, -1);
  showmessage('2.55留1位小數(shù),SimpleRounTo又開始傳統(tǒng)四舍五入2.6:' + floattostr(b));
  b := simpleroundto(2.45, -1);
  showmessage('2.45留1位小數(shù),SimpleRounTo又開始傳統(tǒng)四舍五入2.5:' + floattostr(b));
  a := ceil(123.4);
  showmessage('123.4向上取整:' + inttostr(a));
  a := ceil(-123.4);
  showmessage('-123.4向上取整:' + inttostr(a));
  a := floor(123.4);
  showmessage('123.4向下取整:' + inttostr(a));
  a := floor(-123.4);
  showmessage('-123.4向下取整:' + inttostr(a));
    a := RoundClassic(124.5);
  showmessage('124.5經(jīng)典四舍五入:' + inttostr(a));
    a := RoundThief(124.5);
  showmessage('124.5取巧四舍五入:' + inttostr(a));
  //showmessage('33.025四舍五入:' + floattostr(RoundTo(33.025,-2)));
end;

end.

本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
delphi的取整函數(shù)round、trunc、ceil和floor
X 的 Y 次方
xe unicode byte char函數(shù)與長度
Delphi讀取excel數(shù)據(jù)
Delphi指針基本知識
談談 Delphi 的類型與指針[2]
更多類似文章 >>
生活服務
分享 收藏 導長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服