Results 1 to 4 of 4

Thread: floating point

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1

    floating point

    hi
    consider this simple program

    Code:
    program Project1;
    {$APPTYPE CONSOLE}
    const
        car_speed = 0.01;
        point_a   = 5.14;
        point_b   = 10.00;
    var
        car_pos : single = 0.0;
    begin
    
      while True do
      begin
        car_pos := car_pos + car_speed;
        writeln('car position :',car_pos:2:2);
        if car_pos = point_a then
           writeln('we are in point a');
        if car_pos = point_b then
         begin
           writeln('we are in point b');
           break;
         end;
     end;
     readln;
    end.
    the problem is the 2 conditions (if) will never executing , but if i change car_pos from single to currency , the problem disappeared.

    well i know this is because of floating point rounding problem , but currency is 8byte length , so if i want only 4byte real variable , what suggestion would you propose ?
    Last edited by AirPas; 23-09-2011 at 01:04 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •