HELP HELP HELP - round function error
Author Message HELP HELP HELP - round function error

I have a big problem with the round function...

whent i try to calculate the round of 111.5 for example i get 111 and in
other situation like 112.5 i get 113..

Why is this happening???

--
Victor Hugo Terra Melo
aKa aZoreanMan
Sao Miguel - AZORES
PORTUGAL

Thu, 15 Aug 2002 03:00:00 GMT  HELP HELP HELP - round function error

Quote:
> whent i try to calculate the round of 111.5 for example i get 111 and in
> other situation like 112.5 i get 113..

> Why is this happening???

It is not possible to represent all decimal fractions exactly in a
binary format; therefore, rounding errors are bound to happen somewhere.
Simplistic example:

x=1/3
// Now x is _not_ 1/3. In fact, it's 0.333333333-and-a-bit, depending on
// the precision of the language... not sure how precise Clipper is, but
// certainly not perfectly.
y=x*3
// Now y _may_ be 1. It may also be 0.9999999999-and-a-bit, or even
// 1.000000000-and-a-bit.

Something like that is happening in your code. If you only need a
certain number of decimals, fixed-point math may be a solution.

Richard

Fri, 16 Aug 2002 03:00:00 GMT  HELP HELP HELP - round function error

Quote:
> whent i try to calculate the round of 111.5 for example i get 111 and in
> other situation like 112.5 i get 113..

The 111.5 value is probably closer to 111.4999, so it gets rounded down to
111, but the 112.5 value is greater than or equal to 112.500000, so it gets
rounded up to 113. It is not a bug. It is simply one of the many problems
with using floating point numbers. If you want to see what the actual values
are, use something like the following to display them:

? STR( variable, 30, 20 )

Don't bother trying "? STR( 111.5, 30, 20 )", because Clipper uses an
optimizing compiler and will print "      111.50000000000000000000".
--
David G. Holm

Fri, 16 Aug 2002 03:00:00 GMT  HELP HELP HELP - round function error
Hello.

Quote:
> I have a big problem with the round function...
> whent i try to calculate the round of 111.5 for example i get 111 and in
> other situation like 112.5 i get 113..

Why it happens - see other replies. What to do...
I'd suggest to add a very small number before rounding, as:
Round(111.5 + 0.00001, 0)
will give you 112.
The number to add depends on the largest number you want to
round. If it is near N17.2, you will IMHO need 0.001. If you want
to round small numbers with 4-5 decimals, you will need 0.00000001.
It's result of my tests, which may not be true for all cases of course.
--

Sat, 17 Aug 2002 03:00:00 GMT  HELP HELP HELP - round function error

Quote:
>I have a big problem with the round function...

>whent i try to calculate the round of 111.5 for example i get 111 and in
>other situation like 112.5 i get 113..

>Why is this happening???

Yet another suggestion, based on a problem I had:

I generally have no rounding problems, but observed one when doing repeated
additions and subtraction with numbers with one digit after the decimal
point. Most of the time the result was accurate, but once in a while I got a
result which was 0.1 off from what it should be (see other postings for the
reason why).

As I was only using figures with one digit after the points, I simply
multiplied all the variables with 10, did the calculations with the integers
I know had, and divided the result by 10 at the end.

Dick

Sat, 17 Aug 2002 03:00:00 GMT

 Page 1 of 1 [ 5 post ]

Relevant Pages