> From: Larry Bradley[SMTP:lhbradley@IGS.NET] > Sent: Thursday, December 18, 2003 7:26 AM > To: PICLIST@MITVMA.MIT.EDU > Subject: Re: [PIC]: Converting floating point to decimal > I've done that sort of thing; I use it for debugging code that uses FP. But > that requires some a priori knowledge of the range of the number in > question. For example, if I know that I'm dealing with a number in the > range 100.00 to 999.99, then I can just multiply by 100 and put the decimal > point in the right place. However, I was assuming (perhaps incorrectly) > that there was a generalized way to do this, where I could convert any > number to ASCII. I'd like to be able to use scientific notation as well, if > necessary. > It's more a challenge than a necessity, as it's hard to imagine a case > where I wouldn't have some idea of the range of the numbers I want to > display. I'm doing this already. After reading your first post, I started thinking about this. There does seem to be a method, but I need more time to refine it. There is a clever binary-to-BCD algorithm in the Microchip application note which describes utility math operations (AN526. appendix K). The method involves left shifting the BCD partial result and then "fixing up" the result. After repeating this N times for an N-bit binary number, the BCD result is the value of the original binary number. On close examination, you can see that the shift and fixup operation amounts to multiplying the BCD result by 2. It seems to me that performing this procedure in reverse will divide the BCD number by 2. In fact, it does. This means that if you start with 50 00 00 00 ... in a BCD register, you can successively create 25 00 00 00 ... 12 50 00 00 ... 06 25 00 00 ... 03 12 50 00 .. If you add these BCD numbers to a BCD accumulator, using the binary fraction as a guide for when to add, the result will be the decimal fraction which is equivalent to the original binary fraction. To handle scientific notation, you must deal with large exponents. Doing this quickly and with optimized code is the part I am still thinking about. John Power -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body