当我尝试执行代码时,出现错误,告诉我“值超出范围”。
program solveQuadratic;
#include( "stdlib.hhf" );
static
a: real32;
b: real32;
c: real32;
discriminant: real32;
root1: real32;
root2: real32;
begin solveQuadratic;
// Prompt and read values for a, b, and c
stdout.put("Gimme a value for a: ");
stdin.get(a);
stdout.put("Gimme a value for b: ");
stdin.get(b);
stdout.put("Gimme a value for c: ");
stdin.get(c);
// Calculate the discriminant
fld(b);
fmul(st0, st1); // b^2
fld(a);
fld(c);
fmul(st0, st1); // a * c
fadd(st0, st1); // 2ac
fsub(); // b^2 - 4ac
fstp(discriminant);
// Compute the roots
fld(discriminant);
fsqrt();
fstp(root1); // store sqrt(discriminant) in root1
// Calculate the positive root
fld(b);
fchs(); // -b
fld(root1);
fadd(); // -b + sqrt(discriminant)
fld(a);
fadd(st0, st0); // 2a
fdiv(); // (-b + sqrt(discriminant)) / (2a)
fstp(root1);
// Calculate the negative root
fld(b);
fchs(); // -b
fld(root1);
fsub(); // -b - sqrt(discriminant)
fld(a);
fadd(st0, st0); // 2a
fdiv(); // (-b - sqrt(discriminant)) / (2a)
fstp(root2);
// Output the roots
stdout.put("x is ", root1:0:5, " and x is also ", root2:0:5, "\n");
end solveQuadratic;
我期待这个输出
给出 a 的值: 2.1
给出 b 的值: 3.1
给出 c 的值: -5.0
x 是 -2.44857,x 也是 0.97238
但我得到的只是一个转换错误,我不知道为什么。