Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
500 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (0)


fast sine and cosine calculationsGo to John Pears's websiteComponent available for this articleFormat this article printer-friendly!Bookmark function is only available for registered users!
How to really speed up sine and cosine calculations
Product:
Delphi all versions
Category:
Algorithm
Skill Level:
Scoring:
Last Update:
05/19/2003
Search Keys:
delphi delphi3000 article borland vcl code-snippet sine cosine fast
Times Scored:
4
Visits:
2687
Uploader: John Pears
Company: coventry Uni ( a technician )
Reference: N/A
Component Download: http://www.delphi3000.com/article/3649/3649.zip
 
Question/Problem/Abstract:
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.


unit sin_Tool;

interface
const
  FULL_CIRCLE = 360;
  HALF_CIRCLE = 180;
//  TEN_CIRCLES = 3600;
function MySin(x:integer):real; overload;
function MySin(x:real):real; overload; //  allow both reals or integers

function MyCos(x:integer):real; overload;
function MyCos(x:real):real; overload;  //  allow both reals or integers

{ ===================================================== }
{ ===================================================== }
implementation

uses
   Math;
const
  MULTIPLIER = 10;
  NUM_ELEMENTS = FULL_CIRCLE * MULTIPLIER;
type
   tArcAnswers = array[0..NUM_ELEMENTS] of real;
var
   SinResults,
   CosResults:tArcAnswers;
{ =====================================================
function DegToRad(x:real):real; // OK... no need .. its in the math unit...
===================================================== }
procedure InitArcAnswers;
var
  c:integer;
begin
  for c := 0 to NUM_ELEMENTS do
  begin
    SinResults[c] := sin(DegToRad(c/ MULTIPLIER));
    CosResults[c] := cos(DegToRad(c/ MULTIPLIER));
  end;
  c := 1;
end;
{ ===================================================== }

function MySin(x:integer):real; overload;
begin
  while ( x > FULL_CIRCLE ) do
      x := x - FULL_CIRCLE;
  while ( x < 0 ) do
      x := x + FULL_CIRCLE;

  Result := SinResults[x * MULTIPLIER];
end;

function MySin(x:real):real; overload;
begin
  while ( x > FULL_CIRCLE ) do
      x := x - FULL_CIRCLE;
  while ( x < 0 ) do
      x := x + FULL_CIRCLE;
  Result := SinResults[round(x * MULTIPLIER)];
end;
{ ===================================================== }
function MyCos(x:integer):real; overload;
begin
  while ( x > FULL_CIRCLE ) do
      x := x - FULL_CIRCLE;
  while ( x < 0 ) do
      x := x + FULL_CIRCLE;
  Result := CosResults[x * MULTIPLIER];
end;

function MyCos(x:real):real; overload;
begin
  while ( x > FULL_CIRCLE ) do
      x := x - FULL_CIRCLE;
  while ( x < 0 ) do
      x := x + FULL_CIRCLE;
  Result := CosResults[round(x * MULTIPLIER)];
end;


{ ===================================================== }
{ ===================================================== }
initialization
begin
  InitArcAnswers;
end;


end.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment













 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
S. Carter
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)