/* (x-1.23456789e10)*(x-1.23456789e-10) = 0
* 上記式を展開して得られる
* x^2 -1.23456789e10x +1.52415788 = 0
* を例にとる。
*/
#include <stdio.h>
#include <math.h>
int main(void) {
double a,b,c;
double x1,x2;
a = +1.0;
b = -1.23456789e10;
c = +1.52415788;
/* 解の公式 */
x1
= (-b
+sqrt(b
*b
-4.0*a
*c
))/(2.0*a
); x2
= (-b
-sqrt(b
*b
-4.0*a
*c
))/(2.0*a
);
/* 改良版 */
if (b
>0) x1
= (-b
-sqrt(b
*b
-4.0*a
*c
))/(2.0*a
); else x1
= (-b
+sqrt(b
*b
-4.0*a
*c
))/(2.0*a
); /* ここで (x-x1)(x-x2) = 0 を考える。
* これを展開すると
* x^2 -(x1+x2)x +x1x2 = 0
* だから、加減算の入らない最終項の x1x2 を使用する。
* このケースでは x1x2 = c(1.52415788)だから・・・
*/
x2 = c / x1;
return 0;
}
LyogKHgtMS4yMzQ1Njc4OWUxMCkqKHgtMS4yMzQ1Njc4OWUtMTApID0gMAogKiDkuIroqJjlvI/jgpLlsZXplovjgZfjgablvpfjgonjgozjgosKICogeF4yIC0xLjIzNDU2Nzg5ZTEweCArMS41MjQxNTc4OCA9IDAKICog44KS5L6L44Gr44Go44KL44CCCiAqLwojaW5jbHVkZSA8c3RkaW8uaD4KI2luY2x1ZGUgPG1hdGguaD4KaW50IG1haW4odm9pZCkgewogICAgZG91YmxlICBhLGIsYzsKICAgIGRvdWJsZSAgeDEseDI7CiAgICBhID0gKzEuMDsKICAgIGIgPSAtMS4yMzQ1Njc4OWUxMDsKICAgIGMgPSArMS41MjQxNTc4ODsKICAgIAogICAgLyog6Kej44Gu5YWs5byPICovCiAgICB4MSA9ICgtYitzcXJ0KGIqYi00LjAqYSpjKSkvKDIuMCphKTsKICAgIHgyID0gKC1iLXNxcnQoYipiLTQuMCphKmMpKS8oMi4wKmEpOwogICAgcHJpbnRmKCLop6Pjga7lhazlvI9cbiIpOwogICAgcHJpbnRmKCIgeDEgOiAlZVxuIix4MSk7CiAgICBwcmludGYoIiB4MiA6ICVlXG4iLHgyKTsKICAgIAogICAgLyog5pS56Imv54mIICovCiAgICBpZiAoYj4wKSB4MSA9ICgtYi1zcXJ0KGIqYi00LjAqYSpjKSkvKDIuMCphKTsKICAgIGVsc2UgICAgIHgxID0gKC1iK3NxcnQoYipiLTQuMCphKmMpKS8oMi4wKmEpOwogICAgLyog44GT44GT44GnICh4LXgxKSh4LXgyKSA9IDAg44KS6ICD44GI44KL44CCCiAgICAgKiDjgZPjgozjgpLlsZXplovjgZnjgovjgagKICAgICAqICB4XjIgLSh4MSt4Mil4ICt4MXgyID0gMAogICAgICog44Gg44GL44KJ44CB5Yqg5rib566X44Gu5YWl44KJ44Gq44GE5pyA57WC6aCF44GuIHgxeDIg44KS5L2/55So44GZ44KL44CCCiAgICAgKiDjgZPjga7jgrHjg7zjgrnjgafjga8geDF4MiA9IGMoMS41MjQxNTc4OCnjgaDjgYvjgonjg7vjg7vjg7sKICAgICAqLwogICAgeDIgPSBjIC8geDE7CiAgICBwcmludGYoIuaUueiJr+eJiFxuIik7CiAgICBwcmludGYoIiB4MSA6ICVlXG4iLHgxKTsKICAgIHByaW50ZigiIHgyIDogJWVcbiIseDIpOwogICAgCiAgICByZXR1cm4gMDsKfQo=