2010-02-10 28 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