unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Math, XPtest, end_uses; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private 錾 } public { Public 錾 } end; var Form1: TForm1; procedure testRoundOff; procedure testCeil; procedure testFloor; procedure testTrunc; procedure testDigit; implementation uses MathUnit; {$R *.dfm} procedure testRoundOff; begin Check(True, IsSame(150, RoundOffEx(145, 1),0.000001)); Check(True, IsSame(150, RoundOffEx(154, 1),0.000001)); Check(True, IsSame(140, RoundOffEx(144, 1),0.000001)); Check(True, IsSame(160, RoundOffEx(155, 1),0.000001)); Check(True, IsSame(150, RoundOffEx(150, 1),0.000001)); Check(True, IsSame( 10, RoundOffEx( 14, 1),0.000001)); Check(True, IsSame( 10, RoundOffEx( 5, 1),0.000001)); Check(True, IsSame( 0, RoundOffEx( 4, 1),0.000001)); Check(True, IsSame( 20, RoundOffEx( 15, 1),0.000001)); Check(True, IsSame( 10, RoundOffEx( 10, 1),0.000001)); Check(True, IsSame(200, RoundOffEx(150, 2),0.000001)); Check(True, IsSame(100, RoundOffEx(140, 2),0.000001)); Check(True, IsSame(100, RoundOffEx(149, 2),0.000001)); Check(True, IsSame(200, RoundOffEx(249, 2),0.000001)); Check(True, IsSame(100, RoundOffEx( 50, 2),0.000001)); Check(True, IsSame(1000, RoundOffEx(1400, 3),0.000001)); Check(True, IsSame(2000, RoundOffEx(1500, 3),0.000001)); Check(True, IsSame(2000, RoundOffEx(1600, 3),0.000001)); Check(True, IsSame(1000, RoundOffEx(1499, 3),0.000001)); Check(True, IsSame(2000, RoundOffEx(2480, 3),0.000001)); Check(True, IsSame(-150, RoundOffEx(-145, 1),0.000001)); Check(True, IsSame(-150, RoundOffEx(-154, 1),0.000001)); Check(True, IsSame(-140, RoundOffEx(-144, 1),0.000001)); Check(True, IsSame(-160, RoundOffEx(-155, 1),0.000001)); Check(True, IsSame(-150, RoundOffEx(-150, 1),0.000001)); Check(True, IsSame(- 10, RoundOffEx( -14, 1),0.000001)); Check(True, IsSame(- 10, RoundOffEx( -5, 1),0.000001)); Check(True, IsSame(- 0, RoundOffEx( -4, 1),0.000001)); Check(True, IsSame(- 20, RoundOffEx( -15, 1),0.000001)); Check(True, IsSame(- 10, RoundOffEx( -10, 1),0.000001)); Check(True, IsSame(-200, RoundOffEx(-150, 2),0.000001)); Check(True, IsSame(-100, RoundOffEx(-140, 2),0.000001)); Check(True, IsSame(-100, RoundOffEx(-149, 2),0.000001)); Check(True, IsSame(-200, RoundOffEx(-249, 2),0.000001)); Check(True, IsSame(-100, RoundOffEx( -50, 2),0.000001)); Check(True, IsSame(-1000, RoundOffEx(-1400, 3),0.000001)); Check(True, IsSame(-2000, RoundOffEx(-1500, 3),0.000001)); Check(True, IsSame(-2000, RoundOffEx(-1600, 3),0.000001)); Check(True, IsSame(-1000, RoundOffEx(-1499, 3),0.000001)); Check(True, IsSame(-2000, RoundOffEx(-2480, 3),0.000001)); Check(True, IsSame(0, RoundOffEx(0.49, -1),0.000001)); Check(True, IsSame(1, RoundOffEx(0.50, -1),0.000001)); Check(True, IsSame(1, RoundOffEx(1.49, -1),0.000001)); Check(True, IsSame(2, RoundOffEx(1.50, -1),0.000001)); Check(True, IsSame(1.0, RoundOffEx(1.04, -2),0.000001)); Check(TRue, IsSame(1.1, RoundOffEx(1.05, -2),0.000001)); Check(True, IsSame(1.0001, RoundOffEx(1.00005000, -5),0.00000001)); Check(True, IsSame(1.0000, RoundOffEx(1.00004999, -5),0.000001)); Check(True, IsSame(1.000050, RoundOffEx(1.00005000, -6),0.000001)); Check(True, IsSame(1.000050, RoundOffEx(1.00004999, -6),0.000001)); Check(True, IsSame(1.000040, RoundOffEx(1.00004499, -6),0.000001)); Check(True, IsSame(-0, RoundOffEx(-0.49, -1),0.000001)); Check(True, IsSame(-1, RoundOffEx(-0.50, -1),0.000001)); Check(True, IsSame(-1, RoundOffEx(-1.49, -1),0.000001)); Check(True, IsSame(-2, RoundOffEx(-1.50, -1),0.000001)); Check(True, IsSame(-1.0, RoundOffEx(-1.04, -2),0.000001)); Check(TRue, IsSame(-1.1, RoundOffEx(-1.05, -2),0.000001)); Check(True, IsSame(-1.0001, RoundOffEx(-1.00005000, -5),0.00000001)); Check(True, IsSame(-1.0000, RoundOffEx(-1.00004999, -5),0.000001)); Check(True, IsSame(-1.000050, RoundOffEx(-1.00005000, -6),0.000001)); Check(True, IsSame(-1.000050, RoundOffEx(-1.00004999, -6),0.000001)); Check(True, IsSame(-1.000040, RoundOffEx(-1.00004499, -6),0.000001)); end; procedure testCeil; begin Check(True, IsSame(150, CeilEx(145, 1),0.000001)); Check(True, IsSame(160, CeilEx(154, 1),0.000001)); Check(True, IsSame(150, CeilEx(144, 1),0.000001)); Check(True, IsSame(160, CeilEx(155, 1),0.000001)); Check(True, IsSame(150, CeilEx(150, 1),0.000001)); Check(True, IsSame( 20, CeilEx( 14, 1),0.000001)); Check(True, IsSame( 10, CeilEx( 5, 1),0.000001)); Check(True, IsSame( 10, CeilEx( 4, 1),0.000001)); Check(True, IsSame( 20, CeilEx( 15, 1),0.000001)); Check(True, IsSame( 10, CeilEx( 10, 1),0.000001)); Check(True, IsSame(200, CeilEx(150, 2),0.000001)); Check(True, IsSame(200, CeilEx(140, 2),0.000001)); Check(True, IsSame(200, CeilEx(149, 2),0.000001)); Check(True, IsSame(300, CeilEx(249, 2),0.000001)); Check(True, IsSame(100, CeilEx( 50, 2),0.000001)); Check(True, IsSame(2000, CeilEx(1400, 3),0.000001)); Check(True, IsSame(2000, CeilEx(1500, 3),0.000001)); Check(True, IsSame(2000, CeilEx(1600, 3),0.000001)); Check(True, IsSame(2000, CeilEx(1499, 3),0.000001)); Check(True, IsSame(3000, CeilEx(2480, 3),0.000001)); Check(True, IsSame(-140, CeilEx(-145, 1),0.000001)); Check(True, IsSame(-150, CeilEx(-154, 1),0.000001)); Check(True, IsSame(-140, CeilEx(-144, 1),0.000001)); Check(True, IsSame(-150, CeilEx(-155, 1),0.000001)); Check(True, IsSame(-150, CeilEx(-150, 1),0.000001)); Check(True, IsSame(- 10, CeilEx( -14, 1),0.000001)); Check(True, IsSame( 0, CeilEx( -5, 1),0.000001)); Check(True, IsSame( 0, CeilEx( -4, 1),0.000001)); Check(True, IsSame(- 10, CeilEx( -15, 1),0.000001)); Check(True, IsSame(- 10, CeilEx( -10, 1),0.000001)); Check(True, IsSame(-100, CeilEx(-150, 2),0.000001)); Check(True, IsSame(-100, CeilEx(-140, 2),0.000001)); Check(True, IsSame(-100, CeilEx(-149, 2),0.000001)); Check(True, IsSame(-200, CeilEx(-249, 2),0.000001)); Check(True, IsSame( 0, CeilEx( -50, 2),0.000001)); Check(True, IsSame(-1000, CeilEx(-1400, 3),0.000001)); Check(True, IsSame(-1000, CeilEx(-1500, 3),0.000001)); Check(True, IsSame(-1000, CeilEx(-1600, 3),0.000001)); Check(True, IsSame(-1000, CeilEx(-1499, 3),0.000001)); Check(True, IsSame(-2000, CeilEx(-2480, 3),0.000001)); Check(True, IsSame(1, CeilEx(0.49, -1),0.000001)); Check(True, IsSame(1, CeilEx(0.50, -1),0.000001)); Check(True, IsSame(2, CeilEx(1.49, -1),0.000001)); Check(True, IsSame(2, CeilEx(1.50, -1),0.000001)); Check(True, IsSame(1.1, CeilEx(1.04, -2),0.000001)); Check(TRue, IsSame(1.1, CeilEx(1.05, -2),0.000001)); Check(True, IsSame(1.0001, CeilEx(1.00005000, -5),0.00000001)); Check(True, IsSame(1.0001, CeilEx(1.00004999, -5),0.000001)); Check(True, IsSame(1.000050, CeilEx(1.00005000, -6),0.000001)); Check(True, IsSame(1.000050, CeilEx(1.00004999, -6),0.000001)); Check(True, IsSame(1.000050, CeilEx(1.00004499, -6),0.000001)); Check(True, IsSame(-0, CeilEx(-0.49, -1),0.000001)); Check(True, IsSame(-0, CeilEx(-0.50, -1),0.000001)); Check(True, IsSame(-1, CeilEx(-1.49, -1),0.000001)); Check(True, IsSame(-1, CeilEx(-1.50, -1),0.000001)); Check(True, IsSame(-1.0, CeilEx(-1.04, -2),0.000001)); Check(TRue, IsSame(-1.0, CeilEx(-1.05, -2),0.000001)); Check(True, IsSame(-1.0000, CeilEx(-1.00005000, -5),0.00000001)); Check(True, IsSame(-1.0000, CeilEx(-1.00004999, -5),0.000001)); Check(True, IsSame(-1.000050, CeilEx(-1.00005000, -6),0.000001)); Check(True, IsSame(-1.000040, CeilEx(-1.00004999, -6),0.000001)); Check(True, IsSame(-1.000040, CeilEx(-1.00004499, -6),0.000001)); end; procedure testFloor; begin Check(True, IsSame(140, FloorEx(145, 1),0.000001)); Check(True, IsSame(150, FloorEx(154, 1),0.000001)); Check(True, IsSame(140, FloorEx(144, 1),0.000001)); Check(True, IsSame(150, FloorEx(155, 1),0.000001)); Check(True, IsSame(150, FloorEx(150, 1),0.000001)); Check(True, IsSame( 10, FloorEx( 14, 1),0.000001)); Check(True, IsSame( 0, FloorEx( 5, 1),0.000001)); Check(True, IsSame( 0, FloorEx( 4, 1),0.000001)); Check(True, IsSame( 10, FloorEx( 15, 1),0.000001)); Check(True, IsSame( 10, FloorEx( 10, 1),0.000001)); Check(True, IsSame(100, FloorEx(150, 2),0.000001)); Check(True, IsSame(100, FloorEx(140, 2),0.000001)); Check(True, IsSame(100, FloorEx(149, 2),0.000001)); Check(True, IsSame(200, FloorEx(249, 2),0.000001)); Check(True, IsSame( 0, FloorEx( 50, 2),0.000001)); Check(True, IsSame(1000, FloorEx(1400, 3),0.000001)); Check(True, IsSame(1000, FloorEx(1500, 3),0.000001)); Check(True, IsSame(1000, FloorEx(1600, 3),0.000001)); Check(True, IsSame(1000, FloorEx(1499, 3),0.000001)); Check(True, IsSame(2000, FloorEx(2480, 3),0.000001)); Check(True, IsSame(-150, FloorEx(-145, 1),0.000001)); Check(True, IsSame(-160, FloorEx(-154, 1),0.000001)); Check(True, IsSame(-150, FloorEx(-144, 1),0.000001)); Check(True, IsSame(-160, FloorEx(-155, 1),0.000001)); Check(True, IsSame(-150, FloorEx(-150, 1),0.000001)); Check(True, IsSame(- 20, FloorEx( -14, 1),0.000001)); Check(True, IsSame(- 10, FloorEx( -5, 1),0.000001)); Check(True, IsSame(- 10, FloorEx( -4, 1),0.000001)); Check(True, IsSame(- 20, FloorEx( -15, 1),0.000001)); Check(True, IsSame(- 10, FloorEx( -10, 1),0.000001)); Check(True, IsSame(-200, FloorEx(-150, 2),0.000001)); Check(True, IsSame(-200, FloorEx(-140, 2),0.000001)); Check(True, IsSame(-200, FloorEx(-149, 2),0.000001)); Check(True, IsSame(-300, FloorEx(-249, 2),0.000001)); Check(True, IsSame(-100, FloorEx( -50, 2),0.000001)); Check(True, IsSame(-2000, FloorEx(-1400, 3),0.000001)); Check(True, IsSame(-2000, FloorEx(-1500, 3),0.000001)); Check(True, IsSame(-2000, FloorEx(-1600, 3),0.000001)); Check(True, IsSame(-2000, FloorEx(-1499, 3),0.000001)); Check(True, IsSame(-3000, FloorEx(-2480, 3),0.000001)); Check(True, IsSame(0, FloorEx(0.49, -1),0.000001)); Check(True, IsSame(0, FloorEx(0.50, -1),0.000001)); Check(True, IsSame(1, FloorEx(1.49, -1),0.000001)); Check(True, IsSame(1, FloorEx(1.50, -1),0.000001)); Check(True, IsSame(1.0, FloorEx(1.04, -2),0.000001)); Check(TRue, IsSame(1.0, FloorEx(1.05, -2),0.000001)); Check(True, IsSame(1.0000, FloorEx(1.00005000, -5),0.00000001)); Check(True, IsSame(1.0000, FloorEx(1.00004999, -5),0.000001)); Check(True, IsSame(1.000050, FloorEx(1.00005000, -6),0.000001)); Check(True, IsSame(1.000040, FloorEx(1.00004999, -6),0.000001)); Check(True, IsSame(1.000040, FloorEx(1.00004499, -6),0.000001)); Check(True, IsSame(-1, FloorEx(-0.49, -1),0.000001)); Check(True, IsSame(-1, FloorEx(-0.50, -1),0.000001)); Check(True, IsSame(-2, FloorEx(-1.49, -1),0.000001)); Check(True, IsSame(-2, FloorEx(-1.50, -1),0.000001)); Check(True, IsSame(-1.1, FloorEx(-1.04, -2),0.000001)); Check(TRue, IsSame(-1.1, FloorEx(-1.05, -2),0.000001)); Check(True, IsSame(-1.0001, FloorEx(-1.00005000, -5),0.00000001)); Check(True, IsSame(-1.0001, FloorEx(-1.00004999, -5),0.000001)); Check(True, IsSame(-1.000050, FloorEx(-1.00005000, -6),0.000001)); Check(True, IsSame(-1.000050, FloorEx(-1.00004999, -6),0.000001)); Check(True, IsSame(-1.000050, FloorEx(-1.00004499, -6),0.000001)); end; procedure testTrunc; begin Check(True, IsSame(140, TruncEx(145, 1),0.000001)); Check(True, IsSame(150, TruncEx(154, 1),0.000001)); Check(True, IsSame(140, TruncEx(144, 1),0.000001)); Check(True, IsSame(150, TruncEx(155, 1),0.000001)); Check(True, IsSame(150, TruncEx(150, 1),0.000001)); Check(True, IsSame( 10, TruncEx( 14, 1),0.000001)); Check(True, IsSame( 0, TruncEx( 5, 1),0.000001)); Check(True, IsSame( 0, TruncEx( 4, 1),0.000001)); Check(True, IsSame( 10, TruncEx( 15, 1),0.000001)); Check(True, IsSame( 10, TruncEx( 10, 1),0.000001)); Check(True, IsSame(100, TruncEx(150, 2),0.000001)); Check(True, IsSame(100, TruncEx(140, 2),0.000001)); Check(True, IsSame(100, TruncEx(149, 2),0.000001)); Check(True, IsSame(200, TruncEx(249, 2),0.000001)); Check(True, IsSame( 0, TruncEx( 50, 2),0.000001)); Check(True, IsSame(1000, TruncEx(1400, 3),0.000001)); Check(True, IsSame(1000, TruncEx(1500, 3),0.000001)); Check(True, IsSame(1000, TruncEx(1600, 3),0.000001)); Check(True, IsSame(1000, TruncEx(1499, 3),0.000001)); Check(True, IsSame(2000, TruncEx(2480, 3),0.000001)); Check(True, IsSame(-140, TruncEx(-145, 1),0.000001)); Check(True, IsSame(-150, TruncEx(-154, 1),0.000001)); Check(True, IsSame(-140, TruncEx(-144, 1),0.000001)); Check(True, IsSame(-150, TruncEx(-155, 1),0.000001)); Check(True, IsSame(-150, TruncEx(-150, 1),0.000001)); Check(True, IsSame(- 10, TruncEx( -14, 1),0.000001)); Check(True, IsSame( 0, TruncEx( -5, 1),0.000001)); Check(True, IsSame( 0, TruncEx( -4, 1),0.000001)); Check(True, IsSame(- 10, TruncEx( -15, 1),0.000001)); Check(True, IsSame(- 10, TruncEx( -10, 1),0.000001)); Check(True, IsSame(-100, TruncEx(-150, 2),0.000001)); Check(True, IsSame(-100, TruncEx(-140, 2),0.000001)); Check(True, IsSame(-100, TruncEx(-149, 2),0.000001)); Check(True, IsSame(-200, TruncEx(-249, 2),0.000001)); Check(True, IsSame( 0, TruncEx( -50, 2),0.000001)); Check(True, IsSame(-1000, TruncEx(-1400, 3),0.000001)); Check(True, IsSame(-1000, TruncEx(-1500, 3),0.000001)); Check(True, IsSame(-1000, TruncEx(-1600, 3),0.000001)); Check(True, IsSame(-1000, TruncEx(-1499, 3),0.000001)); Check(True, IsSame(-2000, TruncEx(-2480, 3),0.000001)); Check(True, IsSame(0, TruncEx(0.49, -1),0.000001)); Check(True, IsSame(0, TruncEx(0.50, -1),0.000001)); Check(True, IsSame(1, TruncEx(1.49, -1),0.000001)); Check(True, IsSame(1, TruncEx(1.50, -1),0.000001)); Check(True, IsSame(1.0, TruncEx(1.04, -2),0.000001)); Check(TRue, IsSame(1.0, TruncEx(1.05, -2),0.000001)); Check(True, IsSame(1.0000, TruncEx(1.00005000, -5),0.00000001)); Check(True, IsSame(1.0000, TruncEx(1.00004999, -5),0.000001)); Check(True, IsSame(1.000050, TruncEx(1.00005000, -6),0.000001)); Check(True, IsSame(1.000040, TruncEx(1.00004999, -6),0.000001)); Check(True, IsSame(1.000040, TruncEx(1.00004499, -6),0.000001)); Check(True, IsSame(-0, TruncEx(-0.49, -1),0.000001)); Check(True, IsSame(-0, TruncEx(-0.50, -1),0.000001)); Check(True, IsSame(-1, TruncEx(-1.49, -1),0.000001)); Check(True, IsSame(-1, TruncEx(-1.50, -1),0.000001)); Check(True, IsSame(-1.0, TruncEx(-1.04, -2),0.000001)); Check(TRue, IsSame(-1.0, TruncEx(-1.05, -2),0.000001)); Check(True, IsSame(-1.0000, TruncEx(-1.00005000, -5),0.00000001)); Check(True, IsSame(-1.0000, TruncEx(-1.00004999, -5),0.000001)); Check(True, IsSame(-1.000050, TruncEx(-1.00005000, -6),0.000001)); Check(True, IsSame(-1.000040, TruncEx(-1.00004999, -6),0.000001)); Check(True, IsSame(-1.000040, TruncEx(-1.00004499, -6),0.000001)); end; procedure testDigit; var i: Integer; begin for i := 0 to High(Integer) do begin try Check(Length(IntToStr(i)), Digit(i) , IntToStr(i)); except on E: EAssertionFailed do begin end; end; end; end; procedure TForm1.Button1Click(Sender: TObject); begin testRoundOff; testCeil; testFloor; testTrunc; // testDigit; end; end.