The Python mapping for fixed point types is slightly unclear, and
could benefit from a few of the facilities from other language
mappings.
Below, I've written a proposed specification to replace the existing
section on fixed point. The changes are:
- replace "foo" and "bar" with "digits" and "scale"
- clarify meaning of integers used in constructor and value() method
- add a constructor taking a string
- correct "loss of precision" to be "overflow"
- add requirement for string conversion with str()
- add round() and truncate() methods
- clarify usage of CORBA.fixed() and add string-based versions
- relax the requirement that certain entities are classes
Here is the suggested replacement text:
------
IDL of the form
typedef fixed<digits,scale> MyFixed;
is mapped as follows:
- A constructor MyFixed() expecting either a string representing the
fixed point value, or an integer type representing the digits of
the value.
The string form of the constructor accepts a string representation
of a fixed point literal, with the trailing 'd' or 'D' optional.
The value is truncated if too many digits are given after the
decimal point. If there are too many digits before the decimal
point, or the string is not a valid fixed point value, a
CORBA.DATA_CONVERSION exception is raised.
The integer form of the constructor accepts a Python integer or
long integer, representing the digits of the fixed point value.
The integer is numerically the fixed point value * 10 ** scale.
If the integer has too many digits, CORBA.DATA_CONVERSION is
raised.
e.g. given IDL:
typedef fixed<5,2> MyFixed;
the following is true:
MyFixed("123.45") == MyFixed(12345)
- To facilitate the use of anonymous fixed point values, a generic
CORBA.fixed() constructor is provided. Its arguments take three
possible forms:
- A single string representing the fixed point value, with a
trailing 'd' or 'D' optional. The resulting fixed point value
derives its digits and scale from the string. Raises
DATA_CONVERSION if the value exceeds the size of CORBA fixed,
or the string is invalid.
- The digits and scale values, followed by a conforming string.
The string is treated as with named types described above.
- The digits and scale values, followed by a conforming integer or
long integer. The integer is treated as with named types
described above.
e.g.
a = CORBA.fixed("123.45")
b = CORBA.fixed(5, 2, "123.45")
c = CORBA.fixed(5, 2, 12345)
assert(a == b)
assert(b == c)
The result of calling either kind of constructor is an object with the
following properties:
- Numeric operators for addition, subtraction, multiplication, and
division, both of two fixed point numbers, and in combination with
integers. A DATA_CONVERSION exception is raised if the operation
results in an overflow.
- value() returns an integer or long integer representing the
digits of the fixed point number, in the form accepted by the
constructors.
- precision() returns the number of digits.
- decimals() returns the scale.
- round(scale) returns a new fixed point number containing the
original number rounded to the specified scale.
- truncate(scale) returns a new fixed point number containing the
original number truncated to the specified scale.
- When a fixed point number is passed to the standard str()
function, a string representing the fixed point value is returned.
The string does not contain a trailing 'd'.