Could you explain me this strange behavior (Round)?

If use LotusScript:------------------------------------------------

Sub Click(Source As Button)

Dim a As Double, b As Double, c As Double

Dim a_ As Double, b_ As Double, c_ As Double



a=4.23

b=4.35

c=4.45



a_=Round(a,1)

b_=Round(b,1)

c_=Round(c,1)



Messagebox Cstr(a) + " -> " + Cstr(a_) + Chr(10) +_

Cstr(b) + " -> " + Cstr(b_) + Chr(10) +_

Cstr(c) + " -> " + Cstr(c_) + Chr(10)

End Sub


I get:

4.23 -> 4.2

4.35 -> 4.4

4.45 -> 4.4 (Wrong! Expected: 4.5)

If use @Formula:


a:=4.23;

b:=4.35;

c:=4.45;

a_:=@Round(a; 0.1);

b_:=@Round(b; 0.1);

c_:=@Round(c; 0.1);



@Prompt([Ok]; "Test";

@Text(a) + " -> " + @Text(a_) + @Char(10) +

@Text(b) + " -> " + @Text(b_) + @Char(10) +

@Text(c) + " -> " + @Text(c_))

I get:

4.23 -> 4.2

4.35 -> 4.3 (Wrong! Expected: 4.4)

4.45 -> 4.5

Why do these functions return different results?

And what can I do to get correct results?

Thanks in advance.

Subject: Could you explain me this strange behavior (Round)?

Hi there,

I get the same results as you do. However, there has always been something fishy with Notes and numbers.

For instance, I have had this problem with Notes since at least r4:

I subtract 2 simple numbers, and get a very strange result.

If I add this to the end of your code (or anywhere, this also happens in straight fields)

Dim x As Double, y As Double, z As Double

x = 142

y = 141.99

z = x-y



Messagebox Cstr(z)

The answer I get is:

9.99999999999091E-3

I suspect the same numbering glitch is messing up the rounding function.

-Anastasia Peuhkurinen

Subject: Solution: Could you explain me this strange behavior (Round)?

Sorry,

By the way, to fix your problem, add .005 to each number before you round it:

a_=Round(a+.005,1)

b_=Round(b+.005,1)

c_=Round(c+.005,1)

-Anastasia

Subject: RE: Solution: Could you explain me this strange behavior (Round)?

Anastasia,thank you for your replies.

In my opinion, your suggestion is not really good solution.

I’ve wrote my own LS function, which rounds numbers ending in “5” to the next higher number.

Here it goes.

Function RSRound(number As Double, places As Integer) As Double

Dim factor As Double



factor = 10 ^ places	

RSRound = Sgn(number) * Fix( Abs(number) * factor + 0.5) / factor

End Function

Ilya.

Subject: Could you explain me this strange behavior (Round)?

Actually, the ones you had marked as “wrong” are right – numbers ending in “5” should round to the nearest EVEN number, not to the next higher number. This method of rounding is called “Banker’s Rounding”, and reduces the statistical significance of cumulative errors.

HOWEVER, and this is a big one, these rules apply to decimal mathematics. Computers do not perform decimal mathematics, they work with binary numbers. When you work with integer-only values (whether they are “Integer” or “Long”), there is no difference in behaviour between decimal and binary values. As soon as you allow fractions into the equation, though, you are creating a problem. Most decimal fractions cannot be represented exactly as binary fractions:

a*(1/2) + b(1/4) + c*(1/8) + d*(1/16)…

There will almost always be a small error between the expected decimal value and the actual binary value of a computation. This has nothing at all to do with Notes and Domino – it applies to any binary computer system and every language.

Subject: RE: Could you explain me this strange behavior (Round)?

Stan,thank you for your reply.

I see what you mean, but I needed to round numbers to the next higher number. So I wrote my own LS function ( http://www-10.lotus.com/ldd/nd6forum.nsf/DateAllThreadedweb/8ceaadd680eb542685256fe300255a48?OpenDocument ).

But I still can not understand why @Round and Round() in some cases return different results.

Ilya.

Subject: RE: Could you explain me this strange behavior (Round)?

Well, they ARE different languages and they work on different in-memory data structures. See these articles on Julian Robichaux’s site for more:

http://www.nsftools.com/blog/blog-02-2005.htm

http://www.nsftools.com/blog/blog-03-2005.htm#03-15-05