comp.lang.ada
 help / color / mirror / Atom feed
* Fixed-point question
@ 2015-06-26 13:20 Patrick Noffke
  2015-06-26 16:56 ` Jeffrey R. Carter
  0 siblings, 1 reply; 24+ messages in thread
From: Patrick Noffke @ 2015-06-26 13:20 UTC (permalink / raw)


Why are C1 and C2 different?

with Ada.Text_IO;

procedure FP_Test is
   type Fixed_Type is delta 1.0 / 180.0 range 0.0 .. 65535.0 / 180.0;
   N : constant := 0.2;
   D : constant := 4.5 / 330.0;  -- 0.013636

   C1 : constant Fixed_Type := Fixed_Type (N / D);
   C2 : constant Fixed_Type := N / D;

begin
   Ada.Text_IO.Put_Line ("C1 := " & C1'Img);
   Ada.Text_IO.Put_Line ("C2 := " & C2'Img);
end FP_Test;

$ ./fp_test 
C1 :=  14.664
C2 :=  17.000

I am using GNAT on Fedora 21 (x86_64) and GNAT 2014 for ARM-ELF (Linux-hosted).  The output above is on Fedora.  In the ARM processor (Cortex-M4), C1 is 14.667 and C2 is 18.

It appears that for ARM, the compiler is computing C2 as Fixed_Type (N) / Fixed_Type (D), or 0.2 / (2 / 180) = 0.2 / 0.01111 = 18.

I'm not sure why C2 is 17 on the Fedora PC.

Thanks,
Patrick

^ permalink raw reply	[flat|nested] 24+ messages in thread
* Fixed-point question
@ 1995-04-01  0:00 Garlington KE
  1995-04-06  0:00 ` Robert Dewar
  0 siblings, 1 reply; 24+ messages in thread
From: Garlington KE @ 1995-04-01  0:00 UTC (permalink / raw)


There's probably a simple answer to this question, and I'm just too tired
to see it, but here goes anyway:

We have a user who has defined a fixed-point of the general form

   type A_Type is delta 0.1 range ...;

and some objects like the following:

   A: constant A_Type := 2.2;
   B: constant A_Type := 2.3;

and an abject C of A_Type which gets set to the expression 9.0 * 0.25.

He then runs the following code:

  if C = A then
     Text_IO.Put_Line("C equals A");
  elsif C = B then 
     Text_IO.Put_Line("C equals B"); 
  else
     Text_IO.Put_Line("C equals neither!");
  end if;

and of course it says that C equals neither, since C is stored as the
model number 2.25, which is neither 2.1875 nor 2.3125. I explain this
to the user, and all is well until he asks: "How should I do this
comparison, then?" Thinking back to my old FORTRAN days with floating-point,
I say, "Well, you compute the absolute difference between the two
numbers, and then see if the difference is less than some epsilon."
However, I can't for the life of me decide how to write the
equality function such that (1) C will be "equal" to B and (2) C will not
be "equal" to A, nor what epsilon is right (A_Type'Small?)

If someone could post the "roughly equal" function I'm describing, I would
appreciate it. (Note: this would need to be a pre-Ada 95 solution).

--------------------------------------------------------------------
Ken Garlington                  GarlingtonKE@lfwc.lockheed.com
F-22 Computer Resources         Lockheed Fort Worth Co.

If LFWC or the F-22 program has any opinions, they aren't telling me.




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

end of thread, other threads:[~2015-06-29 14:13 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-26 13:20 Fixed-point question Patrick Noffke
2015-06-26 16:56 ` Jeffrey R. Carter
2015-06-26 18:21   ` Patrick Noffke
2015-06-26 20:21     ` Anh Vo
2015-06-26 20:25     ` Jeffrey R. Carter
2015-06-27  2:55       ` Patrick Noffke
2015-06-26 23:14     ` Qun-Ying
2015-06-27  3:00       ` Patrick Noffke
2015-06-27  4:44         ` Jeffrey R. Carter
2015-06-27 17:59           ` Patrick Noffke
2015-06-27 18:38             ` Jeffrey R. Carter
2015-06-27 19:20               ` Bob Duff
2015-06-27 19:57                 ` Jeffrey R. Carter
2015-06-27 20:42                   ` Bob Duff
2015-06-28 11:39                     ` Brian Drummond
2015-06-29 13:28                     ` Patrick Noffke
2015-06-29 14:13                       ` Bob Duff
2015-06-27 23:41               ` Anh Vo
  -- strict thread matches above, loose matches on Subject: below --
1995-04-01  0:00 Garlington KE
1995-04-06  0:00 ` Robert Dewar
1995-04-07  0:00   ` Robert I. Eachus
1995-04-07  0:00     ` Robert Dewar
1995-04-07  0:00   ` Garlington KE
1995-04-07  0:00     ` Robert Dewar

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