AirPas
23-09-2011, 01:01 PM
hi
consider this simple program
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 ?
consider this simple program
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 ?