**1**or**123**: decimal fixed integers without decimal dot,**12.3**: decimal floating point numbers with decimal dots,**-1.234**: negative decimal floating point numbers with decimal dot,**1.2345E2, 1.2345E+2, 1.2345E+02**: decimal floating point numbers with one or two decimal exponents, and with or without "+",**1.23456E-12**: decimal floating point numbers with negative decimal exponents,**-1.234567E-13**: negative decimal floating point numbers with negative decimal exponent.

- check whether the decimal is negative (the string starts with "-"),
- convert the unsigned decimal mantissa to a binary number format,
- get the decimal exponent (number of decimal digits before the decimal dot plus the number Anzahl der Ziffern vor dem Dezimalkomma plus the negative or positive number following "", if any) and to multiply (positive exponent) or divide (negative exponent) the binary mantissa, including any changes to the binary exponent, if necessary,
- normalize the binary mantissa (highest mantissa bit = 1), and to
- invert the mantissa's sign bit and it's content if the input number is negative.

The software is written for binary mantissas of up to 40 bit length and uses an 8-bit binary exponent. That corresponds to a 48-bit binary float. Those who need less accuracy can remove the last or the two last bytes and save some execution time with that.

- "0": Character smaller than ASCII-zero,
- "9": Character larger than ASCII-nine,
- "E": Exponent larger than +/-39,
- "b": Binary exponent smaller than -128,
- "B": Binary exponent larger than 127.

Starting with the decimal 1,000,000,000,000 (0xE8D4A51000) this decimal is repeatedly divided by two. The integer is then compared with this divided number. If the integer is equal or larger than the divided decimal, the decimal is subtracted and a one is shifted into the result registers. If not, a zero is shifted into the result registers. If, after shifting, a zero is shifted out the division by two and the comparison is repeated. If a one is shifted out, the conversion of 40 bits is complete.

Now the decimal exponent is checked if it is larger than 40 or smaller than -40. If so, the error loop is executed.

If the decimal exponent is positive (bit 7 of

If the decimal exponent is negative, the mantissa is divided by 10, as often as the decimal exponent says. Division by 10 can be done in two ways, both are included in the source code following the label

The second version of dividing by 10 needs a little bit more source lines, but performs faster. The accelerated mode copies the previous mantissa, adds five to the mantissa, then the copy is repeatedly divided by two. The first, the second and the third divided copies are subtracted from the uprounded mantissa, the fourth and fifth are not subtracted. Then the following two divided copies are subtracted and the next following two are not subtracted. The dividing and subtracting ends when the divided copy is empty.

To switch to the accelerated div10 version is useful if many divisions by 10 have to be performed (in case of a negative decimal exponent). In case of 1E-30 the classical div10 method needs 24.55 ms, the accelerated method only 14 ms, and so is nearly double as fast.

The accelerated method is further described here for divisions by N and here for 10 only.

Those who need normalization with an extra mantissa bit, shift the mantissa left until bit 39 is one and then clear bit 39. This shifts the most significant bit out and adds one bit to the mantissa.

Finally: if the flag

After all these operations the result binary mantissa is in

As can be seen, the re-converted numbers differ in the fifth or sixth decimal digit. So is 0.12345651 incorrect for the second "5" in the seventh digit, which would round up falsely to the sixth digit. This is as expected because with 40 bits LOG2(40) is little more than 5. If you need it more accurate, use a 56 or 64 bit mantissa instead. The method is the same, the extensions are pretty small.

Have much fun with playing with this software.

To the page top

©2021 by http://www.avr-asm-tutorial.net