首页 文章

Ada 95 - 子程序中的while循环给出输出*** inf ***

提问于
浏览
-1

Desciption: 我在Ada 95中创建的程序假设根据用户的输入输出一个Tax-list,如下所示:

价格不含税|税|含税价格

如果用户输入 First Price: 10Last Pris: 20, Steps: 0.5和 Tax 作为整数 . 然后程序从10到20迭代,步长为0.5,然后停止 .

我面临的问题是“税收”和“含税价格”的输出值 . 我得到的结果是两列上的**** inf **** . 我不知道这实际意味着什么,我也找不到任何其他信息 . 所以我做了另一次尝试并获得了我想要的结果,除了迭代中的第一行 . 它从Value 10 Taxes = 0开始,最后一个Pris的税收= 0,但所有其他列和行都是正确的 .

是不是假设它与子程序的工作方式与主循环中的循环一样?我在另一个文件中有完全相同的程序,区别在于它全部在Main中,而不是在子程序中 .

有没有其他人经历过这个并知道如何解决它?

--------代码----------------------------------------- -------------

with Ada.Text_IO;          use Ada.Text_IO;
with Ada.Integer_Text_IO;  use Ada.Integer_Text_IO;  
with Ada.Float_Text_IO;    use Ada.Float_Text_IO;

procedure Momstabell is

-- First Price, Last Price, Steps & Taxes(in Integer),- Subprogram

procedure Hamta(S : in String;
        Tal : out Float ) is

begin


    Put(S);
    loop
        Get(Tal); 
        exit when Tal > 0.0;
    end loop;


 end Hamta;

procedure Skriv_Ut(Forsta_Pris, Sista_Pris, Steg, Momsprocent : in 
Float) is

  First_Price, Last_Price, Steps, Price_With_Tax, Tax_Percentage, Tax : 
  Float;

begin

    New_Line;
    Put("=== Price List ===");New_Line;
    Put("Price without tax ");
    Put("Tax ");
    Put("Price with tax");

    -- Local Variables --
    First_Price := Forsta_Pris;
    Last_Price := Sista_Pris;
    Steps := Steg;
    Tax_Percentage := Momsprocent;

    Tax := Tax_percentage/First_Price;
    Price_With_Tax := First_Price + Tax;

  while First_Price <= Last_Price loop

      New_Line;
      Put("    ");
      Put(First_Price, Aft => 2, Exp => 0);
      Put("     ");
      Put(Tax, Aft => 2, Exp => 0);
      Put("     ");
      Put(Price_With_Tax, Aft => 2, Exp => 0);

 -- Iteration --

      First_Price := First_Price + Steps;
      Tax := First_Price / Tax_Percentage;
      Price_With_Tax := First_Price + Tax;


  end loop;

end Skriv_Ut;

-- Deklaration of Variables

Forsta_Pris: Float;
Sista_Pris: Float;
Steg: Float;
Momsprocent: Float;
Moms : Float;

begin

    Hamta("Mata in första pris: ",Forsta_Pris);
    Hamta("Mata in sista pris: ", Sista_Pris);
    Hamta("Mata in Steg: ",Steg);
    Hamta("Mata in Momsprocent: ",Moms);
    Skriv_Ut(Forsta_Pris, Sista_Pris, Steg, Momsprocent);

end Momstabell;

最好的问候Robert

2 回答

  • 1

    如果你在常规计算中得到浮点无穷大,你很可能除以零 .

    此外,如果你需要得到的金额与一分钱相匹配,你不能使用浮动钱 .

  • 2

    当我编译你的代码时(使用 -gnatl 将编译器消息与代码列表交错),我得到了

    70. Steg: Float;
    71. Momsprocent: Float;
        |
        >>> warning: variable "Momsprocent" is read but never assigned
    
    72. Moms : Float;
    

    为了避免使用调试器,我将 Skriv_Ut 中的声明更改为

    subtype My_Float is Float range Float'Range;
    First_Price, Last_Price, Steps, Price_With_Tax, Tax_Percentage, Tax :
      My_Float;
    

    因为GNAT不检测浮点数字溢出 - 此代码将检测Inf(可能还有NaN)结果 .

    运行程序给出

    $ ./momstabell
    Mata in första pris: 10
    Mata in sista pris: 20
    Mata in Steg: .5
    Mata in Momsprocent: 5
    
    === Price List ===
    Price without tax Tax Price with tax
        10.00      0.00     10.00
    
    raised CONSTRAINT_ERROR : englund.ada:59 range check failed
    

    第59行是

    58.       First_Price := First_Price + Steps;
    59.       Tax := First_Price / Tax_Percentage;
    60.       Price_With_Tax := First_Price + Tax;
    

    我认为你的问题是这两行:

    Hamta("Mata in Momsprocent: ",Moms);
    Skriv_Ut(Forsta_Pris, Sista_Pris, Steg, Momsprocent);
    

相关问题