# Dealing with \`divide 0\` or \`Inf\` in GNU Radio C++ code

**URL:** <https://www.ruby-forum.com/t/dealing-with-divide-0-or-inf-in-gnu-radio-c-code/243067>\
**Category:** GNU Radio\
**Created:** [August 19, 2015, 12:51pm UTC](https://www.ruby-forum.com/t/dealing-with-divide-0-or-inf-in-gnu-radio-c-code/243067 "2015-08-19T12:51:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafa\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@Rafa\_F](https://www.ruby-forum.com/u/Rafa_F)\
**Post date:** [August 19, 2015, 12:51pm UTC](https://www.ruby-forum.com/t/dealing-with-divide-0-or-inf-in-gnu-radio-c-code/243067/1 "2015-08-19T12:51:38Z")

</div>

Let’s think about the following auto/cross correlator:  
[http://i.imgur.com/Fy5hdVj.png](http://i.imgur.com/Fy5hdVj.png)  
I’ve placed `divide` because I need to normalize the correlation result.

If we place `null source`, or `constant source with value of zero` right  
before the `pad source`, the simplified form of the correlator becomes 0  
divided by 0.

I’ve placed `time sink` right after the `pad sink` then, I can see the  
line  
hit the infinity. With a simplified flow graph:  
[http://i.imgur.com/Msx9EQj.png](http://i.imgur.com/Msx9EQj.png)

Back to the my correlator, how can I handle such `infinity` value coming  
into the next block?

My guess is that if a source block is a type of float, then `infinity`  
might have a maximum value of float. So, I think I can reject `infinity`  
value with `if (in[i] > threshold)`, where `threshold` is big enough,  
for  
correlator it is fine to have a value just larger than one.

Do you think what I guess is right? Or can some boost numeric types, or  
PMT  
help somehow to deal with it?

In addition, not important.  
In pages [std::numeric\_limits\<T\>::infinity - cppreference.com](http://en.cppreference.com/w/cpp/types/numeric_limits/infinity)  
and  
[INFINITY - cppreference.com](http://en.cppreference.com/w/cpp/numeric/math/INFINITY),  
non floating point numeric types can’t handle infinity.  
Is it also applied to GNU Radio?

Regards,  
Jeon.

---

<div class="post-metadata">

**Author:** ![Jeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e8c25b/32.png) [@Jeon](https://www.ruby-forum.com/u/Jeon)\
**Post date:** [August 19, 2015, 1:26pm UTC](https://www.ruby-forum.com/t/dealing-with-divide-0-or-inf-in-gnu-radio-c-code/243067/2 "2015-08-19T13:26:41Z")

</div>

Jeon,

You can add an appropriate constant before the divide and compensate for  
it afterward. The exact math will depend on the values coming out of the  
correlators.

Jeff

---

<div class="post-metadata">

**Author:** ![Jeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e8c25b/32.png) [@Jeon](https://www.ruby-forum.com/u/Jeon)\
**Post date:** [August 19, 2015, 1:31pm UTC](https://www.ruby-forum.com/t/dealing-with-divide-0-or-inf-in-gnu-radio-c-code/243067/3 "2015-08-19T13:31:22Z")

</div>

(That should be “moving averages”, not “correlators”). Also, subtracting  
instead of dividing might do what you want.

Jeff

---

<div class="post-metadata">

**Author:** ![Jeon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e8c25b/32.png) [@Jeon](https://www.ruby-forum.com/u/Jeon)\
**Post date:** [August 20, 2015, 9:32am UTC](https://www.ruby-forum.com/t/dealing-with-divide-0-or-inf-in-gnu-radio-c-code/243067/4 "2015-08-20T09:32:28Z")

</div>

Hi Jeff,

you could use a strcture like this  
MA\_divisor =\> Divide: % =\> multiply =\> pad sink.  
|-\> threshold detector -^

i.e. take the divisor path in parallel into threshold detector and use  
the output (in {0,1}) in a multiplier for masking the dividing by zero  
cases.

By adjusting the threshold to 0+eps (a small number of your choice),  
this also solves the problem of huge output if the divisor is _almost_  
0.

Best regards

Stephan Ludwig

Robert Bosch GmbH  
Corporate Sector Research & Advance Engineering, Communication  
Technology (CR/AEH4)  
Renningen  
70465 Stuttgart  
GERMANY

> **[Want to know how to live #LikeABosch?](https://www.bosch.com/)**
>
> Moving stories and inspiring interviews. Experience the meaning of "invented for life" by Bosch completely new. Visit our international website.

Tel. +49(711)811-8809  
Fax +49(711)811-1052  
Mobile +49(172)5630639  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Registered Office: Stuttgart, Registration Court: Amtsgericht Stuttgart,  
HRB 14000;  
Chairman of the Supervisory Board: Franz Fehrenbach; Managing Directors:  
Dr. Volkmar Denner,  
Dr. Stefan Asenkerschbaumer, Dr. Rolf Bulander, Dr. Stefan Hartung, Dr.  
Markus Heyn, Dr. Dirk Hoheisel,  
Christoph Kbel, Uwe Raschke, Dr. Werner Struth, Peter Tyroller

-----Ursprngliche Nachricht-----  
Von: [discuss-gnuradio-bounces+stephan.ludwig2=removed\_email\_address@domain.invalid](mailto:discuss-gnuradio-bounces+stephan.ludwig2=removed_email_address@domain.invalid)  
[[mailto:discuss-gnuradio-bounces+stephan.ludwig2=removed\_email\_address@domain.invalid](mailto:discuss-gnuradio-bounces+stephan.ludwig2=removed_email_address@domain.invalid)]  
Im Auftrag von Jeff L.  
Gesendet: Mittwoch, 19. August 2015 13:31  
An: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Betreff: Re: [Discuss-gnuradio] Dealing with `divide 0` or `Inf` in GNU  
Radio C++ code.

(That should be “moving averages”, not “correlators”). Also, subtracting  
instead of dividing might do what you want.

Jeff

On 08/19/2015 07:25 AM, Jeff L. wrote:

> > [http://i.imgur.com/Fy5hdVj.png](http://i.imgur.com/Fy5hdVj.png)  
> > Back to the my correlator, how can I handle such `infinity` value  
> > In addition, not important.
> > 
> > * * *
> > 
> > Discuss-gnuradio mailing list  
> > [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> > [Discuss-gnuradio Info Page](https://lists.gnu.org/mailman/listinfo/discuss-gnuradio)

* * *

Discuss-gnuradio mailing list  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> **[Discuss-gnuradio Info Page](https://lists.gnu.org/mailman/listinfo/discuss-gnuradio)**
