From -8651791047699569462
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,fed4da4ddd62c919
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 1995-01-04 16:37:29 PST
Path: nntp.gmd.de!Germany.EU.net!howland.reston.ans.net!news.sprintlink.net!hookup!olivea!koriel!male.EBay.Sun.COM!engnews2.Eng.Sun.COM!usenet
From: clamage@Eng.Sun.COM (Steve Clamage)
Newsgroups: comp.std.c++
Subject: Re: Packed Decimal and C++
Date: 4 Jan 1995 01:48:34 GMT
Organization: Sun Microsystems Inc.
Lines: 40
Message-ID: <3ecupi$q5@engnews2.Eng.Sun.COM>
References: <D1uv51.Frr@world.std.com>
Reply-To: clamage@Eng.Sun.COM
NNTP-Posting-Host: taumet.eng.sun.com

In article Frr@world.std.com, miket@world.std.com (Michael Trachtman) writes:
>
>My problem is that I have to represent amounts ranging from
>$ .0001 (100ths of a penny) through 100,000,000,000 (100 billion) dollars.
>To do this in integer requires about 43 bits of accuracy.
>I.e. the ratio of the smallest number to the largest is about 1000 ^ 4.x.
>It takes about 10 bits for each power of a thousand. (2 ^ 10  ~= 1000).
>
>Regular integers, even the 32 bit kind won't work. Double precision
>floating point will, however there will be roundoff error.
>
>For accounting purposes, we have to match things exactly to the
>accounting system. Thus, we need either 64 bit integers
>or BCD, or some other arbitrary precision representation,
>that guarantees at least  16 digits or so of decimal accuracy. 

Typical implementations of type double provide about 53 bits of
precision. If you scale the numbers in 100ths of a penny (that is
the value 100 means one penny, 10000 means one dollar) you will
not have any fractional values (unless you perform a division
which needs better than $0.0001 precision).

You can hide the scaling inside a class, and the internal representation
will always be an integral value. The representation will be exact as
long as the true value can be represented in no more than (typically) 53
bits. This is (just barely) true for the range you mention. (You indicated
43 bits above, but it is really about 52 or 53 bits.)

Example: $10.5 plus $1.0022 gets calculated as if you wrote

	105000 + 10022

The result is in 100ths of a penny. Output is formatted by manipulation
of the text representation, not by dividing by 10000. Again, the
manipulations are hidden in the class implementation.

---
Steve Clamage, stephen.clamage@eng.sun.com




