Fun with TCC and C-scripting

Fabrice Bellards superfast tiny C Compiler TCC can be invoked from scripts, just as shell scripts. This makes C possible to use as kind of a scripting language.

All you need is a valid shebang #!/usr/bin/tcc -run in the head of your code. Chmod your file to an executable and run like ./myprog.c

Let's calculate the spherical distance between two points a couple of times:

#!/usr/bin/tcc -run

#include <math.h>
#include <stdio.h>
#define DEG_TO_RAD 0.0174532925199432958

// converts degrees to the radian equivalent
float degToRad(float v){
	return DEG_TO_RAD * v;
}

// spherical distance between two points, given by their geo-coordinates
float sphericalDistance(float radius, float long1, float lat1, float long2, float lat2){ 
	
	lat1 = degToRad(lat1); 
	lat2 = degToRad(lat2); 
	long2 = degToRad(long2); 
	long1 = degToRad(long1);
	float d = radius * acos( sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(long2 - long1) ); 
        return d;
}

int main() { 
	int i; 

    // some calculations just for performance testing
	int calculations = 10000; 
        
    // earth radius in km
    float radius = 6378.388;
 
    // distance in km 
    float d = 0;
        
    // geo coords 
	float longitude1 = 11.575556; 
	float latitude1 = 48.137222;
	float longitude2 = 9.993333; 
	float latitude2 = 53.550556;
	
	// do 10.000 calculations of the same spherical distance
	for (i=0; i < calculations; i++){ 
		d = sphericalDistance(radius, longitude1, latitude1, longitude2, latitude2); 
		printf("%f\n", d); 
	} 

    return 0;
}