comp.lang.ada
 help / color / mirror / Atom feed
* How to round to the nearest fixed-point value?
@ 2014-01-22 16:48 Natasha Kerensikova
  2014-01-22 17:53 ` G.B.
                   ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Natasha Kerensikova @ 2014-01-22 16:48 UTC (permalink / raw)


Hello,

from what I understood of the LRM (at least in Ada 2005 mode),
Fixed_Point_Type'Round is supposed to be a function returning a
Fixed_Point_Type value closest to its argument.

So I'm a bit surprised by the behavior of the following code:


package Lib is
   type High is delta 0.001 digits 9;
   type Low is delta 0.01 digits 9;

   function Convert (Value : High) return Low;
end Lib;


package body Lib is

   function Convert (Value : High) return Low is
   begin
      return Low'Round (Value);
   end Convert;

end Lib;


with Ada.Text_IO;
with Lib;

procedure Testcase is
   Raw_Value : constant Lib.High := 0.999;
   Shown_Value : Lib.Low;
begin
   Ada.Text_IO.Put_Line (Lib.Low'Image (Lib.Low'Round (Raw_Value)));
   Shown_Value := Lib.Convert (Raw_Value);
   Ada.Text_IO.Put_Line (Lib.Low'Image (Shown_Value));
end Testcase;



I tried building it with gnat 4.6.3 from debian stable kfreebsd and with
gnat-aux 4.7.3 from FreeBSD ports, and in both situations I got the
following output:
 1.00
 0.99


Is there something I'm doing wrong that would trigger the truncation at
some point? Or is this a compiler bug? Would anyone know a workaround to
get correct results?


Thanks in advance for your help,
Natasha

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2014-01-28 23:43 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-01-22 16:48 How to round to the nearest fixed-point value? Natasha Kerensikova
2014-01-22 17:53 ` G.B.
2014-01-22 22:26   ` adambeneschan
2014-01-23  9:21     ` Georg Bauhaus
2014-01-22 22:45 ` adambeneschan
2014-01-23  5:29   ` J-P. Rosen
2014-01-23  7:00     ` Natasha Kerensikova
2014-01-23  9:42     ` Georg Bauhaus
2014-01-23  7:02   ` Natasha Kerensikova
2014-01-23 16:41     ` adambeneschan
2014-01-24  9:58 ` Natasha Kerensikova
2014-01-24 22:30   ` Randy Brukardt
2014-01-24 22:47     ` Randy Brukardt
2014-01-26 14:19     ` Natasha Kerensikova
2014-01-28 23:43       ` Randy Brukardt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox