如何编写代码来确定 Ada 中某个值的对数?

发布于 2024-08-21 17:30:00 字数 1060 浏览 13 评论 0原文

使用 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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

━╋う一瞬間旳綻放 2024-08-28 17:30:00

我不知道你是否已经修复了它,但这就是答案。

首先,正如我所见,您在实例化通用版本时传递了 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;

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):

function Log(X, Base : Float_Type'Base) return Float_Type'Base;

You can see there that the base needs to be in a float type too. The correct code for the generic version would be:

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;

The non-generic one would be exactly the same:

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;
朕就是辣么酷 2024-08-28 17:30:00

克桑迪是对的。他的解决方案奏效了。

有两个例外需要防范......

  • 然而,作为 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; 

Xandy is right. His solution worked.

However being Ada there were two exceptions to guard against...

  • Value < 0.0
  • Value = 0.0

Without guards this function as it is causes exceptions to be generated. And remember The_Log as returned can be < 0, 0, and > 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; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文