Adafruit_LSM303 tilt compensated compass I am having troubles with tilt compensating my compass.

I am using the LSM303DLHC accelerometer/compass in combination together with the Adafruit_LSM303_U.h.

I was able to get my compass to the right direction withthis little snippet:

  float heading = (atan2(event.magnetic.y,event.magnetic.x) * 180) / Pi;


I also was able to get my roll and tilt angle with converting my X and Y of my accelerometer to angles:

   headingNew = asin(eventA.acceleration.x/10);
   float tilt = headingNew*180/PI/90*100;
   headingNew = asin(eventA.acceleration.y/10);
   float roll = headingNew*180/PI/90*100;


Now I need to compensate my angle to get a new x and y for inside my heading calculation but the problem is that I can't seem to get the right functions.
Online I found the functions below from a large tutorial about the sensor but this gives me completely wrong values.

    //Xh = event.magnetic.x*cos(tilt)+event.magnetic.z*sin(tilt);
    //Yh = event.magnetic.x*sin(roll)*sin(tilt)+event.magnetic.y*cos(roll)-event.magnetic.z*sin(roll)*cos(tilt);


or in the code itself:

   Xh = event.magnetic.x*(eventA.acceleration.z/10)+event.magnetic.z*(eventA.acceleration.x/10);
   Yh = event.magnetic.x*(eventA.acceleration.y/10)*(eventA.acceleration.x/10)+event.magnetic.y*(eventA.acceleration.z/10)-event.magnetic.z*(eventA.acceleration.y/10)*(eventA.acceleration.z/10);


I actually get the right angle on a flat surface. But it is even more inaccurate at an angle as the normal heading.
Does anybody have any experience with this sensor? Already a big thanks for the help.
The sin() and cos() functions expect their arguments to be in
radians. If you feed them with anything else, be it degrees, percent
slope, or whatever, expect the result to be just crap.



Edit: The OP changed the question, invalidating my answer. So here
comes a new answer...

You have mistaken eventA.acceleration.y for cos(tilt),
eventA.acceleration.x for sin(tilt), and so on. They are not the
same. You may think they are the same up to some multiplicative factor,
and that's right. However, the factor doesn't cancel out as soon as you
do computations like sin(roll)*sin(tilt). Don't rush your math: if you
want to transform known-good formulas to more practical ones, check every
step of the transformation carefully.