Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
326 Floating Point Operations 3. The following algorithm approximates the cube root of a real number x. root := 1.0; repeat oldRoot := root; root := (2.0*root + x/(root*root)) / 3.0; until (|root oldRoot| < smallValue); Implement this design in a console32 program, embedding the value for x in the data section, using 0.001 for smallValue, and leaving root in ST where you can easily examine it with the debugger. Test your program with different values for x. 9.3 Converting Floating Point to and from ASCII This section describes a procedure to facilitate conversion of decimal numbers to float- ing point format from a readable (ASCII) format, and a procedure that converts from float- ing point to ASCII. With these procedures and the input and output macros from io.h, we then modify the quadratic equation solver in Figure 9.13 to input coefficients and output roots. Section 9.5 covers an alternative way of providing I/O--by making the equation solver a procedure that is called from C and C++ main programs that do the I/O. The ASCII to floating point conversion algorithm is given in Figure 9.15. It is similar to the algorithm used for the procedure atodproc described in Section 7.3. It scans memory at the address given by its parameter, interpreting the characters as a floating point number. It looks for a leading minus sign and a decimal point. The scan is terminated by a nondigit. This algorithm is implemented in a procedure atofproc with one parameter, the address of the string. It returns the floating point value in ST. The procedure code appears in Figure 9.16, along with a simple console32 test driver. This implementation of the ASCII to floating point algorithm (Figure 9.16) uses ST for value and ST(1) for divisor except for one short segment where they are reversed in order to modify divisor. After the procedure entry code, the instructions fld1 fldz ; divisor := 1.0 ; value := 0.0