Page 1 of 1

Round function

Posted: Wed Jul 02, 2003 1:48 pm
TO ALL PASCAL PROGRAMMER'S...
I think there was an error with the FreePascal's Round function (you should know by now!)
What should be the result of Round(1.5)? 2! (right)
And what should be the result of Round(2.5)? 3! (wrong!!!)
Run the following program and confirme the results...

[pascal]
Var
i : Integer;
Begin
For i:=0 To 10 Do
WriteLn(i:3,' ', i+0.5, (i+0.5):5:1, (i+0.5):3:0,Round(i+0.5):3)
End.
[/pascal]

And what about the output formatting of reals (x:places:decimals)...
Run the following program and observe the third and fourth column:
[pascal]
Var
i : Integer;
Begin
For i:=0 To 10 Do
WriteLn(i:3,' ', i+0.35, (i+0.35):5:1, (i+0.35):6:1)
End.
[/pascal]
I think many submissions gives WA 'cause of this bug (i speak by myself )
It's time to revise this error for benefict of competition.

(Sorry my poor english!)
Thanks!

Posted: Wed Jul 02, 2003 2:24 pm
I don't understand why you are inconsistent ....
if Round(1.5) == 2, that means round-up 1.5
than why Round(2.5) shouldn't be 3 ???????????????????? It's round-up too. ...

Best regards
DM

Posted: Wed Jul 02, 2003 2:44 pm
The problem is that Round(2.5) is 2 !!!

Posted: Wed Jul 02, 2003 2:49 pm
I think that problem is in binary representation of 1.5 and 2.5 ... its possible, that 2.5 is evaluated to 2.4999999999999 which is smaller than 2.5 and 1.5 is evaluated to 1.5000000001 or something like this .....

Best regards
DM

Posted: Wed Jul 02, 2003 7:14 pm
Yea, that's not a bug actually. However, it's the problem of number representation within the computer. I hope you understand how a number is represented in binary with floating point. There's always precision errors. What you can do is, try to use some longer floating point variable type like extended. I think it might helps. Correct me if I am wrong.

Posted: Wed Jul 02, 2003 7:53 pm
Anyway this is a problem...
and when you expected that the function behaves with same mode for values that have the same fraction part... you must agree that is unconfortable use it.
By the way, the representation in binary of
1.5(10) is 1.1(2), since 1*2^0 + 1*2^-1 = 1.5(10) and
2.5(10) is 10.1(2), since 1*2^1 + 0*2^0 + 2^-1 = 2.5(10)
The number 0.5(10) is perfectely represented em binary, don't you agree...
So, i think the problem should be other 