Skip to main content
Ecohe
Associate II
October 6, 2021
Question

Division by zero

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

This topic has been closed for replies.

11 replies

Peter BENSCH
ST Technical Moderator
October 6, 2021

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
EcoheAuthor
Associate II
October 6, 2021

can give an example

Peter BENSCH
ST Technical Moderator
October 6, 2021

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
EcoheAuthor
Associate II
October 6, 2021

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.

Andrew Neil
Super User
October 6, 2021

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?

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Ecohe
EcoheAuthor
Associate II
October 6, 2021

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'

Andrew Neil
Super User
October 6, 2021

"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

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
TDK
October 6, 2021

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

Is your hope that your code will work in spite of ignoring all divisions by zero?

Surely dividing by zero indicates a flaw somewhere in code logic or otherwise. How is your code so riddled with these errors that you can't isolate where it happens?

"If you feel a post has answered your question, please click ""Accept as Solution""."
waclawek.jan
Super User
October 6, 2021

AFAIK in Cortex-M4 the division by 0 trap is not enabled by default, so there's some code which explicitly enables it (in some sort of SystemInit() or similar obscure piece of code which comes automagically with the environment).

JW