blob: 5f79be4a2145b9edd9883b287e22fddb3f14a46a (
plain) (
blame)
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
|
{ Compute the cosine using the expansion:
cos(x) = 1 - x**2/(2*1) + x**4/(4*3*2*1) - ...
This file verifies a simple unit that exports one function.
}
unit MyCosineUnit;
interface
function mycosine(x : real) : real;
implementation
function mycosine(x : real) : real;
const
eps = 1e-14;
var
sx, s, t : real;
i, k : integer;
begin
t := 1;
k := 0;
s := 1;
sx := sqr(x);
while abs(t) > eps*abs(s) do
begin
k := k + 2;
t := -t * sx / (k * (k - 1));
s := s + t;
end;
mycosine := s
end; { mycosine }
end.
|