你如何编码来确定Ada中值的对数?

mat*_*eek 0 logarithm ada

使用Ada(GNAT):我需要确定给定值的10的幂.最明显的方法是使用对数; 但是无法编译.

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;
Run Code Online (Sandbox Code Playgroud)

错误:

  • 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;
Run Code Online (Sandbox Code Playgroud)

错误:

  • 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行为实例

Xan*_*ndy 5

我不知道你是否已经修好了,但这是答案.

首先,正如我Float在实例化通用版本时所看到的那样,您可以使用非通用版本.

如果您决定使用通用版本,则必须以第二种方式执行此操作,您必须在使用其功能之前实例化该包.

看一下a-ngelfu.ads你可能会看到你需要的函数的实际原型(只有一个参数的自然对数有另一个函数):

function Log(X, Base : Float_Type'Base) return Float_Type'Base;
Run Code Online (Sandbox Code Playgroud)

你可以看到基地也需要浮点型.通用版本的正确代码是:

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;
Run Code Online (Sandbox Code Playgroud)

非通用的将是完全相同的:

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;
Run Code Online (Sandbox Code Playgroud)