Skip to main content
simplified
Source Link
baby-rabbit
  • 1.8k
  • 10
  • 12

C, 371402 bytes

Plenty of room for golfing, just proof of concept to begin with :P

Compiles under clang, osx.

Note: For linux you may have to change the code to use something like: "ld -shared f.o -o f.so -lm"

#include <dlfcn#include<dlfcn.h>
#include <stdio#include<stdio.h>
float e,a,r;
t;mainmain(int i,char**v) {
    // create and compile temporary c file for 'f(x)'
    FILE*file=fopen// note: include offset within function
    FILE*s=fopen("f.c","w");
    fprintf(files,"#include<math.h>\n");
    fprintf(file,"floath>\nfloat f(float x){returnx+=%s;return %s;}\n"",v[2],v[1]); 
    fclose(files);
    system("cc -fPIC -c -Wall f.c");
    system("ldc;ld -bundle f.o -o f.so -lm");
    float (*f)(float)=dlsym(dlopen("f.so",0),"f");
    
    sscanffor(v[2],"%f",&a);
    
    whilei=0;1;e=(1e==0) ?1e-37:e*2){
        r=f(e+ae);
        if(isinf(r))t=1;elsei=1;else if(!isnan(r)){
            ti?printf("%cINF\n",r>0?43:45):printf("%f\n",r);
            break;
        }
        e=(e==0)?1e-37:e*2;
    }
    return 0;
}

Run as:

./a.out "sin(x)/x" 0
./a.out "1/(x*x)" 0
./a.out "(x*x-x)/(x-1)" 1
./a.out "pow(2,x)" 3

Results:

1.000000
+INF
1.000000
8.000000

C, 371 bytes

Plenty of room for golfing, just proof of concept to begin with :P

Compiles under clang, osx.

Note: For linux you may have to change the code to use something like: "ld -shared f.o -o f.so -lm"

#include <dlfcn.h>
#include <stdio.h>
float e,a,r;
t;main(int i,char**v) {
    // create and compile temporary c file for 'f(x)'
    FILE*file=fopen("f.c","w");
    fprintf(file,"#include<math.h>\n");
    fprintf(file,"float f(float x){return %s;}\n",v[1]);
    fclose(file);
    system("cc -fPIC -c -Wall f.c");
    system("ld -bundle f.o -o f.so -lm");
    float (*f)(float)=dlsym(dlopen("f.so",0),"f");
    
    sscanf(v[2],"%f",&a);
    
    while(1) {
        r=f(e+a);
        if(isinf(r))t=1;else if(!isnan(r)){
            t?printf("%cINF\n",r>0?43:45):printf("%f\n",r);
            break;
        }
        e=(e==0)?1e-37:e*2;
    }
    return 0;
}

Run as:

./a.out "sin(x)/x" 0
./a.out "1/(x*x)" 0
./a.out "(x*x-x)/(x-1)" 1
./a.out "pow(2,x)" 3

Results:

1.000000
+INF
1.000000
8.000000

C, 402 bytes

Plenty of room for golfing, just proof of concept to begin with :P

Compiles under clang, osx.

Note: For linux you may have to change the code to use something like: "ld -shared f.o -o f.so -lm"

#include<dlfcn.h>
#include<stdio.h>
float e,r;
main(int i,char**v){
    // create and compile temporary c file for 'f(x)'
    // note: include offset within function
    FILE*s=fopen("f.c","w");
    fprintf(s,"#include<math.h>\nfloat f(float x){x+=%s;return %s;}",v[2],v[1]); 
    fclose(s);
    system("cc -fPIC -c f.c;ld -bundle f.o -o f.so -lm");
    float(*f)(float)=dlsym(dlopen("f.so",0),"f");

    for(i=0;1;e=(e==0)?1e-37:e*2){
        r=f(e);
        if(isinf(r))i=1;else if(!isnan(r)){
            i?printf("%cINF\n",r>0?43:45):printf("%f\n",r);
            break;
        }
    }
}

Run as:

./a.out "sin(x)/x" 0
./a.out "1/(x*x)" 0
./a.out "(x*x-x)/(x-1)" 1
./a.out "pow(2,x)" 3

Results:

1.000000
+INF
1.000000
8.000000
edited body
Source Link
baby-rabbit
  • 1.8k
  • 10
  • 12

C, 371 bytes

Plenty of room for golfing, just proof of concept to begin with :P

Compiles under clang, osx.

Note: For linux you may have to change the code to use something like: "ld -shared f.o -o f.so -lm"

#include <dlfcn.h>
#include <stdio.h>
float e,a,r;
t;main(int i,char**v) {
    // create and compile temporary c file for 'f(x)'
    FILE*file=fopen("f.c","w");
    fprintf(file,"#include<math.h>\n");
    fprintf(file,"float f(float x){return %s;}\n",v[1]);
    fclose(file);
    system("cc -fPIC -c -Wall f.c");
    system("ld -bundle f.o -o f.so -lm");
    float (*f)(float)=dlsym(dlopen("f.so",0),"f");
    
    sscanf(v[2],"%f",&a);
    
    while(1) {
        r=f(e+a);
        if(isinf(r))t=1;else if(!isnan(r)){
            t?printf("%cINF\n",r>0?43:45):printf("%f\n",r);
            break;
        }
        e=(e==0)?1e-37:e*2;
    }
    return 0;
}

Run as:

./a.out "sin(x)/x" 0
./a.out "1/(x*x)" 0
./a.out "(x*x-x)/(x-1)" 1
./a.out "pow(2,3x)" 13

Results:

1.000000
+INF
1.000000
8.000000

C, 371 bytes

Plenty of room for golfing, just proof of concept to begin with :P

Compiles under clang, osx.

Note: For linux you may have to change the code to use something like: "ld -shared f.o -o f.so -lm"

#include <dlfcn.h>
#include <stdio.h>
float e,a,r;
t;main(int i,char**v) {
    // create and compile temporary c file for 'f(x)'
    FILE*file=fopen("f.c","w");
    fprintf(file,"#include<math.h>\n");
    fprintf(file,"float f(float x){return %s;}\n",v[1]);
    fclose(file);
    system("cc -fPIC -c -Wall f.c");
    system("ld -bundle f.o -o f.so -lm");
    float (*f)(float)=dlsym(dlopen("f.so",0),"f");
    
    sscanf(v[2],"%f",&a);
    
    while(1) {
        r=f(e+a);
        if(isinf(r))t=1;else if(!isnan(r)){
            t?printf("%cINF\n",r>0?43:45):printf("%f\n",r);
            break;
        }
        e=(e==0)?1e-37:e*2;
    }
    return 0;
}

Run as:

./a.out "sin(x)/x" 0
./a.out "1/(x*x)" 0
./a.out "(x*x-x)/(x-1)" 1
./a.out "pow(2,3)" 1

Results:

1.000000
+INF
1.000000
8.000000

C, 371 bytes

Plenty of room for golfing, just proof of concept to begin with :P

Compiles under clang, osx.

Note: For linux you may have to change the code to use something like: "ld -shared f.o -o f.so -lm"

#include <dlfcn.h>
#include <stdio.h>
float e,a,r;
t;main(int i,char**v) {
    // create and compile temporary c file for 'f(x)'
    FILE*file=fopen("f.c","w");
    fprintf(file,"#include<math.h>\n");
    fprintf(file,"float f(float x){return %s;}\n",v[1]);
    fclose(file);
    system("cc -fPIC -c -Wall f.c");
    system("ld -bundle f.o -o f.so -lm");
    float (*f)(float)=dlsym(dlopen("f.so",0),"f");
    
    sscanf(v[2],"%f",&a);
    
    while(1) {
        r=f(e+a);
        if(isinf(r))t=1;else if(!isnan(r)){
            t?printf("%cINF\n",r>0?43:45):printf("%f\n",r);
            break;
        }
        e=(e==0)?1e-37:e*2;
    }
    return 0;
}

Run as:

./a.out "sin(x)/x" 0
./a.out "1/(x*x)" 0
./a.out "(x*x-x)/(x-1)" 1
./a.out "pow(2,x)" 3

Results:

1.000000
+INF
1.000000
8.000000
Source Link
baby-rabbit
  • 1.8k
  • 10
  • 12

C, 371 bytes

Plenty of room for golfing, just proof of concept to begin with :P

Compiles under clang, osx.

Note: For linux you may have to change the code to use something like: "ld -shared f.o -o f.so -lm"

#include <dlfcn.h>
#include <stdio.h>
float e,a,r;
t;main(int i,char**v) {
    // create and compile temporary c file for 'f(x)'
    FILE*file=fopen("f.c","w");
    fprintf(file,"#include<math.h>\n");
    fprintf(file,"float f(float x){return %s;}\n",v[1]);
    fclose(file);
    system("cc -fPIC -c -Wall f.c");
    system("ld -bundle f.o -o f.so -lm");
    float (*f)(float)=dlsym(dlopen("f.so",0),"f");
    
    sscanf(v[2],"%f",&a);
    
    while(1) {
        r=f(e+a);
        if(isinf(r))t=1;else if(!isnan(r)){
            t?printf("%cINF\n",r>0?43:45):printf("%f\n",r);
            break;
        }
        e=(e==0)?1e-37:e*2;
    }
    return 0;
}

Run as:

./a.out "sin(x)/x" 0
./a.out "1/(x*x)" 0
./a.out "(x*x-x)/(x-1)" 1
./a.out "pow(2,3)" 1

Results:

1.000000
+INF
1.000000
8.000000