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.

MSP430F249: floating point operations

Part Number: MSP430F249
Other Parts Discussed in Thread: MSP430FR5969

Hi i trying to execute a simple Addition subtraction multiplication program

like

alu_add = 1.700000+1.200000;

alu_mul = 1.700000*1.200000;

the values are getting stored in the variable as alu_add = 2.9000001;alu_mul=2.03999996; when i compare the result to the actual value of 2.9 & 2.4 it fails but i am also using other microcontroller msp430fr5969 here the results stored in those variables are 2.9 and 2.4 only. both the microcontrollers are 16 bit and msp430 series based only, so what is the issue in msp430f249

  • What do you mean it fails?

    Does it not equal?

  • yes it does not equal

  • "Floating Point is like sand piles, every time you move one you lose a little sand and gain a little dirt"

    Read all these faq topics:

    C Programming FAQ

  • Expecting exact floating point representations of arbitrary base 10 numbers just sets you up for disappointment. But in this case the difference might be due to differences in the floating point libraries. The FR5969 has resources that the F249 doesn't so it uses a different version of the library and they could give different results to the same problem. (Or perhaps it is the routines used by printf since you don't say how you know the results.) They shouldn't but you shouldn't expect exact results with floating point anyway.

  • Thanks, David Schiltz

    I want to confirm that if there are any hardware modules that makes msp430fr5969 work better for floating-point operations since it is totally dependent on software libraries we can proceed with it

  • There used to be some external floating point coprocessors, but my google-foo failed to find any. However, I think your best bet is to manage your expectations and learn how to handle the realities of floating point and decimal to binary conversion. I don't know that a hardware solution will be any better.

  • I'll just add some emphasis to what David and Keith already said:

    You're seeing an artifact of floating-point, not of the MSP430. There are some values which can't be exactly represented even in double-precision floating point (just as 1/3rd can't be exactly represented in decimal). 

    Instead of "if (value == expected)" consider something like "if (value - expected < 0.001)".

  • I think you missed the point. He is reporting different results for the F249 and FR5969. Only slightly different, but different.

    Slightly different libraries are used for the two parts because of compile time differences in the library. The results should be the same but it is possible that there is a problem. It would require a deep dive into the source code to find it.

    But the differences are down in the mud of what can be represented and my point is that expecting exact results with floating point is foolish. But I do expect repeatable results and nothing flaky.

**Attention** This is a public forum