numbers - 如何仅使用 Ada 中的 Image 功能来控制显示的小数位数?

标签 numbers decimal display ada

我在 Ada 中有以下代码行,

     Put_Line ("Array of " & Integer'Image (iterations)
        & "          is " & Long_Float'Image (sum) 
        & " Time = " & Duration'Image(milliS) & timescale);  
sum 中的小数位数太长而无法显示(不适用于计算,因为 sum 计算需要长浮点数)。我知道 Ada 有使用前后显示小数的替代方法,而不使用 Image 函数,但在我切换到替代方法之前,我想知道 Image 是否有选项或其他显示小数的技术。 Image 函数有显示小数的选项吗?是否有一种技术可以缩短 Long_Float 的小数位数仅用于显示?
with Ada.Numerics;
 with Ada.Text_IO; use Ada.Text_IO;

 procedure Images is
 sum                : Standard.Long_Float;
 Pi                 : Long_Float := Ada.Numerics.Pi;

  type Fixed is delta 0.001 range -1.0e6 .. 1.0e6;
  type NewFixed is range -(2 ** 31) .. +(2 ** 31 - 1);
  type Fixed2 is new Long_Float range -1.0e99.. 1.0e99;
  type Fixed3 is new Long_Float range -(2.0e99) .. +(2.0e99);


 begin
 sum:=4.99999950000e14;
 Put_Line ("no fixing number: " & Pi'Image);
 Put_Line (" fixed number: " & Fixed'Image(Fixed (Pi)));
 Put_Line ("no fixing number: " & Long_Float'Image(sum));
 Put_Line (" testing fix: " & Fixed3'Image(Fixed3 (sum)));
 end Images;
附录:
  • 请注意,我的变量 sum 被定义为 Standard.Long_Float 以与整个程序中使用的其他变量一致。
  • 我正在添加代码来显示我的问题的罪魁祸首以及我解决问题的尝试。它基于 Simon Wright 提供的示例,我添加了总和数。看起来我只需要弄清楚如何将 delta 插入 Fixed3 类型,因为 delta 定义了小数位数。
  • 最佳答案

    ’Image没有任何选项,见 ARM2012 3.5(35) (还有 (55.4) )。
    然而,Ada 202x ARM K.2(88)4.10(13)建议一个替代方案:

    with Ada.Numerics;
    with Ada.Text_IO; use Ada.Text_IO;
    procedure Images is
       Pi : Long_Float := Ada.Numerics.Pi;
       type Fixed is delta 0.001 range -1.0e6 .. 1.0e6;
    begin
       Put_Line (Pi'Image);
       Put_Line (Fixed (Pi)'Image);
    end Images;
    
    其中报告(GNAT CE 2020,FSF GCC 10.1.0)
    $ ./images 
     3.14159265358979E+00
     3.142
    

    关于numbers - 如何仅使用 Ada 中的 Image 功能来控制显示的小数位数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66765905/

    相关文章:

    java - 如何获取数字小数部分的长度?

    C - 读取一串数字

    excel - 多种格式的小数位

    python-3.x - MatPlotLib美元符号与数千个逗号刻度标签

    reactjs - 如何使用钩子(Hook)显示和隐藏 react 组件

    r - 数字格式,写 1e-5 而不是 0.00001

    powershell - Powershell-数字格式显示错误的结果

    java - double -> 提取整数

    ruby-on-rails - 在 Rails 中生成随机固定小数

    html - 图像旁边的文字在调整大小时移动