How to calculate roll and pitch from an accelerometer in whatever orientation?

I’m trying to use a tri-axial accelerometer as an inclinometer, measuring the so called roll and pitch angles. The underlined principle is that in stationary conditions an accelerometer senses only the gravity acceleration. For this reason, instead of acquiring accelerometer data in various orientation, I simulated the output of an accelerometer starting from two known values of the roll and pitch (user-defined values) and from it I calculated the two angles. More precisely, I defined for each simulation a couple of roll and pitch values belonging to the four quadrants. The output of an accelerometer in stationary conditions can be simulated according to equation 24 of this document

https://www.nxp.com/files-static/sensors/doc/app_note/AN3461.pdf

I attach below the values of the accelerometer outputs, their ratios, and the true and calculated angles.

As you can see, in most of the cases the calculated angles are different from the true ones. But these discrepancies can be avoided identifying the orientation from the accelerometer values and their ratios signs, and subsequently, adding or subtracting 90° or 180°. Unfortunately, there are two couple of orientations which can not be distinguished using this workaround: the first one corresponds to true values of (296.3, 321.8 – row 5) and (106.3, 221.8 – row 10) of roll and pitch respectively; the second one corresponds to true values of (26.3, 131.8 – row 6) and (206.3, 51.8 – row 12) of roll and pitch respectively. In these two couple of conditions, the values of acceleration and their ratios have the same sign and so cannot be distinguished in order to correct them. My question is about the most accurate way to calculate the roll and pitch angles from the output of a tri-axial accelerometer considering that the accelerometer can be oriented in whatever pose.