This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Floating point multiplication

How can i convert floating point to fixed point? When i used hex format, value is bigger than float value. e.g., 0.003639754701873030's hex value 0077h (from scopeFir). If i use 0077h in hardware multiplier, result doesn't same with float multiplication result. How does floating multiplication process happen in microcontroller?

  • floating as well as fixed point numbers are just opaque data to the microprocessor. Their only meaning is inside the library functions. The processor only know about integer values.

    Of course the floating/fixed point numbers have an integer representation but this is no value the processor can work with.

    It's like using logarithmic values. If you try to multiply two logarithmic values (even if they are values after all), the result isn't the same as you'd expect when multiplying two natural numbers.

    So the MSPs hardware multiplier is for integer values only. If you multiply the representations of two floating point numbers, the result would be what you'd get when multilying two integer values of the same numerical representation. And this isn't what you'd get from multiplying two floating point values.

    Only the math library knows how to convert float into int and back and how to do handle mathematical operation on them. And it is not accelerated by any processor hardware support.

    If you want to do fixed or floating point integer, you can multiply the mantisse parts (the number values) but also need to add the exponent parts. So if you multiply 3.1 and 2.25, you'll multiply 31 and 225 (=6975) and add the exponents (1+2 decimal digits), ending up with 6.975 as result. The hardware multiplier doen's know of or care for the exponent part(or, worst case, includes it into the integer multiplication). It's up to you to handle this. IEEE 64 or 80 bit floating point values are even more complex.

    After all, using float or double on a microcontroller wihtout floatign point coprocessor is sloooow and also consumes much more memory. Avoid at all costs.

  • Can i use hardware multiplier for floating numbers? For example, i want to multiply 0x0567 and 0.0568. Is it possible to trick hardware multiplier for floating point numbers and if it is, how can i do that?

  • Yes, you can trick the multiplier to do some of the work. But you, someone, or something, must do the rest of it.

    For example, here is one way to accomplish that. (a) You know that the multiplier cannot handle 0.0568, but it can handle 568. (b) You know that 568 is ten thousand times bigger than 0.0568. (c) Thus you make the multiplier to do 0x0567 * 568 and scale down the result by ten thousand. (d) That is, the multiplier will give you 785544, but you know the answer is 78.5544.

    The c-compiler and the floating-point library is doing a similar trick. Except that they use scale factors of 2, 4, 8, 16, 32, etc. instead of 10, 100, 1000, 10000, etc.

  • Problem is hardware multiplier's result in hex format. I know my result is 78 in decimal but how can i know my scale value in hex?

  • No, hardware multiplier does not result in hex format. It uses two different voltage levels on multiple (16 or 32)  signals. Programmers are trained to recognize them as binary numbers -- one voltage lever represents a binary 1 and the other a binary 0. It is easy to convert binary numbers to hexadecimal numbers. (I myself use octal numbers instead.) Format is all in you head.  Binary 01001110 is the same thing as hex 4E or octal 116 or decimal 78.

  • ferhat said:
    Problem is hardware multiplier's result in hex format.

    No, it results in a value. What this value actually means is a matter of interpretation. The usual interpretation is that it is an integer. (and that's what the multiplier has been optimized to). And how this integer is interpreted is up to the program(mer): binary, octal, decimal or hex value or whatever.

    You can feed the multiplier with logarithmic values, so it calculates exponents instead of doing multiplications. The multiplier won't care or even see the difference.

    Float numbers usually have two parts: mantissa and exponent. When multiplying two floats, you multiply the mantissa and add the exponents.

    The exponent tells, how many digits the mantissa has been shofte left or right of the decimal point.

    So if you multiply 4.3 and 5.67, you catually multiply 34 and 567 and shift the result left by 1+2=3 digits: 24.381. It's easier to calculate, however, when you use numbers based on binary base. Unfortunately, simple numbers like 4.3 won't translate easily to binary.

    So 4.3 is 100.0100110011001100110011001100110011001100110011001101... and 5.67 is 101.1010101110000101000111101011100001010001111010111001.

    Reduced to, say, 16 bits (with 8 bits exponent), those two become 100.0100110011001 = 4.299926... and 101.1010101110000 = 5.669921.

    To multiply them, you'll just multiply the bits -> 1100001100001010101111111110000 and shift the result right by (13+13+1) digits: 11000.01100001010.
    Converted back to decimal, this is 24.37988..., as close as you can get to 24.381 with binary floating point.

    For the multiplication itself, you can use the hardware multiplier. And the addition of the exponent is simple. For the translation from integer to float and back, well, you need quite some code. The integer part is simple (11000=24), but the fraction part is actually the decimal fraction multiplied with the maximum value of the remaining bits, or, the binary value of the n bits of the fraction part divided by 2^n gives the decimal fraction. (01100001010/ 2048=0.379882812)

    Doing float math on a microcontroller without floating point coprocessor, isn't that much slower than integer math. But as soon as you mix up integers with floats (to write your results into a char/word register or print the result with printf etc.) things get sloooooooow. It's also much, much faster to do float comparisons (</>/==) with a value that has been converted to float at compile-time than comparing with an integer (e.g. a register or variable value). Comparisons are, however, still more complicated with floats than with integers, as the processor doesn't know of the exponents role in a float and so some extra code is required.

  • how to multiply 2 different sign floating point? means 1 number is .00345678 & other is -.0367998? .whether to subtract exponent & multiply mantissa??

  • Rajat,

    I am not sure if you mean to use the hardware multiplier (which is what the original thread was about). However, in the basic sense, the answer to your question is that the sign of the result is the XOR of the sign bits from the two multiplicands.

    So, remove and store the sign bits from multiplicands, do normal multiplication, change the sign bit of result to the XOR of the two stored sign bits.

**Attention** This is a public forum