#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2014
    Posts
    5
    Rep Power
    0

    ada float array is changing values


    I have an array declared, plus a couple of variables
    type data_array is array (1 .. 100) of float;
    data : data_array;
    x1: INTEGER := 1;

    in an external file I have 2329639.34 which is read into the the array as
    data(x1)
    but when the program gets down further and data(x1) is to be output to a different txt file it gets printed out (in the external txt file) as 2329639.25

    I'm assuming that it has something to do with the fact that the array is type float but i'm not sure what to do to fix it.

    i'd copy more of my program but don't want to give you information overload.

    any ideas of what is going on and how to fix it?
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2014
    Posts
    5
    Rep Power
    0
    actually let me give you some different code (which will be a full program) so you can more accurately see what is happening.
    with Ada.Float_Text_Io;
    with Ada.Text_Io;
    procedure roundto2 is
    a : float;
    begin
    --------------------------------------------------------------------------
    --
    -- Give "a" a value that has more than 2 places past the decimal
    --------------------------------------------------------------------------
    --
    a := 96393.34555;
    --------------------------------------------------------------------------
    --
    -- Display the value showing 4 places past the decimal
    --------------------------------------------------------------------------
    --
    Ada.Text_Io.Put( "Before Rounding: " );
    Ada.Float_Text_Io.Put( a, 10, 4, 0 );
    Ada.Text_Io.New_Line;
    --------------------------------------------------------------------------
    --
    -- Round to two places past the decimal (for practical purposes)
    --------------------------------------------------------------------------
    --
    a := float'rounding( a * 100.0 ) * 0.01;
    --------------------------------------------------------------------------
    --
    -- Display the value again showing 4 places past the decimal
    --------------------------------------------------------------------------
    --
    Ada.Text_Io.Put( "After Rounding: " );
    Ada.Float_Text_Io.Put( a, 10, 4, 0 );
    Ada.Text_Io.New_Line;
    end roundto2;

    now in the above program you can see that
    a := 96393.34555
    but if you compile and run this program it will show that
    before rounding: 96393.3438
    after rounding: 96393.3359

    a is changing value in the program for some reason.


    so let's start with this issue.
    why is a changing?
  4. #3
  5. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,944
    Rep Power
    1961
    What happen if you create a small program that only read a text file with that number and then save it into another file?
    It might be an issue with precision.
  6. #4
  7. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,944
    Rep Power
    1961
    How does the number looks like if you print it first time "as is":
    Code:
    Ada.Text_Io.Put( "Before Rounding: " );
    Ada.Float_Text_Io.Put( a);
    Ada.Text_Io.New_Line;
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2014
    Posts
    5
    Rep Power
    0
    Before Rounding: 9.63933E+04

    In your previous message you asked me to write a program that only reads a text file with that number and then save it into another file. I will try to do that tonight.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2014
    Posts
    5
    Rep Power
    0
    ok i was able to finish that a little faster then planned lol

    here is the program


    with Ada.Text_IO;
    use Ada.Text_IO;

    with Ada.float_Text_IO;
    use Ada.float_Text_IO;

    with Ada.integer_Text_IO;
    use Ada.integer_Text_IO;

    procedure simple is
    temp:FLOAT:=0.0;
    balanced : FILE_TYPE;
    startingpoint : FILE_TYPE;

    Begin
    OPEN ( startingpoint, IN_FILE, "startingpoint.txt");
    if IS_OPEN(startingpoint) then
    Ada.float_Text_IO.Get (startingpoint, temp);
    end if;
    CLOSE (startingpoint);

    CREATE(balanced, out_file, "balanced.txt");

    if IS_OPEN(balanced) then

    Ada.float_Text_IO.put(balanced, temp);

    end if;


    CLOSE(balanced);

    end simple;


    the startingpoint.txt file had the number of
    2329639.34
    the balanced.txt file has the number of
    2.32964E+06


    now I'm not sure if this is important but there is one space before the 2.32964E+06 in the text file when there is no space there in the original file and also there is a return carriage after the number of 2.32964E+06 when there isn't one in the original file.

    Thank you for the help you are giving me. I do appreciate it.

IMN logo majestic logo threadwatch logo seochat tools logo