Skip to main content
gamedev.net
🔒 Locked

Trigonometric functions...

Started by ace muncher Apr 10, 2002 at 8:01 PM 23 replies 4.5k views
Original Post
ace muncher
ace muncher
I need trig functions. Does anyone have any info on where I can get some fast ones, or maybe you have created some yourselves. I would really appreciate it. -Ace
-Ace
Null and Void
Null and Void
What language are you using? Either C or C++? If so, they both include the functions: sin, cos, tan, asin, acos, atan, and atan2 for trigonometry.

timm
timm
Make some lookup tables if you want it fast.
GameDev Reader TODO List:1. Name my company.2. Buy domain name.3. Create web site.4. Name myself as CEO5. Create Game Engine.6. Design game.
xDS4Lx
xDS4Lx
Standard C include file is Math.h, Standard C++ include file is cmath, just be sure to do a using std::cin..... and so forth or a using namespace std directive if using the C++ file b/c they wont be found by the compiler if you dont.
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
Cone83
Cone83
The functions of math.h are already extremely fast. I replaced them once in my game with lookup tables (my game makes heavy use of the trigonometric functions), but I couldn''t gain any perfomance (or at leas I couldn''t meassure the small difference).

I think this depends on the system you''re programming for. Back in the 80x468 times most of the games used lookup tables.

The problem is: Lookup tables arent very exact.
Jesper T
Jesper T
If you include math.h or cmath you can also use sinf/cosf/tanf which are faster than the sin/cos/tan functions (the use float instead of double).


(There is no border around this image)
davepermen
davepermen
#define sinf(x) (float)sin((double)x)

you really think they coded it twice?
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud
ace muncher
ace muncher
Thanks everyone. I looked into one of the given sites, and found what an Anonymous person said. However, I have another problem.

They gave me files with the extension .asm and .inc, and I have no clue as to how to use them. Does anyone know how to apply them to MSVC++ 98?


-Ace
-Ace
Fruny
Fruny
These are assembly language files. You''ll have to adapt them by hand to the inline assembler in VC. Read the (sparse) doc and good luck.

[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI''s STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]

Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
ace muncher
ace muncher
Thanks. I am not very familiar with that type of coding. I will give it my best shot though.


-Ace
-Ace
jenova
jenova
or you can use "masm" and link the COFF ".obj" files to your project.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
Beer Hunter
Beer Hunter
In an older thread, we came up with this:

  
float sine(float angle) {
float a2 = angle * angle;
float a3 = angle * a2;
float a5 = a2 * a3;
float a7 = a2 * a5;
float a9 = a2 * a7;
float a11 = a2 * a9;

#ifdef DEBUG

if(angle < -3.1415926535f || angle > 3.1415926535f) {
fprintf(stderr, "Please clip the input to (-pi..pi), you lazy, deluded fool.\n");
}
#endif


return angle-a3/6.0f+a5/120.0f-a7/5040.0f+a9/362880.0f-a11/39916800.0f;
}

float cosine(float angle) {
float a2 = angle * angle;
float a4 = a2 * a2;
float a6 = a2 * a4;
float a8 = a2 * a6;
float a10 = a2 * a8;

#ifdef DEBUG

if(angle < -3.1415926535f || angle > 3.1415926535f) {
fprintf(stderr, "Please clip the input to (-pi..pi), you lazy, deluded fool.\n");
}
#endif


return 1-a2/2.0f+a4/24.0f-a6/720.0f+a8/40320.0f-a10/3628800.0f;
}

They''re pretty fast, and accurate enough most of the time. If you need more accuracy, I suggest you stick with sin() and cos().
Beer Hunter
Beer Hunter
Oluseyi, no, that would be a waste of an FADD and an FMUL

Besides, cosine(pi) == -1.081891 isn''t likely to be good enough.
Jesper T
Jesper T
quote:

return 1-a2/2.0f+a4/24.0f-a6/720.0f+a8/40320.0f-a10/3628800.0f;



wouldnt this be faster:

return 1-a2*0.5f+a4*0.416667f-a6*0.0013889f+a8*0.0000248016f-a10*0.0000002756.0f;

?

Anyways, to really make it fast one should use superconverging rows instead of that one.


(There is no border around this image)
Beer Hunter
Beer Hunter
If your compiler doesn''t make that optimization, get a new compiler

I haven''t heard of "superconverging rows". I''ll look that up.
Jesper T
Jesper T
Superconverging was something my math lecturer told me about, he said that it was prefered to the normal "sum( n=0 to n=infinity, (f(n)(0) * x^n) / n! ))" -way of deriving rows for those functions since they converge a lot sooner. When I think of it it might have been hyperconverging he said but I think it was superconverging.


(There is no border around this image)
Beer Hunter
Beer Hunter
I''ve STFW''ed for those terms, and found nothing. Do you have any information?
Jesper T
Jesper T
No, not really. My lecturer didnt know either, he just knew that it existed, but I''ll ask where he heard about it.


(There is no border around this image)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.