Sin table lookup

Run Settings
LanguageC
Language Version
Run Command
#include <stdio.h> #include <math.h> #include <assert.h> typedef struct { double input; double output; } Sin_Table_Cell; Sin_Table_Cell sin_table[128]; double lerp(double a, double b, double t) { return (1 - t) * a + t * b; } double sin_l(double v) { double fi = (v / 6.283185307179586) * 128; // My sin_table generation is a bit broken around the overflow assert(fi >= 1); assert(fi < 127); int li = fi; int hi = li + 1; double ls = sin_table[li].output; double hs = sin_table[hi].output; double diff = fi - li; return lerp(ls, hs, diff); } int main(void) { for(int i = 0; i < 128; ++i) { double input = i / 20.3821656051; //printf("\t(Sin_Table_Cell){%g, %g},\n", input, sin(input)); } double v = 0.7; printf("sin(%g) == %g\n", v, sin(v)); printf("sin_l(%g) == %g\n", v, sin_l(v)); return 0; } Sin_Table_Cell sin_table[128] = { (Sin_Table_Cell){0, 0}, (Sin_Table_Cell){0.0490625, 0.0490428}, (Sin_Table_Cell){0.098125, 0.0979676}, (Sin_Table_Cell){0.147187, 0.146657}, (Sin_Table_Cell){0.19625, 0.194993}, (Sin_Table_Cell){0.245312, 0.242859}, (Sin_Table_Cell){0.294375, 0.290142}, (Sin_Table_Cell){0.343437, 0.336726}, (Sin_Table_Cell){0.3925, 0.382499}, (Sin_Table_Cell){0.441562, 0.427353}, (Sin_Table_Cell){0.490625, 0.471177}, (Sin_Table_Cell){0.539687, 0.513868}, (Sin_Table_Cell){0.58875, 0.555322}, (Sin_Table_Cell){0.637812, 0.595439}, (Sin_Table_Cell){0.686875, 0.634124}, // sin_l(0.7) samples this and (Sin_Table_Cell){0.735937, 0.671282}, // this sample, and does a lerp between them (Sin_Table_Cell){0.785, 0.706825}, (Sin_Table_Cell){0.834062, 0.740667}, (Sin_Table_Cell){0.883125, 0.772726}, (Sin_Table_Cell){0.932187, 0.802926}, (Sin_Table_Cell){0.98125, 0.831193}, (Sin_Table_Cell){1.03031, 0.85746}, (Sin_Table_Cell){1.07937, 0.881663}, (Sin_Table_Cell){1.12844, 0.903744}, (Sin_Table_Cell){1.1775, 0.923651}, (Sin_Table_Cell){1.22656, 0.941334}, (Sin_Table_Cell){1.27562, 0.956752}, (Sin_Table_Cell){1.32469, 0.969868}, (Sin_Table_Cell){1.37375, 0.980649}, (Sin_Table_Cell){1.42281, 0.98907}, (Sin_Table_Cell){1.47187, 0.995111}, (Sin_Table_Cell){1.52094, 0.998757}, (Sin_Table_Cell){1.57, 1}, (Sin_Table_Cell){1.61906, 0.998835}, (Sin_Table_Cell){1.66812, 0.995267}, (Sin_Table_Cell){1.71719, 0.989304}, (Sin_Table_Cell){1.76625, 0.98096}, (Sin_Table_Cell){1.81531, 0.970255}, (Sin_Table_Cell){1.86437, 0.957214}, (Sin_Table_Cell){1.91344, 0.941871}, (Sin_Table_Cell){1.9625, 0.92426}, (Sin_Table_Cell){2.01156, 0.904425}, (Sin_Table_Cell){2.06062, 0.882413}, (Sin_Table_Cell){2.10969, 0.858278}, (Sin_Table_Cell){2.15875, 0.832077}, (Sin_Table_Cell){2.20781, 0.803874}, (Sin_Table_Cell){2.25687, 0.773736}, (Sin_Table_Cell){2.30594, 0.741736}, (Sin_Table_Cell){2.355, 0.707951}, (Sin_Table_Cell){2.40406, 0.672462}, (Sin_Table_Cell){2.45312, 0.635355}, (Sin_Table_Cell){2.50219, 0.596718}, (Sin_Table_Cell){2.55125, 0.556646}, (Sin_Table_Cell){2.60031, 0.515234}, (Sin_Table_Cell){2.64937, 0.472581}, (Sin_Table_Cell){2.69844, 0.428792}, (Sin_Table_Cell){2.7475, 0.383971}, (Sin_Table_Cell){2.79656, 0.338225}, (Sin_Table_Cell){2.84562, 0.291666}, (Sin_Table_Cell){2.89469, 0.244404}, (Sin_Table_Cell){2.94375, 0.196555}, (Sin_Table_Cell){2.99281, 0.148232}, (Sin_Table_Cell){3.04187, 0.0995525}, (Sin_Table_Cell){3.09094, 0.0506335}, (Sin_Table_Cell){3.14, 0.00159265}, (Sin_Table_Cell){3.18906, -0.047452}, (Sin_Table_Cell){3.23812, -0.0963825}, (Sin_Table_Cell){3.28719, -0.145081}, (Sin_Table_Cell){3.33625, -0.19343}, (Sin_Table_Cell){3.38531, -0.241314}, (Sin_Table_Cell){3.43437, -0.288617}, (Sin_Table_Cell){3.48344, -0.335226}, (Sin_Table_Cell){3.5325, -0.381027}, (Sin_Table_Cell){3.58156, -0.425912}, (Sin_Table_Cell){3.63062, -0.469772}, (Sin_Table_Cell){3.67969, -0.512501}, (Sin_Table_Cell){3.72875, -0.553997}, (Sin_Table_Cell){3.77781, -0.594159}, (Sin_Table_Cell){3.82687, -0.632892}, (Sin_Table_Cell){3.87594, -0.670101}, (Sin_Table_Cell){3.925, -0.705698}, (Sin_Table_Cell){3.97406, -0.739596}, (Sin_Table_Cell){4.02312, -0.771714}, (Sin_Table_Cell){4.07219, -0.801975}, (Sin_Table_Cell){4.12125, -0.830306}, (Sin_Table_Cell){4.17031, -0.856639}, (Sin_Table_Cell){4.21937, -0.88091}, (Sin_Table_Cell){4.26844, -0.903062}, (Sin_Table_Cell){4.3175, -0.923039}, (Sin_Table_Cell){4.36656, -0.940796}, (Sin_Table_Cell){4.41562, -0.956288}, (Sin_Table_Cell){4.46469, -0.969479}, (Sin_Table_Cell){4.51375, -0.980336}, (Sin_Table_Cell){4.56281, -0.988834}, (Sin_Table_Cell){4.61187, -0.994953}, (Sin_Table_Cell){4.66094, -0.998677}, (Sin_Table_Cell){4.71, -0.999997}, (Sin_Table_Cell){4.75906, -0.998911}, (Sin_Table_Cell){4.80812, -0.995421}, (Sin_Table_Cell){4.85719, -0.989535}, (Sin_Table_Cell){4.90625, -0.981268}, (Sin_Table_Cell){4.95531, -0.970639}, (Sin_Table_Cell){5.00437, -0.957674}, (Sin_Table_Cell){5.05344, -0.942404}, (Sin_Table_Cell){5.1025, -0.924867}, (Sin_Table_Cell){5.15156, -0.905103}, (Sin_Table_Cell){5.20062, -0.883162}, (Sin_Table_Cell){5.24969, -0.859094}, (Sin_Table_Cell){5.29875, -0.83296}, (Sin_Table_Cell){5.34781, -0.80482}, (Sin_Table_Cell){5.39687, -0.774744}, (Sin_Table_Cell){5.44594, -0.742803}, (Sin_Table_Cell){5.495, -0.709075}, (Sin_Table_Cell){5.54406, -0.67364}, (Sin_Table_Cell){5.59312, -0.636584}, (Sin_Table_Cell){5.64219, -0.597995}, (Sin_Table_Cell){5.69125, -0.557968}, (Sin_Table_Cell){5.74031, -0.516598}, (Sin_Table_Cell){5.78937, -0.473984}, (Sin_Table_Cell){5.83844, -0.43023}, (Sin_Table_Cell){5.8875, -0.385441}, (Sin_Table_Cell){5.93656, -0.339723}, (Sin_Table_Cell){5.98562, -0.293189}, (Sin_Table_Cell){6.03469, -0.245948}, (Sin_Table_Cell){6.08375, -0.198116}, (Sin_Table_Cell){6.13281, -0.149807}, (Sin_Table_Cell){6.18187, -0.101137}, (Sin_Table_Cell){6.23094, -0.052224}, };
Editor Settings
Theme
Key bindings
Full width
Lines