您如何在Ada中求平方根?

安静

因此,我得到了一个分配,以读取一个文件,将数字放入两个矩阵中,再将矩阵相乘,最后将输出结果放入.txt文件中。

我以前从未使用过Ada,所以我认为这将是一个很好的挑战。我陷入试图确定两个单独的数组的界限。

这是我目前拥有的:

currentSpread := I;
g := Ada.Numerics.Generic_Complex_Elementary_Functions.Sqrt(I);
while J < g loop

  if(I mod J = 0) THEN
     if(currentSpread > ((I/J - J)/2)) THEN
        currentSpread := ((I/J - J)/2);
        arrayBounds := J;
     end if;

  end if;

  J := J + 1;
end loop;

我遇到的问题是sqrt函数。我想找到矩阵乘法最佳边界的因素,这是我认为实现该矩阵的唯一方法。

我得到的错误是:

invalid prefix in selected component "Ada.Numerics.Generic_Complex_Elementary_Functions"

非常感谢您的帮助。

- 更新

根据要求提供完整代码:

with Ada.Text_IO;use Ada.Text_IO;
with Ada.Integer_Text_IO;
with Ada.Numerics.Generic_Complex_Elementary_Functions;
with Ada.Numerics.Generic_Elementary_Functions;
with Ada.Numerics.Complex_Elementary_Functions;
with Ada.Numerics.Generic_Complex_Types;

procedure Main is

   dataFile : File_Type;
   resultFile : File_Type;
   value : Integer;
   I : Integer := 0;
   J : Integer;
   currentSpread : Integer;
   arrayBounds : Integer;
   g : Integer;

begin

  Ada.Text_IO.Open(File => dataFile, Mode => Ada.Text_IO.In_File, Name =>"C:\Users\Jeffrey\Desktop\data.txt");


   while not End_Of_File(dataFile) loop
      Ada.Integer_Text_IO.Get(File => dataFile, Item => value);
      Ada.Integer_Text_IO.Put(Item => value);
      Ada.Text_IO.New_Line;
      I := I + 1;
   end loop;

   Ada.Integer_Text_IO.Put(I);
   I := I/2;
   J := 1;
   currentSpread := I;
   g := Ada.Numerics.Generic_Complex_Elementary_Functions.Sqrt(I); 
   while J < g loop

      if(I mod J = 0) THEN
         if(currentSpread > ((I/J - J)/2)) THEN
            currentSpread := ((I/J - J)/2);
            arrayBounds := J;
         end if;

      end if;

        J := J + 1;

   end loop;

   declare
     type newArray is array(Integer range <>, Integer range<>) of Integer;
      X : Integer := J;
      Y : Integer := I/J;
      Arr1 : newArray(1..Y, 1..X);
      Arr2 : newArray(1..X, 1..Y);
      finAnswer : newArray(1..X, 1..X);
   begin

      for z in 1 .. X loop
         for k in 1 .. Y loop
            Ada.Integer_Text_IO.Get(File => dataFile, Item => value);
            Arr1(z, k) := value;
         end loop;
      end loop;

      for z in 1 .. Y loop
         for k in 1 .. X loop
            Ada.Integer_Text_IO.Get(File => dataFile, Item => value);
            Arr2(z, k) := value;
         end loop;
      end loop;

      for l in 1 .. X loop
         for m in 1 .. Y loop
            for n in 1 .. X loop
               finAnswer(l, n) := finAnswer(l, n) + Arr1(l, n)* Arr2(n, m);
            end loop;
         end loop;
      end loop;
   end;



   Ada.Text_IO.Close(File => dataFile);
end Main;

我仅使用平方根来找出数字的因素。我现在的设置方式是上升到平方根,然后扩散最小的因素。我不在乎四舍五入的错误,如果它不是一个完美的正方形,也可以用任何一种方法进行四舍五入。

谢谢。

Generic_Complex_Elementary_Functions是一个通用软件包。不能直接使用。这就是为什么编译器在此行上给您一个错误的原因:

Ada.Numerics.Generic_Complex_Elementary_Functions.Sqrt(I);

要使用它,必须使用要使用Generic_Complex_Types的浮点类型实例化泛型,然后Generic_Complex_Elementary_Functions使用实例实例化Generic_Complex_Types幸运的是,如果您愿意使用内置类型,则不必进行所有操作Float该语言Ada.Numerics.Complex_Elementary_Functions为您提供了使用,可以Float用作浮点类型,也Ada.Numerics.Long_Complex_Elementary_Functions可以用于Long_Float您的编译器供应商是否支持的语言。

但是,我认为您不想使用Complex任何东西。那涉及到复数,我怀疑您想使用那些。使用Ada.Numerics.Elementary_Functions(或Long_Elementary_Functions)处理实数。

最后,即使这样也行不通:

Ada.Numerics.Elementary_Functions.Sqrt(I)

ifI是整数,因为参数类型需要为Float您必须使用类型转换。

Ada.Numerics.Elementary_Functions.Sqrt(Float(I))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章