Round function

Write here if you have problems with your Pascal source code

Moderator: Board moderators

Post Reply
edfigo
New poster
Posts: 6
Joined: Mon Jun 17, 2002 2:57 am
Location: Portugal

Round function

Post by edfigo » 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 :lol: )
It's time to revise this error for benefict of competition.

(Sorry my poor english!)
Thanks!
edfigo

Dominik Michniewski
Guru
Posts: 832
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski » 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
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)

edfigo
New poster
Posts: 6
Joined: Mon Jun 17, 2002 2:57 am
Location: Portugal

Post by edfigo » Wed Jul 02, 2003 2:44 pm

The problem is that Round(2.5) is 2 !!!
edfigo

Dominik Michniewski
Guru
Posts: 832
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski » 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
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)

raymond85
New poster
Posts: 21
Joined: Tue Jul 01, 2003 9:26 am
Location: Hong Kong
Contact:

Post by raymond85 » 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.

edfigo
New poster
Posts: 6
Joined: Mon Jun 17, 2002 2:57 am
Location: Portugal

Post by edfigo » 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 :(

Thanks for reply!
edfigo

raymond85
New poster
Posts: 21
Joined: Tue Jul 01, 2003 9:26 am
Location: Hong Kong
Contact:

Post by raymond85 » Wed Jul 02, 2003 8:05 pm

oh...then...no idea then. Or if anyone knows, plz share with us. I would like to know the reason behind that too.

Post Reply

Return to “Pascal”