(2次方程式を解くサンプル実行例) > java qequ 1 5 6 1.0x^2 + 5.0x + 6.0 = 0 : x = -3.0 , -2.0 (実数解) > java qequ 1 6 9 1.0x^2 + 6.0x + 9.0 = 0 : x = -3.0 (重解) > java qequ 1 3 6 1.0x^2 + 3.0x + 6.0 = 0 : x = -1.5±1.9364917i (虚数解) (以下はコンパイラに処理させたソースコード) Sub main(argv[] as string) dim a,b,c as float //引数の数をチェック if (int)argv<3 then call println("usage : java qequ a b c") exit sub endif //係数 a = argv[0] b = argv[1] c = argv[2] //2次方程式なのでa!=0でないとこまる if a = 0 then call println("err a = 0") exit sub endif Call ResolveQuadricEquation(a,b,c) end sub //2次方程式の解を求める Sub ResolveQuadricEquation(a as float,b as float,c as float) dim d , r as float dim x[2] as float dim st as string //判別式 d = b*b - 4 * a * c //解の表示のための文字列 st = a + "x^2 + " + b + "x + " + c + " = 0 : x = " call print(st) if d > 0.0001 then //実数解が2つ r = sqr(d) x[0] = (-b-r) / (2 * a) x[1] = (-b+r) / (2 * a) call println(x[0] + " , " +x[1] + " (実数解)") elseif d < -0.0001 then //虚数解が2つ r = sqr(-d) x[0] = -b / (2 * a) x[1] = r / (2 * a) call println(x[0] + "±" +x[1] + "i (虚数解)") else //解は1つだけ x[0] = -b / (2 * a) call println(x[0] + " (重解)") endif end sub //平方根をニュートン法で大雑把に求める //RTClassを経由してMath.powを呼んでもよいが、せっかくなので自前でやってみる Function sqr(f as float) as float dim i as int dim x as float //x' = (x^2 + f) / (2 * x) を反復計算 x = f for i = 0 to 5 x = (x * x + f) / (2 * x) next return x end function