I was trying to solve a coupled first order differential equations using the 4 points Range-Kutta method. When outputting the values of m, I get the -1.#IND0 error. I am aware that this can be NaN, but it doesn't make sense to me, because the value of m should be increasing and I get -1IND0 in between valid values. Here is sample of my output:
3110047776596300800000000000000000000.00000 35953700.00 -1.#IND0 35984000.00 -1.#IND0 36013700.00 3721056749337648900000000000000000000.00000 36042800.00 -1.#IND0 36071400.00 4132402773947312100000000000000000000.00000 36099500.00 -1.#IND0 36127200.00 4546861919240663800000000000000000000.00000 36154400.00 and here is my code:
#include <stdio.h> #include <stdlib.h> #include <math.h> #define pi 3.141592654 double f(double p, double m, double r) { return -0.000000000000000012812899255404507 * m * pow(p, 1.0/3) / (r * r); } double g(double p, double r) { return 4 * pi * r * r * p; } int main() { double p_c, //central density p, //densities m, //masses f_val[4], //arrayed f g_val[4], //arrayed g r = 1e-15, //radius dr = 100, //radius increment p_0 = 0.001; //effective zero density double p_min = 1e6; double p_max = 1e14; int i; //Loop counter FILE *data=fopen("dwarf.txt", "w");//Output file for(p_c = p_min; p_c <= p_max; p_c += (p_max - p_min) / 100) { p = p_c; m = (4.0/3) * pi * r * r * r * p_c; while(p > p_0) { //fprintf(data, "%.5lf %.2lf %.2lf\n", p, m, r); f_val[0] = f(p, m, r) * dr; g_val[0] = g(p, r) * dr; f_val[1] = f(p + f_val[0]/2, m + g_val[0]/2, r + dr/2) * dr; g_val[1] = g(p + f_val[0]/2, r + dr/2) * dr; f_val[2] = f(p + f_val[1]/2, m + g_val[1]/2, r + dr/2) * dr; g_val[2] = g(p + f_val[1]/2, r + dr/2) * dr; f_val[3] = f(p + f_val[2], m + g_val[2], r + dr) * dr; g_val[3] = g(p + f_val[2], r + dr) * dr; m += (g_val[0] + 2 * g_val[1] + 2 * g_val[2] + g_val[3]) / 6; p += (f_val[0] + 2 * f_val[1] + 2 * f_val[2] + f_val[3]) / 6; r += dr; } fprintf(data, "%.5lf %.2lf\n", m, r); printf("%.5lf %.2lf\n", m, r); } exit; }
-0.000000000000000012812899255404507That's fully 16 significant figures. It's not obvious that that standard (double) literal will support every bit of that precision. Using-0.000000000000000012812899255404507Lwill get a bit more on platforms wherelong doubleis longer thandouble. (Which you might or might not want, depending...)const double pi = 4.0 * atan(1.0).