This code exemplifies the problem in a much simpler way (sorry for the previous one, it's too complicated). Anyway, note that out_b will print out correctly for any values of index between 0 to 958. The variable out_a, however, will always print out 36893488147419103232. And they're both of type DOUBLE. It looks like the + operation messes up the type of out_a. It won't work even if appendix is of type DOUBLE.
#include <stdio.h>
#include <stdlib.h>
#include <gsl/gsl_math.h>
#include <string.h>
double main (int argc, char *argv[]) {
char *wrapper = "11111111111111111111111111111111111111111111111111111111111111111"; // 65 digits
int appendix = atoi(argv[1]);
int length = strlen(wrapper);
double out_a = gsl_pow_int(2,length) + appendix;
double out_b = gsl_pow_int(2,length + appendix);
printf("%.0lf %.0lf\n", out_a, out_b);
}
Original question
This C program would compute the decimal equivalent to any binary input, as long as it's less than 64 digits... I fail to see why. Any help appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <gsl/gsl_math.h>
#include <string.h>
long double main (int argc, char *argv[]) {
char *wrapper = argv[1];
static int *array, length, llo, i;
static long double sum;
while ( *wrapper && ( *wrapper == '0' ) ) wrapper++;
length = strlen(wrapper);
llo = length - 1;
array = malloc((length*sizeof(*array))+1);
for ( i = 0; i < length; i++ ) {
if ( wrapper[i] >= '0' && wrapper[i] <= '1' ) {
array[i] = wrapper[llo-i] - 48;
sum += array[i] * gsl_pow_int(2,i);
}
else printf("Some error.\n");
}
free(array);
printf("%.0Lf\n", sum);
}