2010-02-10 25 views
0

使用Ada(GNAT):我需要確定給定值的十次冪。最明顯的方法是使用對數;但是沒有編譯。您如何編碼以確定Ada中某個值的對數?

with Ada.Numerics.Generic_Elementary_Functions; 
procedure F(Value : in Float) is 
     The_Log : Integer := 0; 
begin 
     The_Log := Integer(Log(Value, 10)); 
     G(Value, The_Log); 
end; 

錯誤:

  • utilities.adb:495:26: 「日誌」 是不可見的
    • utilities.adb:495:26: 不可見的A-聲明ngelfu.ads:24,實例在線482
    • utilities.adb:495:26: 不可見聲明在a-ngelfu.ads:23,實例在線482

於是我試圖指包,但也失敗:

with Ada.Numerics.Generic_Elementary_Functions; 
procedure F(Value : in Float) is 
     The_Log : Integer := 0; 
     package Float_Functions is new Ada.Numerics.Generic_Elementary_Functions (Float); 
begin 
     The_Log := Integer(Float_Functions.Log(Value, 10)); 
     G(Value, The_Log); 
end; 

錯誤:

  • utilities.adb:495:41:沒有候選人解釋符合實際情況:
  • utilities.adb:495:41:調用「Log」的參數太多
  • utilities.adb:495:53:預計類型「Standard.Float」
  • utilities.adb:495:53:找到類型通用整數==>在a-ngelfu.ads:24調用「Log」例如在線482

回答

5

我不知道你是否已經修好它,但這裏是答案。

首先,當我看到你在通過Float實例化通用版本時,你可能會使用非通用版本。

如果您決定使用通用版本,則必須按照第二種方式進行操作,您必須在使用其功能之前實例化該軟件包。

看着a-ngelfu.ads您可能會看到你所需要的功能的實際原型(還有另外一個功能,自然對數只有1個參數):

function Log(X, Base : Float_Type'Base) return Float_Type'Base; 

你可以看到有該基地也需要在浮動類型。對於仿製藥的正確的代碼是:

with Ada.Numerics.Generic_Elementary_Functions; 

procedure F(Value : in Float) is 
    -- Instantiate the package: 
    package Float_Functions is new Ada.Numerics.Generic_Elementary_Functions (Float); 
    -- The logarithm: 
    The_Log : Integer := 0; 
begin 
    The_Log := Integer(Float_Functions.Log(Value, 10.0)); 
    G(Value, The_Log); 
end; 

非一般人會是一模一樣的:

with Ada.Numerics.Elementary_Functions; 

procedure F(Value : in Float) is 
    -- The logarithm: 
    The_Log : Integer := 0; 
begin 
    The_Log := Integer(Ada.Numerics.Elementary_Functions.Log(Value, 10.0)); 
    G(Value, The_Log); 
end; 
3

Xandy是正確的。他的解決方案奏效

不過是艾達有兩個例外謹防...

  • 值< 0。0
  • 值= 0.0

無人看守此功能,因爲它是導致異常產生。並且記住The_Log返回的結果可以是< 0,0和> 0.

with Ada.Numerics.Elementary_Functions; 

procedure F(Value : in Float) is 
    -- The logarithm: 
    The_Log : Integer := 0; 
begin 
    if Value /= 0.0 then 
     The_Log := Integer(
      Ada.Numerics.Elementary_Functions.Log(abs Value, 10.0)); 
    end if; 
    G(Value, The_Log); 
end; 
+0

+1,您是對的。我只修正了編譯錯誤,沒有意識到這一點。 – Xandy 2010-02-11 18:50:57