🔒 Locked
Trigonometric functions...
Advertisement
Original Post
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
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.


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.
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.
#define sinf(x) (float)sin((double)x)
you really think they coded it twice?
you really think they coded it twice?
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
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
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...
[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...
Thanks. I am not very familiar with that type of coding. I will give it my best shot though.
-Ace
-Ace
In an older thread, we came up with this:
They''re pretty fast, and accurate enough most of the time. If you need more accuracy, I suggest you stick with sin() and cos().
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().
Couldn''t cosine be implemented as
?
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions | Internet Acronyms ]
Thanks to Kylotan for the idea!
inline float cosine( float angle )
{
return sine( angle + PI/2 );
}
?
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions | Internet Acronyms ]
Thanks to Kylotan for the idea!
But it''s inlined, so it''s expanded in place. Details, details...
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions | Internet Acronyms ]
Thanks to Kylotan for the idea!
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions | Internet Acronyms ]
Thanks to Kylotan for the idea!
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.
Besides, cosine(pi) == -1.081891 isn''t likely to be good enough.
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)
If your compiler doesn''t make that optimization, get a new compiler 
I haven''t heard of "superconverging rows". I''ll look that up.
I haven''t heard of "superconverging rows". I''ll look that up.
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)

(There is no border around this image)
I''ve STFW''ed for those terms, and found nothing. Do you have any information?
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement