Ada:浮点数的一部分/固定为整数 [英] Ada: Fraction part of Float/Fixed as Integer
本文介绍了Ada:浮点数的一部分/固定为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
pre code整数integer: = 12;
分数:整数:= 345;
整个部分很简单,但是我不知道如何获得Fraction部分。我开始的想法是:
pre $ 12.345 mod整数12.345
$ b $ p
$ b
$ b
将返回0.345,并且可以乘以相反的幅度(在这种情况下×1000),但是I不知道如何计算数字的数量。
解决方案
不是一个完整的答案,但这得到的小数部分作为字符串,以便进一步操作将其作为整数进行检索:
with Ada.Text_Io;
程序剩余部分是
包Fio是新的Ada.Text_IO.Float_IO(Float);
X:常数浮点数:= 12.345;
X_Int:常量整数:=整数(X);
X_Rem:常量Float:= Float'Remainder(X,Float(X_Int));
begin
Fio.Put(X_Rem,Aft => 6,Exp => 0);
余额;
In Ada, I would like to separate out a number like 12.345 out into two distinct integers:
whole : integer := 12;
fraction : integer := 345;
The Whole part is easy, but I don't know how to get the Fraction part.
My starting idea is:
12.345 mod Integer(12.345)
which will return 0.345, and could be multiplied by the inverse magnitude (in this case ×1000), but I don't know how to count the amount of digits either.
解决方案
Not a full Answer but this gets the fractional part as a string to enable further manipulation to retreive it as an integer:
with Ada.Text_Io;
procedure Remainder is
package Fio is new Ada.Text_IO.Float_IO(Float);
X : constant Float := 12.345;
X_Int : constant Integer := Integer (X);
X_Rem : constant Float := Float'Remainder(X,Float (X_Int));
begin
Fio.Put (X_Rem, Aft => 6, Exp => 0);
end Remainder;
这篇关于Ada:浮点数的一部分/固定为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文