From: "Robert I. Eachus" <rieachus@attbi.com>
Subject: Re: Floating Decimal Package/Library?
Date: Mon, 22 Jul 2002 23:33:01 GMT
Date: 2002-07-22T23:33:01+00:00 [thread overview]
Message-ID: <3D3C963D.5020304@attbi.com> (raw)
In-Reply-To: 3D3571E4.6060503@cogeco.ca
Warren W. Gay VE3WWG wrote:
> Are you sure that it is "binary fixed point". Is Robert lurking
> about here?
You may have been looking for Robert Dewar, but I think I am at least
one of the right Roberts to answer this.
Ada decimal fixed point types are decimal. Unless you insist on peeking
under the covers, you need know nothing else. But if you insist on
peeking, read on.
Decimal types are implemented as (almost always twos-complement) binary
integer representations, with a static divisor that is a power of ten.
So if you say:
type Dollars is delta 0.01 digits 10;
You get a type that can exactly represent hundredths of dollars, and
more important, cannot represent any value that is not an exact multiple
of one cent. (Or more generally all values of the type must be a
multiple of 'Small.)
However, there is another thing you may care about. The maximum
representable value is not specified for the type Dollars above. The
implementation (compiler) may choose any representation as long as all
values from -99_999_999.99..99_999_999.99 are exactly representable.
But the actual data type may allow larger values. In this case, I would
not be surprised if the representation used 64-bit signed integers, and
allowed values up to 184_467_440_737_095_516.15. But if you want to
restrict the values of the type you can put a range constraint on the
type declaration:
type Dollars is delta 0.01 digits 10
range -99_999_999.99..99_999_999.99;
My recommended approach for Cobol/SQL style programming is to declare
most base types like Dollars without a range constraint, and have
constrained subtypes:
subtype Salary is Dollars range 0.0..10_000_000.00;
...for all database fields. If the underlying database wants to store
this as a string, seems wasteful to me but okay.
Now for the really gory stuff. What about PostGres support for a
thousand digits of floating decimal arithmetic? Chump change. ;-) In
Ada the results of certain operations on fixed point types are required
to be represented exactly. In practice values of type _universal_fixed_
exist only to make your life easier. (And sometimes the life of the
compiler implementor more complex.) For example, in the calculation:
Base_Wages := Dollars(Hours_Worked * Base_Pay);
Hours_Worked might be represented in tenths of hours and Base_Pay in
mills (thousanths of a dollar). Doesn't matter, the result of the
multiplication will only be rounded once, to the Dollars type, and will
only overflow if the result is outside the bounds of Dollars (or
Base_Wages!!!). If you don't want rounding, but truncation to the
nearest cent use:
Base_Wages := Dollars'Truncation(Hours_Worked * Base_Pay);
...but be sure to check the union contract first. ;-)
next prev parent reply other threads:[~2002-07-22 23:33 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-07-12 17:19 Floating Decimal Package/Library? Warren W. Gay VE3WWG
2002-07-12 22:58 ` Robert Wessel
2002-07-12 23:30 ` Dann Corbit
2002-07-15 13:34 ` Warren W. Gay VE3WWG
2002-07-16 5:04 ` Robert C. Leif
2002-07-16 7:01 ` tmoran
2002-07-16 15:50 ` Robert C. Leif
2002-07-17 1:24 ` tmoran
2002-07-17 2:53 ` Robert C. Leif
2002-07-17 4:41 ` tmoran
2002-07-17 16:30 ` Warren W. Gay VE3WWG
2002-07-17 22:09 ` tmoran
2002-07-29 17:05 ` Warren W. Gay VE3WWG
2002-07-29 17:41 ` tmoran
2002-07-30 8:58 ` Jean-Pierre Rosen
2002-07-30 16:20 ` John H. Lindsay
2002-07-16 16:32 ` Pascal Obry
2002-07-16 17:53 ` Warren W. Gay VE3WWG
2002-07-17 1:24 ` tmoran
2002-07-17 8:28 ` Vadim Godunko
2002-07-17 13:32 ` Warren W. Gay VE3WWG
2002-07-22 23:33 ` Robert I. Eachus [this message]
2002-07-23 13:16 ` Marin David Condic
2002-07-24 15:18 ` Darren New
2002-07-24 15:43 ` Hyman Rosen
2002-07-24 16:17 ` Fraser Wilson
2002-07-17 13:30 ` Warren W. Gay VE3WWG
2002-07-13 21:55 ` Björn Lundin
2002-07-15 13:37 ` Warren W. Gay VE3WWG
2002-07-17 21:56 ` Waldek Hebisch
2002-07-18 14:13 ` Robert C. Leif
2002-07-19 14:41 ` Waldek Hebisch
2002-07-19 17:29 ` Warren W. Gay VE3WWG
2002-07-19 21:50 ` Waldek Hebisch
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox