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.

Float variable cumulative operation rounding problem(Cumulative operation gives same result in big float variables)

Hello Community Members

I am calculating energy in my application. It is cumulative operation with Active Power and time. But after energy register reaches some big value, anymore cumulative operation does not occur that's why energy register remains always same.

For example in below line, all variables are float variable. I know adding big value float variable and small value variable gives same value with first value. But in my energy register I should be capable to do cumulative operation.

Energy.Active.Feeder1_Import += P.Feeder1_TOT * ENERGY_CONSTANT;


Lets make Energy.Active.Feeder1_Import = 100000.0kWh and instantenous energy value(P.Feeder1_TOT * ENERGY_CONSTANT) is 0.008kWh. When I add

100000.0kWh + 0.008kWh = 100000.0kWh

How can I overcome this problem in such applications ?

Regards

Serkan

  • Look up what "single precision float" actually means. It is 24 bit mantissa, 8 bit exponent.

    100000.0kWh + 0.008kWh = 100000.0kWh

    100000 equals 1*10^5, 0.008 equals 8*10^-3. This means 8 orders of magnitude. The 24-bit mantissa can constitute a "dynamic range" of 2^24, i.e. 16.77*10^6, approximately 6 orders of magnitude. The exponent defines in some way the "minimal increment", giving a float number a "relative precision". As you can see, your small increments cannot have any effect when added to those large float numbers.

    How can I overcome this problem in such applications ?

    That's pretty obvious, isn't it ?

  • You on FIRE this day, Frank!
  • Hello f.m

    Thanks for your reply.

    You are absolutely right !

    Regards
    Serkan
  • Well, the original question was rather simple. Only, no simple tag came to my mind to point out that a short search/investigation could have brought up that fact ...

    Lastly, I refrain from blaming students/beginners only - I guess we see the results of "outcome based education" here.

  • f. m. said:
    I guess we see the results of "outcome based education"...

    But they "pass" the tests - do they not?

    Your explanation was SO good - well walked the fine line between teaching & logical reasoning - (never) found w/in "outcome based."

    Note dual (should be 100) "Likes" (poster & this reporter) first to respond.    Great job - thank you...

  • But they "pass" the tests - do they not?

    You are correct here. But answering would digress too far into politics, conspiracy and philosophy, and surely deleted as off-topic. So I refrain ...  

  • f. m. said:
    So I refrain ...  

    Ratz - there (may) be parallels between "there" (schools) and "here" (forum) - and who better than f.m. to "walk that tight-rope?"

    (The (tag) "politics, conspiracy & philosophy" has yet to appear w/in vendor's (uber-boring, uninspired) "TM4Cxxx only" listings...calls your name!)

  • Serkan, if you are going to use floating point in embedded work you should consider taking a course in finite precision arithmetic. This effect is just the tip of the iceberg. Far worse lies beneath the surface.

    Robert