Hallo gawiellus
I think it may be better you compare the two double values with given number of decimals where the values are compared.
example:
Code:
#include <stdio.h>
#include <math.h>
/* usage: arg1=first double vbalue arg2= second double value arg3= precision decided
* Return value if integer not equal (example 345.456789 and 123.456789) is -1
* Return value if integer are equal but not decimals (example 123.456789 and 123.456897) 3
*
*/
int compdouble(double da, double db, int stellen)
{
int rewer = -1, nachint_a = 0, nachint_b = 0, i, faktor = 1, vka, vkb;
double nachkomma_a, vorkomma_a;
double nachkomma_b, vorkomma_b;
nachkomma_a = std::modf(da, &vorkomma_a);
nachkomma_b = std::modf(db, &vorkomma_b);
vka = vorkomma_a;
vkb = vorkomma_b;
printf("decimals from first double.: %10lf\n", nachkomma_a);
printf("decimals from second double: %10lf\n", nachkomma_b);
if(vka == vkb)
for (i = 0; i <= stellen; i++)
{
faktor *= 10;
nachint_a = nachkomma_a * faktor;
nachint_b = nachkomma_b * faktor;
/* to use this function in other sources please remove this line or set it to a comment */
printf("%d decimals from first double =%d\n%d decimals from second double=%d\ni=%d\n", i, nachint_a, i, nachint_b, i);
if(nachint_a != nachint_b) break;
}
else return rewer;
rewer = i;
return rewer;
}
int main(int argc, char **argv)
{
int erge = 0;
double da = 123.456789, db=123.4568997;
/*
printf("Please enter a double-value.......: ");
scanf("%lf", &da);
printf("Please enter a second double-value: ");
scanf("%lf", &db);
*/
printf("starting compdouble\n");
erge = compdouble(da, db, 4); /* compares double values 4 decimals (after decimal point) */
printf("end of compdouble\n");
printf("decimals were da and db are equal: %d\n", erge);
printf("End of Program\n");
return 0;
}