Detect long double overflows on embedded systems?

As it's not standard C++, you will have to rely on methods provided by your specific environment. The manufacturer of the embedded system should have documented how it can be done. Ask him.

There are no specific methods provided. It's a SH3 CPU and the compiler documentation only mentions the range of long doubles but not how to detect their overflows. I don't need interrupts to catch them, checking overflow after each operation is ok for me.

I just don't know how? – Hossein Mar 12 at 6:49 The fact that it's a SH3 is important information. You should add that to your question.

I could tell you that if the result of an operation on long double is too large to be represented, then NaN should be the result and NaN is the only long double value for which value == value is false. But that's just wild guessing because I don't know that specific CPU, nor the compiler you are using. – Oswald Mar 12 at 7:13.

Your state that you need "big numbers", but this does not necessarily mean that the use of long double is indicated. In most embedded applications that I know of, long double is chosen for its enhanced precision, i.e. More bits of resolution for fractional numbers, than for its increased range.

You also state your implementation offers little of the usual floating point libraries and/or functionality. Based on these statements, I would question whether your need fully functional floating-point capabilities. If your concerns are limited to "big numbers", check to see if your compiler offers a long long datatype, which is a 64-bit integer.

If you do need some floating-point capability, you might consider a fixed-point implementation. Assuming a long long, you might choose to represent numbers in a 48.16 format, which will permit numbers of ~2.8x10^14 with 16 bits to the right of decimal. (If you need an introduction to fixed-point computation, start here.) Having addressed some of the background issues, let's look at the original question.

If you wish to detect overflow in an unsigned int (which I commonly do in my embedded work), it's sufficient to compare your latest result with the previous one. For example, my application requires me to periodically inspect a 16-bit counter that is driven by an external clock. If my current observation is less than the last observation, then I can assume that the 16-bit counter overflowed, and I can take action accordingly.

If you implement your big numbers using a long long integer datatype, you can apply a similar strategy to detect overflow.

Oswald: Luckily, the value==value trick works. @Throwback1986: I'd rather use long doubles. New question: Another way I could think of to work with long doubles is to cast the long double to char12 ( sizeof(long double) is 12 on SH-3 ) then use shifting and arithmetic to put its exponent and mantissa into separate variables and investigate them.

Before I start, do you know any libraries already written for this?

You could just use some bit fields: struct Num { unsigned is_negative : 1; unsigned mantissa: 99; unsigned exponent: 42; }; - substitute correct order and magnitudes. – Tony Delroy Mar 14 at 7:47 Please don't ask follow-up questions in answers. This should be posted as a new question.

– Bill the Lizard? Mar 14 at 14:09.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions