我在 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;
附录:最佳答案
’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/