使用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)
错误:
那么我尝试引用包,但也失败了:
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)
错误:
我不知道你是否已经修好了,但这是答案.
首先,正如我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)