Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to use a fast sine and cosine calculations Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
30-Jun-03
Category
Algorithm
Language
Delphi 2.x
Views
147
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: John Pears

How to really speed up sine and cosine calculations

Answer:

If you have ever written applications that require many sine and  cosine 
calculations over a short time you will have realized that things really start to 
slow down. 

This is an old trick. But if you have never come across it, it really is worth 
using. 

This version uses degrees not radians. 

1   unit sin_Tool;
2   
3   interface
4   const
5     FULL_CIRCLE = 360;
6     HALF_CIRCLE = 180;
7     //  TEN_CIRCLES = 3600;
8   function MySin(x: integer): real; overload;
9   function MySin(x: real): real; overload; //  allow both reals or integers
10  
11  function MyCos(x: integer): real; overload;
12  function MyCos(x: real): real; overload; //  allow both reals or integers
13  
14  { ===================================================== }
15  { ===================================================== }
16  implementation
17  
18  uses
19    Math;
20  const
21    MULTIPLIER = 10;
22    NUM_ELEMENTS = FULL_CIRCLE * MULTIPLIER;
23  type
24    tArcAnswers = array[0..NUM_ELEMENTS] of real;
25  var
26    SinResults,
27      CosResults: tArcAnswers;
28    { =====================================================
29    function DegToRad(x:real):real; // OK... no need .. its in the math unit...
30    ===================================================== }
31  
32  procedure InitArcAnswers;
33  var
34    c: integer;
35  begin
36    for c := 0 to NUM_ELEMENTS do
37    begin
38      SinResults[c] := sin(DegToRad(c / MULTIPLIER));
39      CosResults[c] := cos(DegToRad(c / MULTIPLIER));
40    end;
41    c := 1;
42  end;
43  { ===================================================== }
44  
45  function MySin(x: integer): real; overload;
46  begin
47    while (x > FULL_CIRCLE) do
48      x := x - FULL_CIRCLE;
49    while (x < 0) do
50      x := x + FULL_CIRCLE;
51  
52    Result := SinResults[x * MULTIPLIER];
53  end;
54  
55  function MySin(x: real): real; overload;
56  begin
57    while (x > FULL_CIRCLE) do
58      x := x - FULL_CIRCLE;
59    while (x < 0) do
60      x := x + FULL_CIRCLE;
61    Result := SinResults[round(x * MULTIPLIER)];
62  end;
63  { ===================================================== }
64  
65  function MyCos(x: integer): real; overload;
66  begin
67    while (x > FULL_CIRCLE) do
68      x := x - FULL_CIRCLE;
69    while (x < 0) do
70      x := x + FULL_CIRCLE;
71    Result := CosResults[x * MULTIPLIER];
72  end;
73  
74  function MyCos(x: real): real; overload;
75  begin
76    while (x > FULL_CIRCLE) do
77      x := x - FULL_CIRCLE;
78    while (x < 0) do
79      x := x + FULL_CIRCLE;
80    Result := CosResults[round(x * MULTIPLIER)];
81  end;
82  
83  { ===================================================== }
84  { ===================================================== }
85  initialization
86    begin
87      InitArcAnswers;
88    end;
89  
90  end.



Component Download: 
http://www.delphi3000.com/article/3649/3649.zip

			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC