cancel
Showing results for 
Search instead for 
Did you mean: 

Division by zero

Ecohe
Associate II

Is there a way to prevent a crash while there is a division at zero.

For example when there is a division by zero it will return zero

11 REPLIES 11
Peter BENSCH
ST Employee

Welcome, @Ecohe​, to the community!

Yes, of course. Unfortunately, this has a somewhat unpleasant side effect because it costs additional code and therefore processing time - you can check the divisor for zero before dividing

Regards

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
Ecohe
Associate II

can give an example

Peter BENSCH
ST Employee

There are tons of examples, e.g. here.

The implementation also depends on the number type (integer, floating, ...).

Regards

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
Ecohe
Associate II

sorry it's not helped . I know it will better to make sure it not zero before the division. i am looking for a way to make a trap if this occurred it will return zero.

What do you mean, "it's not helped" ?

What, exactly, did you do?

In what way(s), exactly, were the results unsatisfactory for you?

"i am looking for a way to make a trap if this occurred"

What chip are you using? Have you looked to see what exception occurs (ie, what vector is called) when a divide-by-zero happenss?

Ecohe
Associate II

Sorry for the lack of understanding

I have a big project that I am converting it from Keil and when it run there it returning automatically a zero when division by zero accured.

I can not find all the places and situations when these cases occur.

I am looking for a way to do it the same like keil do (automaticly).

when it occurs in STM32IDE(g++) it goes to HardFault_Handler

for example:

Keil:

int a=1;

a=a /0;

//a got 0

STM32IDE it (g++):

int a=1;

a=a /0;

// goes to   HardFault_Handler

Chip:

STM32F413VGTX

Thanks'

"I am looking for a way to do it the same like keil do"

So have you asked Keil how they do it?

"when it occurs in STM32IDE(g++) it goes to HardFault_Handler"

Are you sure it's a HardFault ? Isn't that a Usage Fault?

Take a look at https://www.keil.com/appnotes/files/apnt209.pdf

Also: https://wiki.segger.com/Cortex-M_Fault#Division_By_Zero

https://www.google.com/search?q=cortex-m4+divide-by-zero+fault

Probably not UsageFault unless explicitly enabled.

HardFault is a trap..

Generally the more wisen approach is to sanity check math that might realistically cause the error. Returning zero could realistically cascade the math failure, silently.

It's more probable the issue wasn't address in the original code, because the failure wasn't detected/addressed due to lack of awareness.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

"Probably not UsageFault unless explicitly enabled"

Yes - that is mentioned in the Segger article, at least.