Wrong output of Texts.WriteReal and Texts.WriteRealFix in PO
I wonder if it is known that the output of the present PO version of Texts.WriteReal and Texts.WriteRealFix for various borderline values can be completely false due to untrapped overflow of FLOOR(x)? Although it is acceptable that floating point representation of real numbers produces round-off errors, the output of obviously wrong values is undesirable. While looking into this I compared the output of these procedures with the same in my first Oberon (DOS Oberon V1.4, which I ordered from ETH Zurich in the early nineties). I don't have the source code of that early Oberon version, but I suppose it uses the source as published in the 1992 Project Oberon book. For the present PO version prof. Wirth rewrote these procedures, originally by J. Gutknecht. I used the following test module: MODULE Test1; IMPORT In, Fonts, Texts, Oberon; VAR W: Texts.Writer; PROCEDURE Do*; VAR x: REAL; k: INTEGER; BEGIN In.Open; In.Real(x); In.Int(k); IF In.Done THEN Texts.WriteReal(W, x, n); Texts.WriteRealFix(W, x, n, k) ELSE Texts.WriteString(W, "Parameter error") END; Texts.WriteLn(W); Texts.Append(Oberon.Log, W.buf) END Do; BEGIN Texts.OpenWriter(W); Texts.SetFont(W, Fonts.This("Courier10.Fnt")) END Test1.Do^ Example input & output: x k Texts.WriteReal Texts.WriteRealFix Texts.WriteRealFix (Ob.-90) 1554.70E5 1 1.554700E+08 155470003.2 155470000. 1554.70E5 2 1.554700E+08 21474836.47 155470000. 1554.70E6 0 1.554700E+09 1554700032. Trap 7 Overflow 1.0E8 1 1.000000E+08 100000000.0 100000000.0 1.0E9 1 1.000000E+09 214748364.7 Trap 7 Overflow 1.0E9 0 1.000000E+09 1000000000.0 Trap 7 Overflow 221500000.0 2 -2.079967E+08 -21474836.47 221499984. In the overview of results below I call a result containing the digits 2147483647 'overflow (Mersenne)' or 'overflow (-Mersenne)' because 2147483647 = MAX(INTEGER) = 2^(32)-1, which is a Mersenne prime. It can be seen from this table that the Oberon-90 version of Texts.WriteRealFix always gives a 'sane' result: either a proper approximation or a 'Trap 7 Overflow'. The Oberon-90 version of Texts.WriteReal never gave wrong results and never trapped in my tests. Texts.WriteRealFix in Oberon-90 prints at most 9 significant digits when there are no leading zeros; with leading zeros there are at most 13 digits after the decimal point. The present Oberon-07 versions of these procedures never trap, but regularly give wrong results, due to silent overflow of the FLOOR(x) function. x k Texts.WriteReal Texts.WriteRealFix Texts.WriteRealFix (Ob.-90) 1554.70E5 1 OK OK OK (9 digits, no decimal) 1554.70E5 2 OK overflow (Mersenne) OK (9 digits, no decimal) 1554.70E6 0 OK OK Trap 7 Overflow 1554.70E7 0 OK overflow (Mersenne) Trap 7 Overflow 1.55470E5 4 OK OK OK (9 digits, no decimal) 1.55470E5 5 OK overflow (Mersenne) OK (9 digits, no decimal) 1.0E8 1 OK OK OK (9 digits) 1.0E9 1 OK overflow (Mersenne) Trap 7 Overflow 1.0E9 0 OK OK Trap 7 Overflow 221500000.0 3 overflow (negative) overflow (-Mersenne) OK (9 digits, no decimal) 221500000.0 2 overflow (negative) overflow (-Mersenne) OK (9 digits, no decimal) 221500000.0 1 overflow (negative) overflow (negative) OK (9 digits, no decimal) 221500000.0 0 overflow (negative) overflow (negative) OK (9 digits, no decimal) 22150000.0 0 OK OK OK 22150000.0 1 OK OK OK 22150000.0 2 OK overflow (Mersenne) OK (1 decimal digit) 22150000.0 11 OK overflow (Mersenne) OK (1 decimal digit) 2215000.0 2 OK OK OK 2215000.0 3 OK overflow (Mersenne) OK (2 decimal digits) 2147483647.0 0 overflow (-1.0) overflow ( -1.) Trap 7 Overflow 2147483648.0 1 overflow ( 0 ) overflow ( 0 ) Trap 7 Overflow 2147483649.0 0 overflow ( 1.0) overflow ( 1.) Trap 7 Overflow 214748364.0 0 OK OK OK 2147484000.0 0 overflow (352.) overflow (352.) Trap 7 Overflow 214748400.0 0 overflow (-Mersenne) overflow (-Mersenne) OK (9 digits, no decimal) 214740000.0 0 OK OK OK (9 digits, no decimal) 214740000.0 1 OK OK OK (9 digits, no decimal) 214740000.0 2 OK overflow (Mersenne) OK (9 digits, no decimal) 0.000012345 9 OK .0000123 0.000012345) 0.000012345 11 OK .0000123 0.00001234500) 0.000012345 13 OK .0000123 0.0000123449984) 0.000012345 14 OK .0000123 0.0000123449984) -------------- Mersenne: 2147483647 (with decimal point) -Mersenne: -2147483647 (with decimal point) I tried to find a way to catch such overflow of FLOOR (so that an ASSERT(FALSE) run time trap can be produced), but was unable to find a satisfactory solution. The only special cases that I could catch were (FLOOR(x) = 2147483647) OR (FLOOR(x) = -2147483647), but this is not a real solution. I also ported Jürg Gutknecht's Texts.WriteStringFix version to Oberon-07, but I could not think up an implementation of Reals.Convert(x, n, d) without using FLOOR(x)... Does anyone have a suggestion? Could it be that the TRAP7 is not due to integer overflow but to an array index overflow? -- Hans ----------- (* Below is my port to Oberon-07 of Jürg Gutknecht's original Oberon-90 version of WriteRealFix from the book Project Oberon (1992) *) PROCEDURE Ten(n: INTEGER): REAL; VAR t, p: REAL; BEGIN t := 1.0; p := 10.0; (*compute 10^n *) WHILE n > 0 DO IF ODD(n) THEN t := p * t END ; p := p*p; n := n DIV 2 END ; RETURN t END Ten; PROCEDURE Convert (x: REAL; n: INTEGER; VAR d: ARRAY OF CHAR); (* Convert REAL x to a string of n characters. This version still is unsatisfactory, because FLOOR overflows silently. *) VAR i, k: INTEGER; BEGIN i := FLOOR(x); k := 0; WHILE k < n DO d[k] := CHR(i MOD 10 + 30H); i := i DIV 10; INC(k) END END Convert; PROCEDURE WriteRealFix* (VAR W: Texts.Writer; x: REAL; n, k: INTEGER); CONST maxD = 9; (* maximal number of digits written, apart from leading zeros *) VAR e, i: INTEGER; sign: CHAR; x0: REAL; d: ARRAY maxD OF CHAR; (* digits *) PROCEDURE seq (VAR W: Texts.Writer; ch: CHAR; n: INTEGER); (* Write a sequence of n characters ch *) BEGIN WHILE n > 0 DO Texts.Write(W, ch); DEC(n) END END seq; PROCEDURE dig(VAR W: Texts.Writer; d: ARRAY OF CHAR; VAR i: INTEGER; n: INTEGER); (* Write n digits taken from string d *) BEGIN WHILE n > 0 DO DEC(i); Texts.Write(W, d[i]); DEC(n) END END dig; BEGIN e := ASR(ORD(x), 23) MOD 100H; (*binary exponent*) IF k < 0 THEN k := 0 END; IF e = 0 THEN seq(W, " ", n-k-2); Texts.Write(W, "0"); seq(W, " ", k+1) ELSIF e = 255 THEN Texts.WriteString(W, " NaN"); seq(W, " ", n-4) ELSE e := (e - 127) * 77 DIV 256; (* decimal exponent *) IF x < 0.0 THEN sign := "-"; x := -x ELSE sign := " " END; IF e >= 0 THEN (*x >= 1.0, 77/256 = log 2*) x := x/Ten(e) ELSE (*x < 1.0*) x := Ten(-e) * x END; IF x >= 10.0 THEN x := 0.1*x; INC(e) END; (* 1 <= x < 10 *) IF k+e >= maxD-1 THEN k := maxD-1-e ELSIF k+e < 0 THEN k := -e; x := 0.0 END; x0 := Ten(k+e); x := x0*x + 0.5; IF x >= 10.0*x0 THEN INC(e) END; (*e = no. of digits before decimal point*) INC(e); i := k+e; Convert(x, i, d); IF e > 0 THEN seq(W, " ", n-e-k-2); Texts.Write(W, sign); dig(W, d, i, e); Texts.Write(W, "."); dig(W, d, i, k) ELSE seq(W, " ", n-k-3); Texts.Write(W, sign); Texts.Write(W, "0"); Texts.Write(W, "."); seq(W, "0", -e); dig(W, d, i, k+e) END END END WriteRealFix;
-----Original Message----- From: Oberon [mailto:oberon-bounces@lists.inf.ethz.ch] On Behalf Of Hans Klaver Sent: Sunday, 4 October 2020 7:08 AM To: ETH Oberon and related systems Subject: [Oberon] Wrong output of Texts.WriteReal and Texts.WriteRealFix in PO
I wonder if it is known that the output of the present PO version of Texts.WriteReal and Texts.WriteRealFix for various borderline values can be completely false due to untrapped overflow of FLOOR(x)? Although it is acceptable that floating point representation of real numbers produces round-off errors, the output of obviously wrong values is undesirable.
While looking into this I compared the output of these procedures with the same in my first Oberon (DOS Oberon V1.4, which I ordered from ETH Zurich in the early nineties). I don't have the source code of that early Oberon version, but I suppose it uses the source as published in the 1992 Project Oberon book. For the present PO version prof. Wirth rewrote these procedures, originally by J. Gutknecht.
Traps that are implemented in Project Oberon 2013 are defined in section 12.7.10: 1 array index out of range 2 type guard failure 3 array or string copy overflow 4 access via NIL pointer 5 illegal procedure call 6 integer division by zero 7 assertion violated Note that this differs from the traps defined in section 12.9 of the 1992 / 2005 version of Project Oberon: 2 Address trap: typically reference via NIL pointer 3 Floating-point trap: overflow 6 Division by zero 7 Flag trap: invalid index 13 Integer trap (or invalid index): overflow 18 Type guard failure You will see that Floating-point and Integer overflow were detected and trapped in the older system but not in the current one. There are valid arguments for and against either approach. E.g. a trade-off between efficiency and protecting programmers from themselves. Consequently, programmers of any Project Oberon 2013 applications that could result in numeric overflow need to ensure that the desired resulting behaviour is clearly defined in the requirements and that they design, program and test their applications accordingly. Regards, Chris Chris Burrows CFB Software https://www.astrobe.com
Chris:
Consequently, programmers of any Project Oberon 2013 applications that could result in numeric overflow need to ensure that the desired resulting behaviour is clearly defined in the requirements and that they design, program and test their applications accordingly.
I do not think it is right. Numerical results must be either correct or none. Returning a NaN or a trap is tolerable, though troublesome. Returning a wrong result cannot be accepted as valid. There are enough experts in the community to fix this problem. Perhaps Robert's Component Pascal numerical libraries can help? Thank you, Wojtek
On Sa, 2020-10-03 at 23:30 +0000, Skulski, Wojciech wrote:
I do not think it is right. Numerical results must be either correct or none. Returning a NaN or a trap is tolerable, though troublesome. Returning a wrong result cannot be accepted as valid.
I fully second this statement. And the definitive standard how to do that correctly is IEEE 754. If you don't believe me, read the failure stories in, e.g., Hennessy/Patterson's "Computer Organization & Design".
There are enough experts in the community to fix this problem. Perhaps Robert's Component Pascal numerical libraries can help?
I suspect the floating-point hardware itself isn't fit for IEEE 754, although I didn't investigate that. If RISC5 is meant to be a serious tool for floating-point tasks, this area must be cleared up, IMHO. Hellwig
I suspect the floating-point hardware itself isn't fit for IEEE 754 You‘re right; NaN (NaNs and NaNq) is not implemented in RISC5. Converting correctly strings to/from floating is surprisingly an extremely difficult task.
One quick remark: It is true that FLOOR cannot handle values >MAX (2^31-1=2147483647) as those values cannot be stored in an RISC-5 INTEGER. Remember that on RISC-5 i = FLOOR(FLT(i)) is not guaranteed to be true for INTEGERs i > 16777216 br Jörg
Joerg: let me throw in a few observations on the floating point in FPGA Oberon. 1) You said that erroneous conversion from internal binary to the ASCII string is "surprisingly an extremely difficult task". Fortunately, printing the numbers is "just software" which can be fixed, though I am not sure when and by whom. 2) But you also said that "floating-point hardware itself isn't fit for IEEE 754". This looks more serious to me. The FP *firmware* in FPGA Oberon consists of FPAdder.v, FPMultiplier.v, and FPDivider.v. The documentation in Chapter 16 of PO.Computer consists of only two pages (starts on page 8). These pages do not look like comprehensive a proof that the algorithm has been accurately and correctly expressed in the Verilog code. 3) I am not aware of test results proving the correct operation of these Verilog modules in corner cases. How sure can we be that the results are always correct, if there are no test results to prove it? An alternative to present firmware might consist of using well tested cores, either open source or commercial. I made a quick search for floating point firmware implementations. I searched Google for "xilinx floating point dsp slice". Here are some results. An interesting paper "ITERATIVE FLOATING POINT COMPUTATION USING FPGA DSP BLOCKS" by Fredrik Brosser, Hui Yan Cheah, Suhaib A. Fahmy is available from the web. It mentions FloPoCo among other topics. Their implementation is worth looking at because quite likely Wirth's implementation is quite similar. (Search the web for 10.1.1.703.3437.pdf) Open source arithmetic and floating point cores. ------------------------------------------------- 4) A comprehensive open source website "FPGA Arithmetic the way it should be". The documentation is very comprehensive and the code itself is supported by the authors. 4.1. Home page http://flopoco.gforge.inria.fr/ 4.2. Operator list for FloPoCo version 4.1.2 http://flopoco.gforge.inria.fr/operators_4.1.2.html 4.3. Two page "Reflections on 10 years of FloPoCo" is linked in the 1st paragraph of 4.1. Xilinx cores available for free under either ISE or Vivado. ---------------------------------------------------------------- Neither one is open source, but they can be instantiated for free in Xilinx projects. They are extensively documented and tested. Their interfaces look nasty to me, but they can be wrapped into the present Oberon firmware interfaces. 5) ISE 14.x Core Generator is providing Floating Point operator v 5.0 for Spartan-3 and upwards. Described in data sheet DS335 March 1, 2011 included in the ISE 14.x Core Generator Supported operations: • multiply • add/subtract • divide • square-root • comparison • conversion from floating-point to fixed-point • conversion from fixed-point to floating-point • conversion between floating-point types 6) Vivado is providing Floating Point operator v 7.x for Series-7 and upwards, described in Product Guide PG060 (LogiCORE IP Floating-Point Operator v7.0). Supported operators: ° Multiply ° Add/subtract ° Accumulator ° Fused multiply-add ° Divide ° Square-root ° Comparison ° Reciprocal ° Reciprocal square root ° Absolute value ° Natural logarithm ° Exponential ° Conversion from floating-point to fixed-point ° Conversion from fixed-point to floating-point ° Conversion between floating-point types My assessment of Xilinx Floating Point operators ---------------------------------------------- ° Interfaces are nasty. They will require unpleasant Verilog or VHDL wrappers. ° Extensively documented and tested. Unlikely to contain major bugs. ° Compatible with IEEE standard. Departures from the standard are documented.. ° Not portable to non-Xilinx FPGAs ° Operator 5.0 does not provide transcendental functions ° Operator 7.0 provides more functions, including logarithm and exponential My assessment of FloPoCo ---------------------------------------------- ° Promising, but requires more time to digest the documentation. (The reverse of the present Oberon FP firmware.) Wojtek ________________________________________ From: Oberon [oberon-bounces@lists.inf.ethz.ch] on behalf of Joerg [joerg.straube@iaeth.ch] Sent: Sunday, October 4, 2020 3:38 AM To: ETH Oberon and related systems Subject: Re: [Oberon] [EXT] Re: Wrong output of Texts.WriteReal and Texts.WriteRealFix in PO
I suspect the floating-point hardware itself isn't fit for IEEE 754
You‘re right; NaN (NaNs and NaNq) is not implemented in RISC5. Converting correctly strings to/from floating is surprisingly an extremely difficult task. One quick remark: It is true that FLOOR cannot handle values >MAX (2^31-1=2147483647) as those values cannot be stored in an RISC-5 INTEGER. Remember that on RISC-5 i = FLOOR(FLT(i)) is not guaranteed to be true for INTEGERs i > 16777216 br Jörg
participants (5)
-
Chris Burrows -
Hans Klaver -
Hellwig Geisse -
Joerg -
Skulski, Wojciech