[openbeos] Re: trigonometric functions

Axel Dörfler wrote on Tue, 17 May 2005 23:48:18 +0200 CEST:

> "Stephan Assmus" <superstippi@xxxxxx> wrote:
> > I've run into a strange problem. When running the Haiku app_server 
> > on 
> > R5, Painter draws ellipses just fine. However, when running the 
> > real 
> > thing, ellipses look really strange, though they look always the 
> > same 
> > weird shape. At first I was completely puzzled, but now I'm 
> > suspecting 
> > that maybe one or more trigonometric functions might return bogus. 
> > Any 
> > ideas on how to track this down in an efficient way? I have not 
> > digged 
> > to deeply into AGG to see which ones it uses, but I'm hoping that 
> > there 
> > are some tests which I can run on stdlib, libroot or whatever 
> > implements these.
> 
> AFAICT there are no unit tests for these yet. Feel free to write 
> them, 
> though :-)
> All trigonometric functions can be converted to one another with more 
> or less effort, and there is also fixed behaviour and terms (like a^2
> +b
> ^2=c^2) that you can use for testing.

It is embarassing, but I really don't have the slightest clue on how to 
fix this. I have gotten as far as confirming that sin() and cos() are 
broken in our libroot. Here are some tests:
              R5             "Haiku on R5"    Haiku

sin(-10) =    0.544021       -0.544021      -10.000000
cos(-10) =   -0.839072        0.839072        1.000000

sin(-9) =    -0.412118        0.412118       -9.000000
cos(-9) =    -0.911130        0.911130        1.000000

sin(-8) =    -0.989358       -0.145500       -8.000000
cos(-8) =    -0.145500        0.989358        1.000000

sin(-7) =    -0.656987       -0.656987       -7.000000
cos(-7) =     0.753902        0.753902        1.000000

sin(-6) =     0.279415        0.279415       -6.000000
cos(-6) =     0.960170        0.960170        1.000000

sin(-5) =     0.958924       -0.283662       -5.000000
cos(-5) =     0.283662        0.958924        1.000000

sin(-4) =     0.756802        0.653644       -4.000000
cos(-4) =    -0.653644        0.756802        1.000000

sin(-3) =    -0.141120        0.141120       -3.000000
cos(-3) =    -0.989992        0.989992        1.000000

sin(-2) =    -0.909297       -0.416147       -2.000000
cos(-2) =    -0.416147        0.909297        1.000000

sin(-1) =    -0.841471        0.540302       -1.000000
cos(-1) =     0.540302        0.841471        1.000000

sin(0) =      0.000000        0.000000        0.000000
cos(0) =      1.000000        1.000000        1.000000

sin(1) =      0.841471       -0.540302       -0.570796
cos(1) =      0.540302        0.841471        1.000000

sin(2) =      0.909297        0.416147        0.416147
cos(2) =     -0.416147        0.909297        0.909297

sin(3) =      0.141120       -0.141120       -0.141593
cos(3) =     -0.989992        0.989992        1.000000

sin(4) =     -0.756802       -0.653644       -0.712389
cos(4) =     -0.653644        0.756802       -0.712389

sin(5) =     -0.958924        0.283662        0.283662
cos(5) =      0.283662        0.958924        0.958924

sin(6) =     -0.279415       -0.279415       -0.283185
cos(6) =      0.960170        0.960170        1.000000

sin(7) =      0.656987        0.656987        0.656987
cos(7) =      0.753902        0.753902        0.753902

sin(8) =      0.989358        0.145500        0.145500
cos(8) =     -0.145500        0.989358        0.989358

sin(9) =      0.412118       -0.412118       -0.424778
cos(9) =     -0.911130        0.911130        1.000000

sin(10) =    -0.544021        0.544021        0.544021
cos(10) =    -0.839072        0.839072        0.839072


By "Haiku on R5" I mean I have copied all related headers (trig.h, 
math.h, mathimpl.h) and sincos.c into my project folder, replaced 
#include <math.h> with #include "math.h" and did the same thing in the 
subsequent headers.

The code is this:

void
print_sin(double value)
{
        printf("sin(%f) = %f\n", value, sin(value));
}

void
print_cos(double value)
{
        printf("cos(%f) = %f\n", value, cos(value));
}

// main
int
main(int argc, char** argv)
{
        for (double v = -10.0; v <= 10.0; v++) {
                print_sin(v);
                print_cos(v);
                printf("\n");
        }
}

I'm thankful for any and all help. As you can guess, this problem is 
hold up for my demo app.

Best regards and thanks in advance,
-Stephan


Other related posts: