EmbeddedRelated.com
Forums

8051 floating pt routines

Started by Unknown February 22, 2005
On Wed, 23 Feb 2005 20:34:53 GMT, the renowned "Nicholas O. Lindan"
<see@sig.com> wrote:

>"Hans-Bernhard Broeker" <broeker@physik.rwth-aachen.de> wrote >> Spehro Pefhany <speffSNIP@interlogdotyou.knowwhat> wrote: > >> > 'e did axe for decimal. Any of these decimal? >> I guess we all silently agree that he didn't really mean what he said, >Same here: a stranger walks up and says "I just murdered your wife." Well >I would think he is just having a bad med day. Wouldn't even >cross my mind he was serious. > >OK, now as to BCD floating point: > >What is the reason for _needing_ decimal? >And why _floating point_ decimal?
A couple of possibilities-- the need to both do floating point and to represent decimal fractions exactly. Or wanting to avoid the overhead of conversion to/from decimal for display and/or communications. Or, more likely, he doesn't know exactly what he wants. The request for log and sqrt may imply that. It's not really any more difficult to write a math package to handle BCD than for binary on something like an 8051, but of course the expensive math operations such as divide and multiply tend to run slower (or require tables etc.). Best regards, Spehro Pefhany -- "it's the network..." "The Journey is the reward" speff@interlog.com Info for manufacturers: http://www.trexon.com Embedded software/hardware/analog Info for designers: http://www.speff.com
"Paul Keinanen" <keinanen@sci.fi> wrote

> The only situation I can think of this might be useful is in some > financial calculations with strange rounding rules, especially in > exchange rate conversions etc. > > IIRC, before 2001 in the euro zone, the conversion from one national > currency to an other had to go through the euro, with a euro > representation of _exactly_ 5 decimal digits
I agree, accounting rules are the only compelling reason for using BCD. But the accounting specification could have just as easily specified 16 binary digits. Besides, BCD is decimalist. If only we were born with 16 fingers ... -- Nicholas O. Lindan, Cleveland, Ohio Consulting Engineer: Electronics; Informatics; Photonics. To reply, remove spaces: n o lindan at ix . netcom . com psst.. want to buy an f-stop timer? nolindan.com/da/fstop/
"Nicholas O. Lindan" wrote:
>
... snip ...
> > For interface to humans I convert float -> integer -> ASCII BCD > for display. I scale the floating point value so it is between > -9999, 9999 convert to BCD/ASCII and stick the decimal point > where I need it. There isn't much physical that needs more than > 4 digits to describe it.
Exactly. When I built my package for the 8080/z80 many moons ago I used a three byte format, with 8 bits for exponent, and 16 for significand with the implied leading 1 bit replaced by the sign. This give 4.7 decimal digits of accuracy, and all the arithmetic could be done in the registers. With care for proper rounding it outperformed the available four byte packages in both speed (by a factor of 5 to 10) and accuracy (over any extended calculations, such as a three by three matrix reduction). The system included i/o translation and formatting, and transcendental functions (trig, log, exp). The accuracy was always more than enough for the physical things it was measuring and displaying. For i/o I scaled to an integer in 6554..65535 and a power of ten, with a separate sign. Again 3 bytes, because the power of ten byte had room for a sign bit. The i/o worked with the resultant binary significands, and BCD never entered the picture. A slightly less polished version was published in DDJ. It got faster and more compact later. IIRC it fitted into a single 2k eprom. Doesn't meet the C standard requirements though. -- "If you want to post a followup via groups.google.com, don't use the broken "Reply" link at the bottom of the article. Click on "show options" at the top of the article, then click on the "Reply" at the bottom of the article headers." - Keith Thompson
On 2005-02-23, Paul Keinanen <keinanen@sci.fi> wrote:
> On Wed, 23 Feb 2005 20:34:53 GMT, "Nicholas O. Lindan" <see@sig.com> > wrote: > >>OK, now as to BCD floating point: >> >>What is the reason for _needing_ decimal? >>And why _floating point_ decimal? > > The only situation I can think of this might be useful is in some > financial calculations with strange rounding rules, especially in > exchange rate conversions etc.
Yup. Financial calcualations are pretty much the only time BCD is used. For engineering and scientific stuff, it's binary FP. -- Grant Edwards grante Yow! I like the way ONLY at their mouths move... They visi.com look like DYING OYSTERS
On Thu, 24 Feb 2005 03:09:43 GMT, the renowned CBFalconer
<cbfalconer@yahoo.com> wrote:

>"Nicholas O. Lindan" wrote: >> >... snip ... >> >> For interface to humans I convert float -> integer -> ASCII BCD >> for display. I scale the floating point value so it is between >> -9999, 9999 convert to BCD/ASCII and stick the decimal point >> where I need it. There isn't much physical that needs more than >> 4 digits to describe it. > >Exactly. <totally rude snippage>
I can't let this go without mentioning that just because 4 digits might be enough to describe something physical does NOT mean that you can get away with only 4 or 5 digits internally. I've run into a couple of situations over that years that made me glad I was paying rapt attention in university Numerical Methods class. ;-) Sometimes you can improve the algorithms to make them less sensitive, but it's not always possible, convenient or cost-effective. Best regards, Spehro Pefhany -- "it's the network..." "The Journey is the reward" speff@interlog.com Info for manufacturers: http://www.trexon.com Embedded software/hardware/analog Info for designers: http://www.speff.com
On 24 Feb 2005 04:02:41 GMT, Grant Edwards <grante@visi.com> wrote:

>Yup. Financial calcualations are pretty much the only time BCD >is used. For engineering and scientific stuff, it's binary FP.
I understand that the IEEE is currently working on extending the floating point specification to also include decimal floating numbers. If I understood the proposal correctly, the internal (binary) representation would have been quite strange. Paul
Grant Edwards wrote:
> On 2005-02-23, Paul Keinanen <keinanen@sci.fi> wrote: >> "Nicholas O. Lindan" <see@sig.com> > wrote: >> >>> OK, now as to BCD floating point: >>> >>> What is the reason for _needing_ decimal? >>> And why _floating point_ decimal? >> >> The only situation I can think of this might be useful is in some >> financial calculations with strange rounding rules, especially in >> exchange rate conversions etc. > > Yup. Financial calcualations are pretty much the only time BCD > is used. For engineering and scientific stuff, it's binary FP.
Once you have decided on decimal you might as well pick a better encoding. Excess 3 [1] has many advantages. One is that the 9's complement is the bit complement, another is symmettry. The correction after a binary add/subtract is simpler. By making the fundamental adder a subtractor you can avoid the appearance of negative zero, just as in binary operation. The sign is detected from a single bit in 9's or 10's complement arithmetic. 9's has the advantage of allowing direct display, and the disadvantage of needing end-around carries. The only thing that favors the use of BCD is the existence of such instructions as DAA (in X86) for conversion. [1] Excess 3 coding uses the binary values 0011 thru 1100 to express 0 thru 9. For addition the excess 3's add, so correction is subtract 3. For subtraction, the excess 3's cancel, so correction is add 3. The binary carry is the decimal carry. -- "If you want to post a followup via groups.google.com, don't use the broken "Reply" link at the bottom of the article. Click on "show options" at the top of the article, then click on the "Reply" at the bottom of the article headers." - Keith Thompson
Spehro Pefhany wrote:
> > On Thu, 24 Feb 2005 03:09:43 GMT, the renowned CBFalconer > <cbfalconer@yahoo.com> wrote: > > >"Nicholas O. Lindan" wrote: > >> > >... snip ... > >> > >> For interface to humans I convert float -> integer -> ASCII BCD > >> for display. I scale the floating point value so it is between > >> -9999, 9999 convert to BCD/ASCII and stick the decimal point > >> where I need it. There isn't much physical that needs more than > >> 4 digits to describe it. > > > >Exactly. <totally rude snippage> > > I can't let this go without mentioning that just because 4 digits > might be enough to describe something physical does NOT mean that you > can get away with only 4 or 5 digits internally. I've run into a > couple of situations over that years that made me glad I was paying > rapt attention in university Numerical Methods class. ;-) Sometimes > you can improve the algorithms to make them less sensitive, but it's > not always possible, convenient or cost-effective.
Of course. But that applies no matter what the precision level actually is. Knuth has a good treatment IIRC. -- "If you want to post a followup via groups.google.com, don't use the broken "Reply" link at the bottom of the article. Click on "show options" at the top of the article, then click on the "Reply" at the bottom of the article headers." - Keith Thompson
Spehro Pefhany wrote:

> I can't let this go without mentioning that just because 4 digits > might be enough to describe something physical does NOT mean that you > can get away with only 4 or 5 digits internally.
Absitvely. It is pretty easy to get into trouble with 32-bit IEEE short reals and 64 & 80 bit precision is needed. Though with some massaging of the algorithm it is often possible to reduce the precision requirements. '4-digits is good enough' applies only when talking to humans. -- Nicholas O. Lindan, Cleveland, Ohio Consulting Engineer: Electronics; Informatics; Photonics. To reply, remove spaces: n o lindan at ix . netcom . com psst.. want to buy an f-stop timer? nolindan.com/da/fstop/
"CBFalconer" <cbfalconer@yahoo.com> wrote

> Once you have decided on decimal you might as well pick a better > encoding. Excess 3 [1] has many advantages.
That takes me back to university. When BCD calculations were done with 2-input nand/nor's. And 'RTL' meant something completely different. "A micro-what? And who is 'Intel'?" Paul wrote:
> I understand that the IEEE is currently working on extending the > floating point specification to also include decimal floating numbers. > If I understood the proposal correctly, the internal (binary) > representation would have been quite strange.
Any trick that can be used with BCDs WRT to processing one digit at a time are equally applicable to processing one 16-bit digit (nibble) at a time, only more so. But I would be interested in seeing what my membership fees are up to ... -- Nicholas O. Lindan, Cleveland, Ohio Consulting Engineer: Electronics; Informatics; Photonics. To reply, remove spaces: n o lindan at ix . netcom . com psst.. want to buy an f-stop timer? nolindan.com/da/fstop/