Changeset 660 for libcaca/trunk


Ignore:
Timestamp:
Mar 22, 2006, 3:56:50 PM (15 years ago)
Author:
Sam Hocevar
Message:
  • Use ln(x) = 2 * (t + t3/3 + t5/5 + ...) with t = (x-1)/(x+1).
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/cucul/bitmap.c

    r657 r660  
    184184    register long double v, e;
    185185#else
    186     register float tmp, t, r;
     186    register float tmp, t, t2, r;
    187187    int i;
    188188#endif
     
    209209    return v;
    210210#else
    211     /* Compute ln(x) as ln(1+t) where t = x - 1, x ∈ ]0,1[
    212      *   ln(1+t) = t - t^2/2 + t^3/3 - t^4/4 + t^5/5 ...
    213      * The convergence is quite slow, especially when x is near 0. */
    214     tmp = t = r = x - 1.0;
    215     for(i = 2; i < 30; i++)
    216     {
    217         r *= -t;
     211    /* Compute ln(x) for x ∈ ]0,1]
     212     *   ln(x) = 2 * (t + t^3/3 + t^5/5 + ...) with t = (x-1)/(x+1)
     213     * The convergence is a bit slow, especially when x is near 0. */
     214    t = (x - 1.0) / (x + 1.0);
     215    t2 = t * t;
     216    tmp = r = t;
     217    for(i = 3; i < 20; i += 2)
     218    {
     219        r *= t2;
    218220        tmp += r / i;
    219221    }
    220222
    221     /* Compute x^-y as e^t where t = y*ln(x):
     223    /* Compute -y*ln(x) */
     224    tmp = - y * 2.0 * tmp;
     225
     226    /* Compute x^-y as e^t where t = -y*ln(x):
    222227     *   e^t = 1 + t/1! + t^2/2! + t^3/3! + t^4/4! + t^5/5! ...
    223      * The convergence is a lot faster here, thanks to the factorial. */
    224     r = t = - y * tmp;
     228     * The convergence is quite faster here, thanks to the factorial. */
     229    r = t = tmp;
    225230    tmp = 1.0 + t;
    226231    for(i = 2; i < 16; i++)
Note: See TracChangeset for help on using the changeset viewer.