题
使用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;
错误:
- 公用事业。adb:495:26:"记录"是不可见的
- 公用事业。adb:495:26:不可视宣言》在ngelfu.广告:24,例如在线482
- 公用事业。adb:495:26:不可视宣言》在ngelfu.广告: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;
错误:
- 公用事业。adb:495:41:没有任何候选人的解释相匹配的实际值:
- 公用事业。adb:495:41:太多的论点称为"记录"
- 公用事业。adb:495:53:预期类型"的标准。Float"
- 公用事业。adb:495:53:找到类型普遍整数==>在调用"记录"在ngelfu.广告:24,例如在线482
解决方案
我不知道如果你修好它已经或不是,但这里是答案。
首先,正如我看见你穿 Float
当实例的普通版本可以使用不通用的一种替代。
如果你决定使用通用的版本你必须这样做的第二种方法,你没有,你有的实例包在你之前使用其职能。
看 a-ngelfu.广告 你可以看到的实际原型的功能你需要(还有另一种功能的自然对数只有1parameter):
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;
其他提示
Xandy是正确的。他的解决方案的工作。
然而正在Ada有两种例外情况,以防止...
- 值 < 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;
不隶属于 StackOverflow