Log function

Is this the best way for making a log function?

a^x = y

A = (x-1)/(x+1)
ln(x) = ( 1/n(A^n ) //where n starts at 1 and increases by 2 each time
log(x) = ln(x) / ln(10)


so something like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>

double log( const double &x );

int main()
{
    using std::cout;
    using std::endl;
    using std::cin;

    char again = 'y';
    double x;
    while( again == 'y' )
    {
        cout << "Please enter X: ";
        cin >> x;
        cout << "Log(" << x << ") = " << log( x ) << endl;
        cout << "Again(y/n)? ";
        cin >> again;
    }
}

double log( const double &x )
{
    const double THRESHOLD = 0.000000001;
    if( x <= 0.0 + THRESHOLD )
        throw "Domain error: Can't have a domain of a negative number.";
    else if(x >= 1.0 - THRESHOLD && x <= 1.0 + THRESHOLD )
        return( 0 ); //a^0 == 1  in formula a^x = y;
    else
    {
        const double A = (x-1.0)/(x+1.0);
        double tempA = A;
        int n = 1;
        const int PRECISION = 15;
        const double BASE10 = 2.3025850929940456840179914546844;
        double result = 0.0;
        for( int i = 0; i < PRECISION; ++i )
        {
            result += (1.0 / n) * tempA;
            n += 2;
            tempA *= A * A;;
        }
        result *= 2.0;
        result /= BASE10; //convert from base e to base 10
        return( result );
    }
}


Or are there better formulas for finding logs? I tried to understand what they were doing in the cmath header it looks like they make a table of like 500 random logs then use those in the function but I don't exactly understand that I'm better with formulas.


Thanks,
~Giblit

they make a table of like 500 random logs then use those in the function but I don't exactly understand that I'm better with formulas.

It is not surprising approach. For many smooth functions it is beneficial to have some points precalculated and then to interpolate other values between them with linear or quadratic function. Technically you can for each segment [x1, x2) store two or three coefficients for linear or quadratic function approximating target function with the Ax^2 + Bx + C.

As for logarithm, one needs precalculations only for short segment, since you know, for example, for log with base 10 we can use formula like:

Log(12345) = Log(1.2345) + 4

So we can only create interpolation table for segment [1, 2)
Alright thanks I'll have to give that a try I forgot about that shortcut with logs and when I said table I mean they have tables of hexadecimal values like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
__private_extern__
const uint32_t logTable[] __attribute__ ((aligned(8))) = 
      { 
/*i = 0*/                                                                       
      0x3FE81000, 0x0029D0FA, 0x3FF54725, 0xE69688D3, 0xBFD23EC5, 0x98AF81D4, 
      0x3FE83000, 0x0029AA1F, 0x3FF52AFF, 0x56838EB8, 0xBFD1E9E1, 0x671A5B74, 
      0x3FE85000, 0x00313D0A, 0x3FF50F22, 0xE0E71E79, 0xBFD1956D, 0x3B1A2597, 
      0x3FE87000, 0x001372E5, 0x3FF4F38F, 0x62CC9FF7, 0xBFD14167, 0xEF038828, 
      0x3FE89000, 0x001CDE1B, 0x3FF4D843, 0xBEC3ACBD, 0xBFD0EDD0, 0x606C48DC, 
      0x3FE8B000, 0x00297513, 0x3FF4BD3E, 0xDD83BC26, 0xBFD09AA5, 0x727B4D71, 
      0x3FE8D000, 0x0014C18E, 0x3FF4A27F, 0xAD64BE64, 0xBFD047E6, 0x0CA8FA3A, 
      0x3FE8F000, 0x0015D3E2, 0x3FF48805, 0x21EF4FEF, 0xBFCFEB22, 0x3379FE23, 
      0x3FE91000, 0x0026073F, 0x3FF46DCE, 0x343A6103, 0xBFCF474B, 0x128BBA00, 
      0x3FE93000, 0x00255A5B, 0x3FF453D9, 0xE2A95180, 0xBFCEA444, 0x9E46D828, 
      0x3FE95000, 0x00285F27, 0x3FF43A27, 0x308BAB81, 0xBFCE020C, 0xC5573403, 
      0x3FE97000, 0x00196E3C, 0x3FF420B5, 0x264A3A02, 0xBFCD60A1, 0x7F103DF7, 
      0x3FE99000, 0x00149CB4, 0x3FF40782, 0xD0FE3EDF, 0xBFCCC000, 0xC974061A, 
      0x3FE9B000, 0x001BAF93, 0x3FF3EE8F, 0x42903385, 0xBFCC2028, 0xAA8E048C, 
      0x3FE9D000, 0x0027FC5C, 0x3FF3D5D9, 0x918BBBBF, 0xBFCB8117, 0x2FF1DB60, 
      0x3FE9F000, 0x0024313F, 0x3FF3BD60, 0xD9079E01, 0xBFCAE2CA, 0x6EB49066, 
      0x3FEA1000, 0x001ACE5A, 0x3FF3A524, 0x38669383, 0xBFCA4540, 0x82630427, 
      0x3FEA3000, 0x00122129, 0x3FF38D22, 0xD3587F83, 0xBFC9A877, 0x8D930D07, 
      0x3FEA5000, 0x001F0679, 0x3FF3755B, 0xD1B2545E, 0xBFC90C6D, 0xB965CFD5, 
      0x3FEA7000, 0x002067B1, 0x3FF35DCE, 0x5F876E13, 0xBFC87121, 0x36B40519, 
      0x3FEA9000, 0x001B2FDA, 0x3FF34679, 0xACCC58CD, 0xBFC7D690, 0x3C2C5884, 
      0x3FEAB000, 0x0028C3FD, 0x3FF32F5C, 0xED4CCFCB, 0xBFC73CB9, 0x068C4BA9, 
      0x3FEAD000, 0x002551E6, 0x3FF31877, 0x58CF5787, 0xBFC6A399, 0xDA09AA3A, 
      0x3FEAF000, 0x002A6E2C, 0x3FF301C8, 0x2AA61215, 0xBFC60B30, 0xFFE6F63E, 
      0x3FEB1000, 0x0012C8E5, 0x3FF2EB4E, 0xA1F1AF6F, 0xBFC5737C, 0xC8A8B3CB, 
      0x3FEB3000, 0x002900DB, 0x3FF2D50A, 0x0110E9AF, 0xBFC4DC7B, 0x88BAB61A, 
      0x3FEB5000, 0x0021C9F4, 0x3FF2BEF9, 0x8E430627, 0xBFC4462B, 0x9D2B59DD, 
      0x3FEB7000, 0x0013B33D, 0x3FF2A91C, 0x92E65B0D, 0xBFC3B08B, 0x66FC0B35, 
      0x3FEB9000, 0x001AF6AA, 0x3FF29372, 0x5BA5D88A, 0xBFC31B99, 0x4CBD179C, 
      0x3FEBB000, 0x00260E7B, 0x3FF27DFA, 0x3888636F, 0xBFC28753, 0xBB61BBEA, 
      0x3FEBD000, 0x001C0820, 0x3FF268B3, 0x7CC3734D, 0xBFC1F3B9, 0x25715AF4, 
      0x3FEBF000, 0x001EBC63, 0x3FF2539D, 0x7E7D4E24, 0xBFC160C8, 0x01BE5574, 
      0x3FEC1000, 0x001902A9, 0x3FF23EB7, 0x97071DA2, 0xBFC0CE7E, 0xCD5AAE6F, 
      0x3FEC3000, 0x0024932F, 0x3FF22A01, 0x22888083, 0xBFC03CDC, 0x09ABD5A6, 
      0x3FEC5000, 0x0013D8A9, 0x3FF21579, 0x803BA8C2, 0xBFBF57BC, 0x7CDC92D1, 
      0x3FEC7000, 0x001C5240, 0x3FF20120, 0x11EF31C8, 0xBFBE3707, 0xED31544C, 
      0x3FEC9000, 0x0018D480, 0x3FF1ECF4, 0x3C702302, 0xBFBD1797, 0x87430759, 
      0x3FECB000, 0x00150CCE, 0x3FF1D8F5, 0x6721322C, 0xBFBBF968, 0x75E3F1BC, 
      0x3FECD000, 0x0017661A, 0x3FF1C522, 0xFC0E71F6, 0xBFBADC77, 0xED8B044D, 
      0x3FECF000, 0x0012279F, 0x3FF1B17C, 0x67E7A12F, 0xBFB9C0C3, 0x2CAC8965, 
      0x3FED1000, 0x001E5763, 0x3FF19E01, 0x19CDAD28, 0xBFB8A647, 0x79869857, 
      0x3FED3000, 0x001CD50E, 0x3FF18AB0, 0x837ED7D3, 0xBFB78D02, 0x2540A07F, 
      0x3FED5000, 0x0020A798, 0x3FF1778A, 0x19086124, 0xBFB674F0, 0x88192A2B, 
      0x3FED7000, 0x001D78AD, 0x3FF1648D, 0x50EAC854, 0xBFB55E10, 0x040DB811, 
      0x3FED9000, 0x001E48FB, 0x3FF151B9, 0xA3EC17B0, 0xBFB4485E, 0x02D59DD1, 
      0x3FEDB000, 0x001AF138, 0x3FF13F0E, 0x8D24A05A, 0xBFB333D7, 0xF72FEB7D, 
      0x3FEDD000, 0x0010AD74, 0x3FF12C8B, 0x89E4252E, 0xBFB2207B, 0x5BE91ED0, 
      0x3FEDF000, 0x001AB6D1, 0x3FF11A30, 0x1998055D, 0xBFB10E45, 0xB2E67875, 
      0x3FEE1000, 0x0013E0C6, 0x3FF107FB, 0xBDF5CD94, 0xBFAFFA69, 0x10590729, 
      0x3FEE3000, 0x001883FD, 0x3FF0F5ED, 0xFAA55F81, 0xBFADDA8A, 0xDAC82163, 
      0x3FEE5000, 0x0015539A, 0x3FF0E406, 0x55767DDE, 0xBFABBCEB, 0xFB00BB84, 
      0x3FEE7000, 0x0017E427, 0x3FF0D244, 0x56286A1F, 0xBFA9A187, 0xB3E1FC83, 
      0x3FEE9000, 0x00155A73, 0x3FF0C0A7, 0x867F8D0E, 0xBFA78859, 0x58CFBEBB, 
      0x3FEEB000, 0x0018539F, 0x3FF0AF2F, 0x7221B2D9, 0xBFA5715C, 0x4A6DEF53, 
      0x3FEED000, 0x0017674A, 0x3FF09DDB, 0xA6A2E461, 0xBFA35C8B, 0xF91C4D5E, 
      0x3FEEF000, 0x00161C41, 0x3FF08CAB, 0xB3699206, 0xBFA149E3, 0xE2927035, 
      0x3FEF1000, 0x0013FD64, 0x3FF07B9F, 0x29AE4F68, 0xBF9E72BF, 0x2580D378, 
      0x3FEF3000, 0x00101055, 0x3FF06AB5, 0x9C709E49, 0xBF9A55F5, 0x46B6554E, 
      0x3FEF5000, 0x00113323, 0x3FF059EE, 0xA0697A38, 0xBF963D61, 0x763691D7, 
      0x3FEF7000, 0x00124587, 0x3FF04949, 0xCC0CED91, 0xBF9228FB, 0x1D970718, 
      0x3FEF9000, 0x00117D16, 0x3FF038C6, 0xB7794AF4, 0xBF8C3173, 0x805895C6, 
      0x3FEFB000, 0x00118ED5, 0x3FF02864, 0xFC6E35F2, 0xBF841929, 0xF4F968B6, 

/* Log for table entries 61 < i < 66 are computed directly from a polynomial.    
0x3FEFD000, 0x001016E4, 0x3FF01824, 0x3649566C, 0xBF781212, 0x0C40D5F4, 
0x3FEFF000, 0x000FD3BE, 0x3FF00804, 0x01F90EB1, 0xBF600401, 0x45F9DCC8, 
0x3FF00800, 0x000FDC91, 0x3FEFF007, 0xFBE26580, 0x3F5FF802, 0xE8FDAB99, 
0x3FF01800, 0x000F8871, 0x3FEFD047, 0x948259EB, 0x3F77EE11, 0xFB4975A9, 
*/
      0x3ff00000, 0x00000000, 0x3ff00000, 0x00000000, 0x00000000, 0x00000000, 
/* i = 64 */
      0x3ff00000, 0x00000000, 0x3ff00000, 0x00000000, 0x00000000, 0x00000000, 
      0x3ff00000, 0x00000000, 0x3ff00000, 0x00000000, 0x00000000, 0x00000000, 
      0x3ff00000, 0x00000000, 0x3ff00000, 0x00000000, 0x00000000, 0x00000000, 
      0x3FF02800, 0x000FEE6B, 0x3FEFB0C6, 0x10B6A965, 0x3F83E729, 0x650924A1, 
      0x3FF03800, 0x000FBD7E, 0x3FEF9182, 0xB66298A0, 0x3F8BCF71, 0x3437CACB, 
      0x3FF04800, 0x000FEF7B, 0x3FEF727C, 0xCE408B87, 0x3F91D7F7, 0xEF892C84, 
      0x3FF05800, 0x000FC95F, 0x3FEF53B3, 0xA3DBDDFD, 0x3F95C45A, 0x5595EB46, 
      0x3FF06800, 0x001006D2, 0x3FEF3526, 0x857D10A0, 0x3F99ACE7, 0x59051396, 
      0x3FF07800, 0x000FF34A, 0x3FEF16D4, 0xC42202C9, 0x3F9D91A6, 0x70340104, 
      0x3FF08800, 0x001039F4, 0x3FEEF8BD, 0xB36B8528, 0x3FA0B94F, 0x7E0FF039, 
      0x3FF09800, 0x000FA856, 0x3FEEDAE0, 0xA996B668, 0x3FA2A7EC, 0x23F8059E, 
      0x3FF0A800, 0x00110424, 0x3FEEBD3C, 0xFF65A3C3, 0x3FA494AC, 0xC558A171, 
      0x3FF0B800, 0x00128EDA, 0x3FEE9FD2, 0x1022E94D, 0x3FA67F94, 0xF2CD1118, 
      0x3FF0C800, 0x0010AE4D, 0x3FEE829F, 0x3990A123, 0x3FA868A8, 0x3280E6B8, 
      0x3FF0D800, 0x00126017, 0x3FEE65A3, 0xDBC62432, 0x3FAA4FEA, 0x01D26093, 
      0x3FF0E800, 0x0011E997, 0x3FEE48DF, 0x594F1D47, 0x3FAC355D, 0xD2B09832, 
      0x3FF0F800, 0x00130FB2, 0x3FEE2C51, 0x16F80938, 0x3FAE1907, 0x0E6683AE, 
      0x3FF10800, 0x0012E9EF, 0x3FEE0FF8, 0x7BE07E5A, 0x3FAFFAE9, 0x13D42B16, 
      0x3FF11800, 0x0015A503, 0x3FEDF3D4, 0xF157F7A3, 0x3FB0ED83, 0x9C994E7B, 
      0x3FF12800, 0x00121A8C, 0x3FEDD7E5, 0xE2F75B7D, 0x3FB1DCB2, 0x64E93C79, 
      0x3FF13800, 0x00118740, 0x3FEDBC2A, 0xBE5F2CCE, 0x3FB2CB02, 0x84FA785F, 
      0x3FF14800, 0x001585B8, 0x3FEDA0A2, 0xF35B553B, 0x3FB3B875, 0x99F08A7D, 
      0x3FF15800, 0x00139574, 0x3FED854D, 0xF3E082F4, 0x3FB4A50D, 0x3BC2C0A0, 
      0x3FF16800, 0x00152EF2, 0x3FED6A2B, 0x33CBA813, 0x3FB590CA, 0xFF27A95F, 
      0x3FF17800, 0x0014EE12, 0x3FED4F3A, 0x29144BE4, 0x3FB67BB0, 

      };


**part of the really large table since the limit on messages is capped
Topic archived. No new replies allowed.