Issue 4658: Python mapping issue: fixed point (python-ftf) Source: AT&T (Dr. Duncan Grisby, ) Nature: Uncategorized Issue Severity: Summary: 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. * Operations as follows: - 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'. Resolution: Accept the suggested changes Revised Text: In section 1.3.2, replace the paragraphs starting with "A fixed point type fixed<foo,bar>>" and ending with "in the case of anonymous fixed types" with the following 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: o 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. o The digits and scale values, followed by a conforming string. The string is treated as with named types described above. o 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. · Operations as follows: o value() returns an integer or long integer representing the digits of the fixed point number, in the form accepted by the constructors. o precision() returns the number of digits. o decimals() returns the scale. o round(scale) returns a new fixed point number containing the original number rounded to the specified scale. o 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'. Actions taken: November 5, 2001: received issue October 23, 2002: closed issue Discussion: End of Annotations:===== To: issues@omg.org Subject: Python mapping issue: fixed point From: Duncan Grisby Date: Mon, 05 Nov 2001 12:03:07 +0000 Sender: dpg1@uk.research.att.com Content-Type: text X-UIDL: =8gd9^I9!!X%F!!mBNd9 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 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. * Operations as follows: - 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'. -------- Cheers, Duncan. -- -- Duncan Grisby \ Research Engineer -- -- AT&T Laboratories Cambridge -- -- http://www.uk.research.att.com/~dpg1 --