-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMathUtils.pas
91 lines (75 loc) · 2 KB
/
MathUtils.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
unit MathUtils;
{$mode objfpc}{$H+}
interface
function isInteger(x : Extended) : Boolean;
function fmod(x, y : Extended) : Extended;
function fdiv(x, y : Extended) : Extended;
function ftrunc(x : Extended) : Extended;
function ffrac(x : Extended) : Extended;
function fround(x : Extended) : Extended;
function ffloor(x : Extended) : Extended;
function fceiling(x : Extended) : Extended;
implementation
uses Math;
function isInteger(x : Extended) : Boolean;
begin
Result := (x = int(x));
end;
function fmod(x,y:Extended):Extended;
begin
Result := x - y * Int(x/y);
end;
function fdiv(x, y : Extended) : Extended;
begin
Result := Int(x/y);
end;
function ftrunc(x : Extended) : Extended;
begin
//{$IFDEF cpu32} writeln( 'cpu32' );
if x <= High(LongInt)
then Result := trunc(x)
else Result := fdiv(x,1);
//{$ENDIF}
end;
function ffrac(x : Extended) : Extended;
begin
//{$IFDEF cpu32} writeln( 'cpu32' );
if x <= High(LongInt)
then Result := frac(x)
else Result := fmod(x,1);
//{$ENDIF}
end;
function fround(x : Extended) : Extended;
begin
if abs(x) <= High(LongInt)
then
//Result := Trunc(x+0.5)
if (x <= 0)
then Result := Trunc(x-0.5)
else Result := Trunc(x+0.5)
else
if (x <= 0)
then Result := fdiv(x-0.5,1)
else Result := fdiv(x+0.5,1);
end;
function ffloor(x : Extended) : Extended;
begin
if abs(x) <= High(LongInt)
then Result := Floor(x)
else if (isInteger(x))
then Result := fdiv(x,1)
else if (x < 0)
then Result := fdiv(x,1)-1
else Result := fdiv(x,1);
end;
function fceiling(x : Extended) : Extended;
begin
if abs(x) <= High(LongInt)
then Result := Ceil(x)
else if (isInteger(x))
then Result := fdiv(x,1)
else if (x < 0)
then Result := fdiv(x,1)
else Result := fdiv(x,1)+1;
end;
end.