Rounding Off or Precision Error
Author Message
Rounding Off or Precision Error

Hello

Recently I have observed that many of my total are going off by .01 which
seems to be due to precision and rounding off error.

I looked for a round function but the one that is inbuilt will turn the
figure into whole number and not nearest 2 digit.

How do I go about it. Is there any other routines available which take
care of this.

Also while browsing the table I found that figure are displayed upto 2
decimals but I feel there may be values which have greater than 2 digit
figure which are causing problems. How do I locate them.

Any help wiill be greatly appreciated.

TIA

Natwar Lath
Rourkela India

Sun, 05 Jan 2003 03:00:00 GMT
Rounding Off or Precision Error
Round to 2 digits:

Result := Round(Value * 100.0) * 0.01;

Find values with more than 2 digits:

if Value * 100.0 - Int(Value * 100.0) > 0 then ...

Quote:
> Hello

> Recently I have observed that many of my total are going off by .01 which
> seems to be due to precision and rounding off error.

> I looked for a round function but the one that is inbuilt will turn the
> figure into whole number and not nearest 2 digit.

> How do I go about it. Is there any other routines available which take
> care of this.

> Also while browsing the table I found that figure are displayed upto 2
> decimals but I feel there may be values which have greater than 2 digit
> figure which are causing problems. How do I locate them.

> Any help wiill be greatly appreciated.

> TIA

> Natwar Lath
> Rourkela India

Sun, 05 Jan 2003 03:00:00 GMT
Rounding Off or Precision Error
Quote:

> Round to 2 digits:

> Result := Round(Value * 100.0) * 0.01;

It is good to keep in mind that Round function rounds number in weird way
when the fractional part is 0.5. Is such case if integer part is odd it
rounds up and when it is even it rounds down (for example Round(2.5)=2,
Round(3.5)=4). The typical way is to always round up when fractional part is
0.5

--
PSky

Sun, 05 Jan 2003 03:00:00 GMT
Rounding Off or Precision Error
No, it is even stranger !

Round(0,5) = 0 Round(1,5) = 1 Round(2,5) = 2 Round(3,5) = 3 Round(4,5) = 4
Round(5,5) = 6 Round(6,5) = 7 Round(7,5) = 8 Round(8,5) = 9 Round(9,5) = 10
Round(10,5) = 10 Round(11,5) = 11 Round(12,5) = 12 Round(13,5) = 13
Round(14,5) = 14
Round(15,5) = 16 Round(16,5) = 17 Round(17,5) = 18 Round(18,5) = 19
Round(19,5) = 20
etc.

Quote:

> > Round to 2 digits:

> > Result := Round(Value * 100.0) * 0.01;
> It is good to keep in mind that Round function rounds number in weird way
> when the fractional part is 0.5. Is such case if integer part is odd it
> rounds up and when it is even it rounds down (for example Round(2.5)=2,
> Round(3.5)=4). The typical way is to always round up when fractional part
is
> 0.5

> --
> PSky

Sun, 05 Jan 2003 03:00:00 GMT
Rounding Off or Precision Error
"Floating point numbers are like piles of sand on the ground.  Each time
you move one, you lose a little sand (precision) and pick up a little
dirt (error)."

Floating point numbers are intended for scientific calculation and do
not meet the requirements of most accounting-type applications.  For
these, either BCD (Binary-Coded Decimal) or scaled integers (value
stored = value * 10000) are normally used.  Delphi does not provide
built-in BCD support although the routines are readily available.

Accounting applications commonly carry all operations using 4 digits to
the right of decimal, then either truncate or round to two digits.  A
technique called "banker's rounding" is often used, where odd numbers
round up and even numbers round down, thereby cancelling most of the
accumulated effects of rounding, which otherwise could add up to many
dollars.

Quote:

> Recently I have observed that many of my total are going off by .01 which
> seems to be due to precision and rounding off error.

> I looked for a round function but the one that is inbuilt will turn the
> figure into whole number and not nearest 2 digit.

> How do I go about it. Is there any other routines available which take
> care of this.

> Also while browsing the table I found that figure are displayed upto 2
> decimals but I feel there may be values which have greater than 2 digit
> figure which are causing problems. How do I locate them.

------------------------------------------------------------------
Sundial Services :: Scottsdale, AZ (USA) :: (480) 946-8259

Quote:
> Fast(!), automatic table-repair with two clicks of the mouse!
> ChimneySweep(R):  "Click click, it's fixed!" {tm}
> http://www.sundialservices.com/products/chimneysweep

Sun, 05 Jan 2003 03:00:00 GMT
Rounding Off or Precision Error
Try this

Function RoundScale(x: Extended; scale: Integer): Extended;
var intero : extended;
begin
If x>0 then intero := int(x*Power(10, scale) + 0.5)
else if x<0 then intero := int(x*Power(10,scale) - 0.5)
else intero := 0;
Result := intero / Power(10, scale);
end;

Robert

Quote:
> Hello

> Recently I have observed that many of my total are going off by .01 which
> seems to be due to precision and rounding off error.

> I looked for a round function but the one that is inbuilt will turn the
> figure into whole number and not nearest 2 digit.

> How do I go about it. Is there any other routines available which take
> care of this.

> Also while browsing the table I found that figure are displayed upto 2
> decimals but I feel there may be values which have greater than 2 digit
> figure which are causing problems. How do I locate them.

> Any help wiill be greatly appreciated.

> TIA

> Natwar Lath
> Rourkela India

Sun, 05 Jan 2003 03:00:00 GMT
Rounding Off or Precision Error

Quote:

> No, it is even stranger !

> Round(0,5) = 0 Round(1,5) = 1 Round(2,5) = 2 Round(3,5) = 3 Round(4,5) = 4
> Round(5,5) = 6 Round(6,5) = 7 Round(7,5) = 8 Round(8,5) = 9 Round(9,5) = 10
> Round(10,5) = 10 Round(11,5) = 11 Round(12,5) = 12 Round(13,5) = 13
> Round(14,5) = 14
> Round(15,5) = 16 Round(16,5) = 17 Round(17,5) = 18 Round(18,5) = 19
> Round(19,5) = 20
> etc.

With D3 and D1 I get different rounding results, and they both differ from
the results above. Here's the code how I did run the test:

procedure TForm1.Button1Click(Sender: TObject);
begin
end;

And here are the resulting Memo1 contents:

Delphi-1 results
----------------
0.5=1
1.5=2
2.5=3
3.5=4
4.5=5
5.5=6
6.5=7
7.5=8
8.5=9
9.5=10
10.5=11
11.5=12
12.5=13
13.5=14
14.5=15
15.5=16
16.5=17
17.5=18
18.5=19
19.5=20

Delphi-3 results
----------------
0.5=0
1.5=2
2.5=2
3.5=4
4.5=4
5.5=6
6.5=6
7.5=8
8.5=8
9.5=10
10.5=10
11.5=12
12.5=12
13.5=14
14.5=14
15.5=16
16.5=16
17.5=18
18.5=18
19.5=20

For me, the Delphi-1 rounding results look the most usable.

Markku Nevalainen

Mon, 06 Jan 2003 03:00:00 GMT
Rounding Off or Precision Error

Quote:

> > No, it is even stranger !

--cut--
Maybe D1 wasn't using co-processor to do rounding as 32-bit Delphi does.

Quote:
> For me, the Delphi-1 rounding results look the most usable.

For me too :)

--
PSky

Tue, 07 Jan 2003 03:00:00 GMT

 Page 1 of 1 [ 8 post ]

Relevant Pages