
I think the discussion on the decimal went in the wrong direction and is it probably my own fault. Lets start it over in more general terms. My view on decimal types are that they can be based on 4 different base types 1. binary floating-point, explicit rounding 2. decimal floating-point, explicit rounding 3. decimal fixed-precision, global rounding 4. decimal fixed-precision, object rounding * Explicit rounding means that all operations store the full precision supported by the type. Rounding to a number of decimals is performed when needed by a (maybe implicit) call to a function like round(x, rounding_mode). Ofcourse there is a rounding mode used to fit the result of an operation into the type which might cause rounding errors (e.g. 0.499999 might first be rounded to 0.5000 to fit the type which is then explicitly rounded to 1). I think the cases where the double rounding is a problem are so rare that it is enough to point out the problem in the documentation. * global rounding means the same rounding is used for all objects. * object rounding means that the rounding is specified for each object. The decimal type I proposed was in category 4. The proposed TR decimal type is category 2 and it might also support type 3. Float, double is category 1. --- The discussion on decimal types, including the review of a decimal library two years ago, indicates that to be accepted in boost a solution should allow some (or all 4) types to be used as base in something like "basic_decimal<>". So lets discuss how a basic_decimal interface should look like. The main problem I see lies in the construction: Type 1 & 2: basic_decimal(value) Type 3: basic_decimal(value, precision) Type 4: basic_decimal(value, precision, rounding_mode) Rounding is not a big problem since a sensible default can be used but how to solve the precision? Creating a decimal type 3 or 4 without specifying a precision is bad since there is no good value to use as default. Precision also has a different meaning for a decimal type 1 & 2 (i.e. only affects extractions, not operations). I see three possible solutions: 1. basic_decimal include all variants of constructors and then generates compiler error if that type of constructor isn't suitable for the underlying type 2. basic_decimal only got the "full" constructor (value, precision, rounding_mode) but supplies default for precision (=0?) and rounding mode 3. basic_decimal only includes the minimal constructor (value) and then other members are needed to specify precision and rounding_mode for type 3 & 4 decimals. The other members of basic_decimal isn't very complicated: A full set of arithmetic operations (+,-,*,/,%) and assignment must be included. Operations should work on integers and maybe also string literals. For decimal type 1 & 2 it makes sense to also allow operations on doubles. Assignment should work on integers, string literals and double for all types. There needs to be a way to extract the decimal as a binary floating point value and probably also a decimal floating point value (when available). Functions like round, abs etc are also needed but they can be free functions (maybe friends for performace reasons). Note that the above only deals with the decimal type. Money and currency might need a slightly different interface. My view is that the types should built up like: typedef basic_decimal<value_type> decimal_type; typedef basic_money<decimal_type> money_type; typedef basic_currency<money_type> currency_type; Any comments?

Martin wrote: [...]
--- The discussion on decimal types, including the review of a decimal library two years ago, indicates that to be accepted in boost a solution should allow some (or all 4) types to be used as base in something like "basic_decimal<>".
So lets discuss how a basic_decimal interface should look like. The main problem I see lies in the construction:
Type 1 & 2: basic_decimal(value) Type 3: basic_decimal(value, precision) Type 4: basic_decimal(value, precision, rounding_mode)
Rounding is not a big problem since a sensible default can be used but how to solve the precision? Creating a decimal type 3 or 4 without specifying a precision is bad since there is no good value to use as default. Precision also has a different meaning for a decimal type 1 & 2 (i.e. only affects extractions, not operations).
IMO any kind of automatic rounding and precision should not be part of a basic_decimal type at all! The behaviour of basic_decimal should stick to the decimal proposal of Mike Cowlishaw and he explains very well in his FAQ section why it's not a good idea to have normalized decimal arithmetic: http://www2.hursley.ibm.com/decimal/decifaq.html However, it could be possible to provide some add on, maybe controlled by a policy, that also handles rounding, precision and normalization. [...]
The other members of basic_decimal isn't very complicated: A full set of arithmetic operations (+,-,*,/,%) and assignment must be included. Operations should work on integers and maybe also string literals. For decimal type 1 & 2 it makes sense to also allow operations on doubles. Assignment should work on integers, string literals and double for all types.
These are the operations of IEEE 754R; they should be at least supported in some way. * abs * add and subtract * compare * divide * divide-integer * max * min * minus and plus * multiply * normalize * quantize * remainder * remainder-near * round-to-integral-value * same-quantum * square-root * power And don't forget relational operators.... Stefan

IMO any kind of automatic rounding and precision should not be part of a basic_decimal type at all!
I assume you mean that we don't need a decimal type with a fixed number of decimals. There is an extension to the propsed standard that allows fixed number of decimals so there seem to be some who need it. I know that I need fixed number of decimals in the software I am working with and that I often both have objects with round_to_nearest and round_toward_zero in the same application. If I had a decimal floating point type available I could probably have used it. It would make some things easier and other things more difficult.
The behaviour of basic_decimal should stick to the decimal proposal of Mike Cowlishaw and he explains very well in his FAQ section why it's not a good idea to have normalized decimal arithmetic:
Why do you mention "normalized decimal arithmetic"? Isn't that a way store floating point numbers or do you mean something else.

Martin wrote:
IMO any kind of automatic rounding and precision should not be part of a basic_decimal type at all!
I assume you mean that we don't need a decimal type with a fixed number of decimals. There is an extension to the propsed standard that allows fixed number of decimals so there seem to be some who need it.
No, I'm sure that wasn't my intention.
The behaviour of basic_decimal should stick to the decimal proposal of Mike Cowlishaw and he explains very well in his FAQ section why it's not a good idea to have normalized decimal arithmetic:
Why do you mention "normalized decimal arithmetic"? Isn't that a way store floating point numbers or do you mean something else.
Did you read this proposal? br, Stefan
participants (3)
-
Martin
-
Stefan Slapeta
-
Stefan Slapeta