如何编写代码来确定 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;
错误:
- 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:没有候选解释与实际情况匹配:
- 实用程序.adb:495:41:调用“Log”
- 实用程序时参数过多。adb:495:53:预期类型“Standard.Float”
- 实用程序。adb:495:53:找到类型通用整数==>在 a-ngelfu.ads:24 处调用“Log”,例如第 482 行
Using Ada (GNAT): I need to determine the power of ten for a given value. The most obvious approach is to use a logarithm; but that fails to compile.
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;
error:
- utilities.adb:495:26: "Log" is not visible
- utilities.adb:495:26:
non-visible declaration at a-ngelfu.ads:24, instance at line 482 - utilities.adb:495:26:
non-visible declaration at a-ngelfu.ads:23, instance at line 482
- utilities.adb:495:26:
So then I attempt to refer to the package, but that also fails:
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;
error:
- utilities.adb:495:41: no candidate interpretations match the actuals:
- utilities.adb:495:41: too many arguments in call to "Log"
- utilities.adb:495:53: expected type "Standard.Float"
- utilities.adb:495:53: found type universal integer ==> in call to "Log" at a-ngelfu.ads:24, instance at line 482
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道你是否已经修复了它,但这就是答案。
首先,正如我所见,您在实例化通用版本时传递了
Float
,您可以使用非通用版本。如果您决定使用通用版本,则必须按照第二种方式进行操作,必须在使用包的功能之前实例化该包。
查看a-ngelfu.ads,您可能会看到所需函数的实际原型(还有另一个仅具有 1 个参数的自然对数函数):
您可以看到底数需要为也是浮动类型。通用版本的正确代码是:
非通用版本完全相同:
I don't know if you fixed it already or not, but here is the answer.
First of all, as I see you're passing
Float
when instantiating the generic version you may use the non generic one instead.If you decide to use the generic version you have to do it the second way you did, you have to instantiate the package before you use its functions.
Looking at a-ngelfu.ads you may see the actual prototype of the function you need (there is another function for the natural logarithm with just 1 parameter):
You can see there that the base needs to be in a float type too. The correct code for the generic version would be:
The non-generic one would be exactly the same:
克桑迪是对的。他的解决方案奏效了。
有两个例外需要防范......
如果没有防护,此函数将导致生成异常。并记住返回的 The_Log 可以是 < 0、0和> 0。
Xandy is right. His solution worked.
However being Ada there were two exceptions to guard against...
Without guards this function as it is causes exceptions to be generated. And remember The_Log as returned can be < 0, 0, and > 0.