Menu

#73 Sine And cosine optimization proposal

GC 3.x
open
nobody
5 - default
2026-04-01
2026-04-01
No

Hello Simon,
Please find attached the results of the work on the sine and cosine functions.

The attached file contains a proposed modification of the sine and cosine functions that is approximately twice as fast in calculation time and with greater accuracy.

The principle:

  • a function that reduces the initial angle to an angle whose absolute value is between 0 and π/2 and also returns the quadrant to which the initial angle belongs

  • we use the calculation of the cosine to evaluate the sine. The function is optimized compared to the original because the angle is first divided by a power of 2, and then, after calculating the series, the identity cos(2x) = 2(cos(x/2)^2) - 1 is used.

Finally, the calculations are performed with greater precision because they are based on the precision used for the constant π plus an extra.

All the tests are compared in terms of precision with the mpfr library.

I integrated this code snippet into the cosine calculation.

     {
            cob_sli_t       scale;
            cob_sli_t       len;

            char buffer[5 + 2];
            mpf_get_str (buffer, &scale, 10, 5, dst_val);
            if ( scale  <= COB_MAX_PREC_DIGITS ) {
                    mpf_set_ui (dst_val, 0UL) ;
                    return ;
            }
    }

`

All calculations are performed in Gnucobol with a precision of 2048 bits using the GMP library. This corresponds to a number of digits of approximately 616 (2048*ln(2)/ln(10)). If the exponent of the calculated number is less than this value, the result is considered to be 0.
If this test is retained, it means that we should test the result of the cosine function to calculate the tangent.
Denis

4 Attachments

Discussion


Log in to post a comment.

MongoDB Logo MongoDB