LISTSERV mailing list manager LISTSERV 16.0

Help for TEAM-ADA Archives


TEAM-ADA Archives

TEAM-ADA Archives


TEAM-ADA@LISTSERV.ACM.ORG


View:

Message:

[

First

|

Previous

|

Next

|

Last

]

By Topic:

[

First

|

Previous

|

Next

|

Last

]

By Author:

[

First

|

Previous

|

Next

|

Last

]

Font:

Proportional Font

LISTSERV Archives

LISTSERV Archives

TEAM-ADA Home

TEAM-ADA Home

TEAM-ADA  February 1997

TEAM-ADA February 1997

Subject:

Floating point problems in GNAT

From:

Lamar Harris <[log in to unmask]>

Reply-To:

Lamar Harris <[log in to unmask]>

Date:

Wed, 12 Feb 1997 10:57:05 -0400

Content-Type:

text/plain

Parts/Attachments:

Parts/Attachments

text/plain (180 lines)

To All,

I apolgize in advance for the length of this message, but the problem I've
encountered is rather subtle and the code and sample run to demonstrate the
error are rather lengthy even though I've tried to shorten them.

I've been working on the Traveling Salesman Problem using GNAT 3.09 for
Solaris 2.4 on a Sun Sparc Ultra 1. While working on TSP, I've encountered a
strange run-time problem (error, phenomenom??) with floating point numbers.
I was wondering if anyone out there has had a similar experience, has an
idea as to what's going on, or has any advice as to how to fix it. Here goes...

In my program, I generate a random set of points whose X and Y coordinates
are within the range 0.0 to 1.0.  I then find an ordering of these points
using two different methods. One method is to search through all possible
permutations and return the permutation with the shortest path length. The
other method I will not discuss as I intend to publish the results (sorry).
To gather empirical evidence that my second method is valid before I launch
into a lengthy analysis of it, I compare the path length generated by the
permutation method with the path length generated by my method. Here's where
the fun began.

I first developed this program in Lucid Common Lisp running on the same Sun
Ultra machine. The problem I had there was that there were small variances
in the least (15th) significant digit. I was able to set this digit to zero
and then compare the remaining 14 signifcant digits. I encountered no
further problems. To further verify my method, I rewrote it in Ada95. I
verified the individual components and then tried to test my method against
the permutation method and it almost immediately failed. I added additional
debugging output and from what I learned from that and an analysis of the
code, I've been able to reproduce the problem in the program given below.

Here's how the program below works:
1. I define my own floating point type, IO and Elementary_Functions for that
type, and a Point_Type record that uses that type to store the X and Y
coordinates of the point.

2. There are two functions. One determines the distance between two points
and the other returns a random point.

3. I then define five random points A through E.

4. I then use the Distance function to determine the path lengths for going
from point A to B to C to D to E and back to A and for going from point C to
B to A to E to D and back to C. Since these are the same closed path (just
starting at a different point and going in opposite directions), the lengths
should be the same.

5. I then print out the coordinates for the points (displaying more digits
than the type is defined for).

6. Finally, I print out the path lengths, compare the path lengths, and
print out the results of the comparison.

About four times out of five the path lengths are the same and the
comparison reports this.  The other time, however, the path lengths
displayed are the same, BUT the comparison reports that the first is LONGER
than the second.

Here's the output from a run that show this behavior:

Point A: 0.6049214601516720000000000, 0.6367374658584600000000000
Point B: 0.2369488328695300000000000, 0.0501336120069027000000000
Point C: 0.5963447093963620000000000, 0.3965126872062680000000000
Point D: 0.8365010619163510000000000, 0.6842445731163020000000000
Point E: 0.2764832675457000000000000, 0.4119921326637270000000000

Path 1 Length (ABCDEA) = 2.5870557464745700000000000
Path 2 Length (CBAEDC) = 2.5870557464745700000000000
Path 1 is longer than Path 2

Note that the actual values for the path lengths are the same, but the
comparison fails.

I've also run the same program under GNAT 3.07 for DOS on an Intel 486 and
under GNAT 3.05 for Win95 on an Intel Pentium and I get the same error.
Also, occasionally the first is reported as shorter than the second.

What is causing this and how do I fix it? Please don't tell me I have to do
this all in Lisp or, Heaven forbid, C because of a fluke in Ada!

If you try this on your own, be sure to run the program at least about 10
times. It may take this many times for the error to show up.

Thanks is advance for any help with this that I receive.

Here's the source code that generated this error:

with Ada.Text_IO, Ada.Integer_Text_IO, Ada.Numerics.Float_Random;
with Ada.Numerics.Generic_Elementary_Functions;
use Ada.Text_IO, Ada.Integer_Text_IO, Ada.Numerics.Float_Random;

procedure Float_Test is

  type My_Float is digits 8;
  package My_Float_IO is new Ada.Text_IO.Float_IO(My_Float);
  package My_Float_Functions
    is new Ada.Numerics.Generic_Elementary_Functions(My_Float);
  use My_Float_IO, My_Float_Functions;

  type Point_Type is record
    X: My_Float;
    Y: My_Float;
  end record;

  function Distance(Point1, Point2: in Point_Type) return My_Float is
  begin
    return Sqrt((Point1.X - Point2.X)**2 + (Point1.Y - Point2.Y)**2);
  end Distance;

  Float_Generator: Ada.Numerics.Float_Random.Generator;

  function Random_Point return Point_Type is
    New_Point: Point_Type;
  begin
    New_Point.X := My_Float(Random(Float_Generator));
    New_Point.Y := My_Float(Random(Float_Generator));
    return New_Point;
  end Random_Point;

  Point_A, Point_B, Point_C, Point_D, Point_E: Point_Type;

  Path_A_Length, Path_B_Length: My_Float;

begin

  Reset(Float_Generator);

  Point_A := Random_Point; Point_B := Random_Point;
  Point_C := Random_Point; Point_D := Random_Point;
  Point_E := Random_Point;

  Path_A_Length
    := Distance(Point_A, Point_B)+Distance(Point_B, Point_C)+
       Distance(Point_C, Point_D)+Distance(Point_D, Point_E)+
       Distance(Point_E, Point_A);

  Path_B_Length
    := Distance(Point_C, Point_B)+Distance(Point_B, Point_A)+
       Distance(Point_A, Point_E)+Distance(Point_E, Point_D)+
       Distance(Point_D, Point_C);

  Put("Point A: "); Put(Point_A.X,0,25,0);Put(", "); Put(Point_A.Y,0,25,0);
  New_Line;
  Put("Point B: "); Put(Point_B.X,0,25,0);Put(", "); Put(Point_B.Y,0,25,0);
  New_Line;
  Put("Point C: "); Put(Point_C.X,0,25,0);Put(", "); Put(Point_C.Y,0,25,0);
  New_Line;
  Put("Point D: "); Put(Point_D.X,0,25,0);Put(", "); Put(Point_D.Y,0,25,0);
  New_Line;
  Put("Point E: "); Put(Point_E.X,0,25,0);Put(", "); Put(Point_E.Y,0,25,0);
  New_Line(2);
  Put("Path 1 Length (ABCDEA) = "); Put(Path_A_Length,0,25,0);
  New_Line;
  Put("Path 2 Length (CBAEDC) = "); Put(Path_B_Length,0,25,0);
  New_Line;

  if Path_A_Length < Path_B_Length then
    Put_Line("Path 1 is shorter than Path 2");
  elsif Path_A_Length > Path_B_Length then
    Put_Line("Path 1 is longer than Path 2");
  elsif Path_A_Length = Path_B_Length then
    Put_Line("Path 1 is the same length as Path 2");
  else
    Put_Line("Comparison Error!");
  end if;

end Float_Test;

Thanks again for helping,
       ______     __     __
      / ____ \   / /\   / /\  Ray Harris, 2Lt, USAF
     / /\__/ /\ / / /  / / / Rome Lab/C3CA
    / /_/_/ / // /_/__/ / / 525 Brooks Rd.
   / __   _/ // _____  / / Rome, NY 13441-4505
  / /\_| |\_// /\___/ / / "Mind is the Software for the Hardware Called Brain"
 / / / | || / / /  / / / E-Mail: [log in to unmask] (Home: [log in to unmask])
/_/ /  |_||/_/ /  /_/ / Phone : 315-330-4266 (DSN 587-4266)
\_\/   \_\|\_\/   \_\/ Fax   : 315-330-7989 (DSN 587-7989)

Top of Message | Previous Page | Permalink

Advanced Options


Options

Log In

Log In

Get Password

Get Password


Search Archives

Search Archives


Subscribe or Unsubscribe

Subscribe or Unsubscribe


Archives

December 2017
November 2017
October 2017
September 2017
June 2017
May 2017
April 2017
January 2017
December 2016
November 2016
October 2016
August 2016
July 2016
June 2016
May 2016
April 2016
March 2016
February 2016
January 2016
December 2015
November 2015
October 2015
September 2015
August 2015
June 2015
May 2015
April 2015
March 2015
February 2015
January 2015
December 2014
November 2014
September 2014
August 2014
July 2014
June 2014
May 2014
April 2014
March 2014
February 2014
January 2014
December 2013
November 2013
October 2013
September 2013
June 2013
May 2013
April 2013
March 2013
February 2013
January 2013
December 2012
November 2012
October 2012
September 2012
August 2012
July 2012
June 2012
May 2012
April 2012
February 2012
January 2012
December 2011
November 2011
October 2011
September 2011
August 2011
July 2011
June 2011
May 2011
April 2011
December 2010
November 2010
October 2010
September 2010
June 2010
May 2010
April 2010
March 2010
February 2010
January 2010
December 2009
November 2009
October 2009
September 2009
August 2009
July 2009
June 2009
May 2009
April 2009
February 2009
January 2009
December 2008
November 2008
October 2008
September 2008
August 2008
June 2008
May 2008
April 2008
March 2008
February 2008
January 2008
December 2007
November 2007
October 2007
September 2007
June 2007
May 2007
March 2007
February 2007
December 2006
November 2006
October 2006
September 2006
August 2006
July 2006
June 2006
May 2006
April 2006
March 2006
February 2006
January 2006
December 2005
November 2005
October 2005
August 2005
July 2005
June 2005
May 2005
April 2005
March 2005
February 2005
January 2005
December 2004
November 2004
October 2004
September 2004
August 2004
July 2004
June 2004
May 2004
March 2004
February 2004
January 2004
December 2003
November 2003
October 2003
September 2003
August 2003
July 2003
June 2003
May 2003
April 2003
March 2003
February 2003
January 2003
December 2002
November 2002
October 2002
September 2002
August 2002
July 2002
June 2002
May 2002
April 2002
March 2002
February 2002
December 2001
November 2001
October 2001
September 2001
August 2001
July 2001
June 2001
May 2001
April 2001
March 2001
February 2001
January 2001
December 2000
November 2000
October 2000
September 2000
August 2000
July 2000
June 2000
May 2000
April 2000
March 2000
February 2000
January 2000
December 1999
November 1999
October 1999
September 1999
August 1999
July 1999
June 1999
May 1999
April 1999
March 1999
February 1999
January 1999
December 1998
November 1998
October 1998
September 1998
August 1998
July 1998
June 1998
May 1998
April 1998
March 1998
February 1998
January 1998
December 1997
November 1997
October 1997
September 1997
August 1997
July 1997
June 1997
May 1997
April 1997
March 1997
February 1997
January 1997
December 1996
November 1996
October 1996

ATOM RSS1 RSS2



LISTSERV.ACM.ORG

Secured by F-Secure Anti-Virus CataList Email List Search Powered by the LISTSERV Email List Manager