|
> 今、二つの値を比べて有効数字が何桁まで一致しているかを調べるプログラム
> を作っているのですが、どんなアルゴリズムを用いればよいのでしょうか?
こんなのはいかがですか?
#include <stdio.h>
#include <math.h>
int precision(double a, double b)
{
return (a == b) ? 16 : -(int)log10(fabs((a - b) / b));
}
int main(void)
{
double a, b, c;
while (scanf("%lf%lf", &a, &b) == 2)
printf("%d\n", precision(a, b));
return 0;
}
|