1 /*! ========================================================================
2 ** Extended Template and Library Test Suite
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
7 ** Copyright (c) 2008 Chris Moore
9 ** This package is free software; you can redistribute it and/or
10 ** modify it under the terms of the GNU General Public License as
11 ** published by the Free Software Foundation; either version 2 of
12 ** the License, or (at your option) any later version.
14 ** This package is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 ** General Public License for more details.
19 ** === N O T E S ===========================================================
21 ** ========================================================================= */
23 /* === H E A D E R S ======================================================= */
27 #include <ETL/fastangle>
30 #include <ETL/hermite>
32 /* === M A C R O S ========================================================= */
39 /* === C L A S S E S ======================================================= */
41 int fastangle_test(void)
53 theta=angle::degrees(0),theta2=fastangle::degrees(0);
54 theta<=angle::degrees(360);
55 theta+=angle::degrees(10),theta2+=fastangle::degrees(10)
58 error=(float)angle::sin(theta).get() -(float)fastangle::sin(theta2).get();
60 fprintf(stderr,"angle: sin(%d)=%f ;\tfastangle: sin(%d)=%f ;\t diff: %f\n",
61 (int)angle::degrees(theta),
62 (float)angle::sin(theta),
63 (int)fastangle::degrees(theta2),
64 (float)fastangle::sin(theta2),
68 if(error > largest_error)
70 if(error < -largest_error)
75 printf("fastangle: Largest SIN error: (+/-)%f\n",largest_error);
76 if(largest_error>0.075)ret++;
85 theta=angle::degrees(0),theta2=fastangle::degrees(0);
86 theta<=angle::degrees(360);
87 theta+=angle::degrees(10),theta2+=fastangle::degrees(10)
90 error=(float)angle::cos(theta).get() -(float)fastangle::cos(theta2).get();
92 fprintf(stderr,"angle: cos(%d)=%f ;\tfastangle: cos(%d)=%f ;\t diff: %f\n",
93 (int)angle::degrees(theta),
94 (float)angle::cos(theta),
95 (int)fastangle::degrees(theta2),
96 (float)fastangle::cos(theta2),
100 if(error > largest_error)
102 if(error < -largest_error)
103 largest_error=-error;
107 printf("fastangle: Largest COS error: (+/-)%f\n",largest_error);
108 if(largest_error>0.075)ret++;
121 error=angle::radians(angle::sin(val)).get() -fastangle::radians(fastangle::sin(val)).get();
123 fprintf(stderr,"angle: asin(%f)=%frad ;\tfastangle: asin(%f)=%frad ;\t diff: %f\n",
125 (float)(angle::radians)angle::sin(val),
127 (float)(fastangle::radians)fastangle::sin(val),
131 if(error > largest_error)
133 if(error < -largest_error)
134 largest_error=-error;
138 printf("fastangle: Largest ASIN error: (+/-)%frad\n",largest_error);
139 if(largest_error>0.075)ret++;
153 error=angle::radians(angle::cos(val)).get() -fastangle::radians(fastangle::cos(val)).get();
155 fprintf(stderr,"angle: acos(%f)=%frad ;\tfastangle: acos(%f)=%frad ;\t diff: %f\n",
157 (float)(angle::radians)angle::cos(val),
159 (float)(fastangle::radians)fastangle::cos(val),
163 if(error > largest_error)
165 if(error < -largest_error)
166 largest_error=-error;
170 printf("fastangle: Largest ACOS error: (+/-)%frad\n",largest_error);
171 if(largest_error>0.075)ret++;
181 theta=angle::degrees(0),theta2=fastangle::degrees(0);
182 theta<angle::degrees(360);
183 theta+=angle::degrees(38),theta2+=fastangle::degrees(38)
186 error=angle::tan(theta).get() - fastangle::tan(theta2).get();
188 fprintf(stderr,"angle: tan(%d)=%f ;\tfastangle: tan(%d)=%f ;\t diff: %f\n",
189 (int)angle::degrees(theta),
190 (float)angle::tan(theta),
191 (int)fastangle::degrees(theta2),
192 (float)fastangle::tan(theta2),
196 if(error > largest_error)
198 if(error < -largest_error)
199 largest_error=-error;
203 printf("fastangle: Largest TAN error: (+/-)%f\n",largest_error);
204 if(largest_error>0.75)ret++;
217 error=angle::radians(angle::tan(val)).get() -fastangle::radians(fastangle::tan(val)).get();
219 fprintf(stderr,"angle: atan(%f)=%frad ;\tfastangle: atan(%f)=%frad ;\t diff: %f\n",
221 (float)(angle::radians)angle::tan(val),
223 (float)(fastangle::radians)fastangle::tan(val),
227 if(error > largest_error)
229 if(error < -largest_error)
230 largest_error=-error;
234 printf("fastangle: Largest ATAN error: (+/-)%frad\n",largest_error);
235 if(largest_error>0.075)ret++;
244 theta=angle::degrees(-179);
245 theta<angle::degrees(180);
246 theta+=angle::degrees(5)
249 error=angle::radians(theta).get()-fastangle::radians(fastangle::tan(angle::sin(theta).get(),angle::cos(theta).get())).get();
251 fprintf(stderr,"\tfastangle: atan2(%f, %f)=%fdeg (%f) ;\t diff: %frad\n",
252 (float)angle::sin(theta),
253 (float)angle::cos(theta),
254 (float)(fastangle::degrees)fastangle::tan(angle::sin(theta),angle::cos(theta)),
255 (float)(fastangle::degrees)fastangle::tan((float)angle::sin(theta)/(float)angle::cos(theta)),
259 if(error > largest_error)
261 if(error < -largest_error)
262 largest_error=-error;
266 printf("fastangle: Largest ATAN2 error: (+/-)%frad\n",largest_error);
267 if(largest_error>0.075)ret++;
269 printf("constant tests: %f==%f\n",
270 (float)angle::degrees(angle::tan(1.01)).get(),
271 (float)fastangle::degrees(fastangle::tan(1.01)).get());
272 printf("constant tests: %f==%f\n",
273 (float)angle::degrees(angle::tan(-1.0)).get(),
274 (float)fastangle::degrees(fastangle::tan(-1.0)).get());
279 template <class Angle>
280 void angle_cos_speed_test(void)
285 for(tmp=-1.0;tmp<1.0;tmp+=0.000002)
287 a=(typename Angle::cos)(tmp);
288 b=(typename Angle::cos)(tmp);
289 c=(typename Angle::cos)(tmp);
290 d=(typename Angle::cos)(tmp);
291 tmp2=((typename Angle::cos)(a)).get();
292 tmp2=((typename Angle::cos)(b)).get();
293 tmp2=((typename Angle::cos)(c)).get();
294 tmp2=((typename Angle::cos)(d)).get();
297 template <class Angle>
298 void angle_sin_speed_test(void)
303 for(tmp=-1.0;tmp<1.0;tmp+=0.000002)
305 a=(typename Angle::sin)(tmp);
306 b=(typename Angle::sin)(tmp);
307 c=(typename Angle::sin)(tmp);
308 d=(typename Angle::sin)(tmp);
309 tmp2=((typename Angle::sin)(a)).get();
310 tmp2=((typename Angle::sin)(b)).get();
311 tmp2=((typename Angle::sin)(c)).get();
312 tmp2=((typename Angle::sin)(d)).get();
315 template <class Angle>
316 void angle_tan_speed_test(void)
321 for(tmp=-1.0;tmp<1.0;tmp+=0.000002)
323 a=(typename Angle::tan)(tmp);
324 b=(typename Angle::tan)(tmp);
325 c=(typename Angle::tan)(tmp);
326 d=(typename Angle::tan)(tmp);
327 tmp2=((typename Angle::tan)(a)).get();
328 tmp2=((typename Angle::tan)(b)).get();
329 tmp2=((typename Angle::tan)(c)).get();
330 tmp2=((typename Angle::tan)(d)).get();
333 template <class Angle, class mytan>
334 void angle_atan2_speed_test(void)
339 for(y=-10.0;y<10.0;y+=0.05)
340 for(x=-10.0;x<10.0;x+=0.05)
357 int fastangle_speed_test(void)
366 fastangle_atan2_time,
373 angle_cos_speed_test<angle>();
374 angle_cos_time=MyTimer();
375 printf("angle: Cosine test: %f seconds\n",angle_cos_time);
378 angle_cos_speed_test<fastangle>();
379 fastangle_cos_time=MyTimer();
380 printf("fastangle: Cosine test: %f seconds\n",fastangle_cos_time);
381 printf("fastangle is %.02f%% faster\n",(angle_cos_time/fastangle_cos_time)*100.0-100.0);
384 angle_sin_speed_test<angle>();
385 angle_sin_time=MyTimer();
386 printf("angle: Sine test: %f seconds\n",angle_sin_time);
389 angle_sin_speed_test<fastangle>();
390 fastangle_sin_time=MyTimer();
391 printf("fastangle: Sine test: %f seconds\n",fastangle_sin_time);
392 printf("fastangle is %.02f%% faster\n",(angle_sin_time/fastangle_sin_time)*100.0-100.0);
395 angle_tan_speed_test<angle>();
396 angle_tan_time=MyTimer();
397 printf("angle: Tangent test: %f seconds\n",angle_tan_time);
400 angle_tan_speed_test<fastangle>();
401 fastangle_tan_time=MyTimer();
402 printf("fastangle: Tangent test: %f seconds\n",fastangle_tan_time);
403 printf("fastangle is %.02f%% faster\n",(angle_tan_time/fastangle_tan_time)*100.0-100.0);
406 angle_atan2_speed_test<angle,angle::tan>();
407 angle_atan2_time=MyTimer();
408 printf("angle: arcTangent2 test: %f seconds\n",angle_atan2_time);
411 angle_atan2_speed_test<fastangle,fastangle::tan>();
412 fastangle_atan2_time=MyTimer();
413 printf("fastangle: arcTangent2 test: %f seconds\n",fastangle_atan2_time);
414 printf("fastangle is %.02f%% faster\n",(angle_atan2_time/fastangle_atan2_time)*100.0-100.0);
424 dist=angle::deg(angle::deg(330).dist(angle::deg(30))).get();
425 printf("angle: angular difference between 330deg and 30deg is %0.1fdeg\n",dist);
426 if(floor(dist+0.5)!=300)
428 printf("angle: error: should be 300deg!\n");
432 dist=angle::deg(angle::deg(30).dist(angle::deg(330))).get();
433 printf("angle: angular difference between 30deg and 330deg is %0.1fdeg\n",dist);
434 if(floor(dist+0.5)!=-300)
436 printf("angle: error: should be -300deg!\n");
440 dist=angle::deg(angle::deg(30).dist(angle::deg(-30))).get();
441 printf("angle: angular difference between 30deg and -30deg is %0.1fdeg\n",dist);
442 if(floor(dist+0.5)!=60)
444 printf("angle: error: should be 60deg!\n");
448 dist=angle::deg(angle::deg(-30).dist(angle::deg(30))).get();
449 printf("angle: angular difference between -30deg and 30deg is %0.1fdeg\n",dist);
450 if(floor(dist+0.5)!=-60)
452 printf("angle: error: should be -60deg!\n");
456 dist=angle::deg(angle::deg(20).dist(angle::deg(195))).get();
457 printf("angle: angular difference between 20deg and 195deg is %0.1fdeg\n",dist);
458 if(floor(dist+0.5)!=-175)
460 printf("angle: error: should be -175deg!\n");
464 dist=angle::deg(angle::deg(20).dist(angle::deg(205))).get();
465 printf("angle: angular difference between 20deg and 205deg is %0.1fdeg\n",dist);
466 if(floor(dist+0.5)!=-185)
468 printf("angle: error: should be -185deg!\n");
474 for(i=-1000;i<1000;i++)
476 dist=angle::deg(angle::deg(20+i+360).dist(angle::deg(205+i-360))).get();
477 if(floor(dist+0.5)!=535)
479 printf("angle: error: Badness at %d!\n",i);
485 for(i=-1000;i<1000;i++)
487 dist=angle::deg(angle::deg(20+i-360).dist(angle::deg(195+i+360))).get();
488 if(floor(dist+0.5)!=-895)
490 printf("angle: error: Badness at %d!\n",i);
500 angle a(angle::deg(-2005));
501 angle b(angle::deg(200));
503 affine_combo<angle> combo;
505 hermite<angle> hermie(a,b,b.dist(a),b.dist(a));
507 for(f=0;f<1.001;f+=0.1)
509 printf("@%f--affine_combo: %f hermie: %f\n",angle::deg(f).get(),angle::deg(combo(a,b,f)).get(),angle::deg(hermie(f)).get());
517 /* === E N T R Y P O I N T ================================================= */
523 error+=fastangle_test();
524 error+=fastangle_speed_test();