Malfunction round()

Xbase++ 2.0 Build 554 or later
Post Reply
Message
Author
Piotr D
Posts: 131
Joined: Mon Jul 28, 2014 1:26 am
Location: Poznań, Poland

Malfunction round()

#1 Post by Piotr D »

Hi,
in some cases the function Round() gives wrong result. For example:
0.82*0.15*115.00 = 14.145
but round() of these value:
Round(0.82*0.15*115.00 ,2) gives 14.14 instead 14.15 !!!

Piotr

Piotr D
Posts: 131
Joined: Mon Jul 28, 2014 1:26 am
Location: Poznań, Poland

Re: Malfunction round()

#2 Post by Piotr D »

Update:
when you round intermediate results, like:
Round(Round(0.82*0.15,4)*115.00 ,2)
the result is correct: 14.15

Look like it's again the result of a binary representation of the non-integer value (like 14.15 = 14.1499999999999999999...)

Regards
Piotr

Wolfgang Ciriack
Posts: 481
Joined: Wed Jan 27, 2010 10:25 pm
Location: Berlin Germany

Re: Malfunction round()

#3 Post by Wolfgang Ciriack »

That is the reason, i use the function roundZ() for some years:

Code: Select all

Function RoundZ(nValue)
Return (Round(Round(nValue, 4), 2))
_______________________
Best Regards
Wolfgang

Piotr D
Posts: 131
Joined: Mon Jul 28, 2014 1:26 am
Location: Poznań, Poland

Re: Malfunction round()

#4 Post by Piotr D »

Wolfgang,
these function don't solve problem. For example:
x=14.144966
Round(x,2) will be 14.14
and RoundZ(x) will be 14.45.

To get the correct result of multiple multiplication of a non-integer values, we need round after each multiplication.

Piotr

Piotr D
Posts: 131
Joined: Mon Jul 28, 2014 1:26 am
Location: Poznań, Poland

Re: Malfunction round()

#5 Post by Piotr D »

Hi all,
I found that these problem was described under PDR 6678. When I add in ARC file under VERSION:
"FPU_ControlWord" = "626"

this problem was solved.

Regards
Piotr

Piotr D
Posts: 131
Joined: Mon Jul 28, 2014 1:26 am
Location: Poznań, Poland

Re: Malfunction round()

#6 Post by Piotr D »

Hi all,
I found another solution. This easy trick solve the problem :)
instead Round(x,n) ---> Round(Val(Str(x)),n)

Regards
Piotr

Post Reply