<p>Turns out my code was correct.  My problem was that the test data I had entered in my Interpretation field just happened to calculate where the denominator was "0" which was causing a mathematical error.</p>

<p>What I ended up doing was checking to make sure the denominators sum were greater than 0. If they were then run the calculation code.</p>

<pre><code>&lt;!-- Prevent Division by zero error --&gt;
{% if (entriesLogInterpretationTruePositive + entriesLogInterpretationFalseNegative &gt; 0) and (entriesLogInterpretationTrueNegative + entriesLogInterpretationFalsePositive &gt; 0) %}

    &lt;!-- Get interpretation score. --&gt;
    {% set interpretationSensitivity = entriesLogInterpretationTruePositive / (entriesLogInterpretationTruePositive + entriesLogInterpretationFalseNegative) %}
    {% set interpretationSpecificity = entriesLogInterpretationTrueNegative / (entriesLogInterpretationTrueNegative + entriesLogInterpretationFalsePositive) %}
    {% set interpretationAccuracy = (entriesLogInterpretationTruePositiveAorta + entriesLogInterpretationTrueNegative) / (entriesLogInterpretationTruePositive + entriesLogInterpretationTrueNegative + entriesLogInterpretationFalsePositive + entriesLogInterpretationFalseNegative) %}

{% endif %}
</code></pre>

<p><strong>Output section:</strong></p>

<pre><code>    &lt;!-- Table Rows --&gt;
    &lt;!-- Prevent Division by zero error --&gt;
    {% if (entriesLogInterpretationTruePositive + entriesLogInterpretationFalseNegative &gt; 0) and (entriesLogInterpretationTrueNegative + entriesLogInterpretationFalsePositive &gt; 0) %}
        &lt;td class="c-center"&gt;{{ interpretationSensitivity|round(3, 'common') }}&lt;/td&gt;
        &lt;td class="c-center"&gt;{{ interpretationSpecificity|round(3, 'common') }}&lt;/td&gt;
        &lt;td class="c-center"&gt;{{ interpretationAccuracy|round(3, 'common') }}&lt;/td&gt;
    {% else %}
        &lt;td class="c-center"&gt;N/A&lt;/td&gt;
        &lt;td class="c-center"&gt;N/A&lt;/td&gt;
        &lt;td class="c-center"&gt;N/A&lt;/td&gt;
    {% endif %}

{% endfor %}
</code></pre>

CC BY-SA 3.0
2016-08-27T00:57:21.860Z
2016-08-27T16:26:08.663Z
2016-08-27T16:26:08.663Z
Lori
<p>Calling <code>total()</code> is already going to return a 0 if no results are found.</p>

<p>I believe you've just got a math problem.  In your denominators, the sums of either:</p>

<pre><code>entriesLogInterpretationTruePositive + entriesLogInterpretationFalseNegative
</code></pre>

<p>or</p>

<pre><code>entriesLogInterpretationTrueNegative + entriesLogInterpretationFalsePositive
</code></pre>

<p>or </p>

<pre><code>entriesLogInterpretationTruePositive + entriesLogInterpretationTrueNegative + entriesLogInterpretationFalsePositive + entriesLogInterpretationFalseNegative
</code></pre>

<p>are returning 0 calling the division to fail (since you can't divide by 0).</p>

<p>I'm not sure what the actual number you're trying to calculate here it, but you can guard against dividing by 0 with something like:</p>

<pre><code>{% set denominator = entriesLogInterpretationTruePositive + entriesLogInterpretationFalseNegative %}

{% if denominator &gt; 0 %}
    {% set interpretationSensitivity = entriesLogInterpretationTruePositive / denominator %}
{% endif %}
</code></pre>

CC BY-SA 3.0
2016-08-26T22:03:00.913Z
Thanks @bradbell. Let's say I have 3 entries. To calculate Sensitivity, I need to count all entries that have a drop down value of "true positive" and "false negative". If any of the entries don't contain either value then default value is 0 for that item. Lets say out of 3 entries, 2 have a value of "true positive", 1 entry has "true negative" (which is not used in the Sensitivity calculation so it's ignored). My code breaks because I don't have a result for "false negative" which is needed. If it worked it should calculate, 2 "true positive" / (2 "true positive" + 0 "false negative") = 1.
Lori
CC BY-SA 3.0
2016-08-26T22:15:45.643Z
Yeah, I get that.  But the error indicates the math isn't working out like you're expecting.  Enable devMode and when it blows up, it'll show a stack trace where you can figure out the line number of your template it's choking on. (Can add a pic of it to your original question if you want). Also, try outputting the values into your template to make sure they're what you expect. i.e. entriesLogInterpretationTruePositive = {{ entriesLogInterpretationTruePositive }}, etc.
Brad Bell
CC BY-SA 3.0
2016-08-26T23:06:12.757Z
Brad @bradbell, looks like it's if none of the entries have results.  So in my example, if Sensitivity = 0 / (0 + 0) then I get the Division by zero error.
Lori
CC BY-SA 3.0
2016-08-26T23:45:35.140Z
It just hit me what you said Brad.  I have the template working just fine and that its actually impossible in the math realm to divide by zero. I have test data in and didn't account for that when I entered in test data. So sorry about that. Should I delete my post since it isn't really a problem?
Lori
CC BY-SA 3.0
2016-08-27T00:08:09.290Z
Ahhh... no worries.  I'd probably just add your solution as an answer. Entirely possible someone else might have a similar situation.  Glad you're all sorted!
Brad Bell
CC BY-SA 3.0
2016-08-26T21:30:12.083Z
2016-08-26T21:30:12.083Z
Brad Bell
<p>I have entries which contain a drop down list of items that I need to run calculations on. In my template, I am filtering the entries by the drop down field so I can get the sum total of the entries that meet the criteria and then run calculations based on that.  I am running into a "<strong>Division by zero</strong>" error when I try to output some calculations. I believe I'm getting that error because there are no results for some of my entries (meaning an item in the drop down list was not selected in any of the entries). I believe I need to add <code>|default(0)</code> so that if the entry search turns up with no results then the number 0 is used in the calculation for that result.</p>

<p>Please refer below for the simplified version of my code. I'll pull one area out as an example. If there are no results for <code>entriesLogInterpretationTrueNegative</code> then I get the "<strong>Division by zero</strong>" error. In this example, there are no entries in the database where "True Negative" is selected in the drop down menu.  I believe I need to say that if there are no results then use "0".</p>

<pre><code>&lt;table id="" class="table table-striped table-bordered table-hover"&gt;
    &lt;thead&gt;
        &lt;tr&gt;
            &lt;th class="c-center c-font-sbold"&gt;Sensitivity&lt;/th&gt;
            &lt;th class="c-center c-font-sbold"&gt;Specificity&lt;/th&gt;
            &lt;th class="c-center c-font-sbold"&gt;Accuracy&lt;/th&gt;
        &lt;/tr&gt;
    &lt;/thead&gt;

    &lt;tbody&gt;

        &lt;!-- Loop through all study type groups. (Global Variable set in Base Layout). --&gt;    
        {% for keywordStudyType in GVstudyTypeGroup %}

            &lt;!-- Get count total for result for logged in sonographer. --&gt;
            {% set entriesLogInterpretationTruePositive = craft.entries.section('logsQA').logGeneral_statusWorkflow('reviewed').search('logReviewer_interpretation:truePositive -logReviewer_interpretation:0 logGeneral_studyType:'~keywordStudyType).relatedTo(
                { targetElement: currentUser, field: 'logGeneral_sonographerName' }
            ).total() %}

            {% set entriesLogInterpretationTrueNegative = craft.entries.section('logsQA').logGeneral_statusWorkflow('reviewed').search('logReviewer_interpretation:trueNegative -logReviewer_interpretation:0 logGeneral_studyType:'~keywordStudyType).relatedTo(
                { targetElement: currentUser, field: 'logGeneral_sonographerName' }
            ).total() %}

            {% set entriesLogInterpretationFalsePositive = craft.entries.section('logsQA').logGeneral_statusWorkflow('reviewed').search('logReviewer_interpretation:falsePositive -logReviewer_interpretation:0 logGeneral_studyType:'~keywordStudyType).relatedTo(
                { targetElement: currentUser, field: 'logGeneral_sonographerName' }
            ).total() %}

            {% set entriesLogInterpretationFalseNegative = craft.entries.section('logsQA').logGeneral_statusWorkflow('reviewed').search('logReviewer_interpretation:falseNegative -logReviewer_interpretation:0 logGeneral_studyType:'~keywordStudyType).relatedTo(
                { targetElement: currentUser, field: 'logGeneral_sonographerName' }
            ).total() %}

            &lt;!-- Get interpretation score. --&gt;
            {% set interpretationSensitivity = entriesLogInterpretationTruePositive / (entriesLogInterpretationTruePositive + entriesLogInterpretationFalseNegative) %}
            {% set interpretationSpecificity = entriesLogInterpretationTrueNegative / (entriesLogInterpretationTrueNegative + entriesLogInterpretationFalsePositive) %}
            {% set interpretationAccuracy = (entriesLogInterpretationTruePositiveAorta + entriesLogInterpretationTrueNegative) / (entriesLogInterpretationTruePositive + entriesLogInterpretationTrueNegative + entriesLogInterpretationFalsePositive + entriesLogInterpretationFalseNegative) %}


            &lt;!-- Table Rows --&gt;
            &lt;tr&gt;
                &lt;td class="c-center"&gt;{{ interpretationSensitivity|round(3, 'common') }}&lt;/td&gt;
                &lt;td class="c-center"&gt;{{ interpretationSpecificity|round(3, 'common') }}&lt;/td&gt;
                &lt;td class="c-center"&gt;{{ interpretationAccuracy|round(3, 'common') }}&lt;/td&gt;
            &lt;/tr&gt;

        {% endfor %}

    &lt;/tbody&gt;
&lt;/table&gt;
</code></pre>

<templating><filter><logic>
Division by zero error as a result of calculation
# Division by zero error as a result of calculation

(Asked by: username_0 on 2016-08-26)

I have entries which contain a drop down list of items that I need to run calculations on. In my template, I am filtering the entries by the drop down field so I can get the sum total of the entries that meet the criteria and then run calculations based on that.  I am running into a "Division by zero" error when I try to output some calculations. I believe I'm getting that error because there are no results for some of my entries (meaning an item in the drop down list was not selected in any of the entries). I believe I need to add |default(0) so that if the entry search turns up with no results then the number 0 is used in the calculation for that result.
Please refer below for the simplified version of my code. I'll pull one area out as an example. If there are no results for entriesLogInterpretationTrueNegative then I get the "Division by zero" error. In this example, there are no entries in the database where "True Negative" is selected in the drop down menu.  I believe I need to say that if there are no results then use "0".

    
        
            Sensitivity
            Specificity
            Accuracy
        
    

    

            
        {% for keywordStudyType in GVstudyTypeGroup %}

            
            {% set entriesLogInterpretationTruePositive = craft.entries.section('logsQA').logGeneral_statusWorkflow('reviewed').search('logReviewer_interpretation:truePositive -logReviewer_interpretation:0 logGeneral_studyType:'~keywordStudyType).relatedTo(
                { targetElement: currentUser, field: 'logGeneral_sonographerName' }
            ).total() %}

            {% set entriesLogInterpretationTrueNegative = craft.entries.section('logsQA').logGeneral_statusWorkflow('reviewed').search('logReviewer_interpretation:trueNegative -logReviewer_interpretation:0 logGeneral_studyType:'~keywordStudyType).relatedTo(
                { targetElement: currentUser, field: 'logGeneral_sonographerName' }
            ).total() %}

            {% set entriesLogInterpretationFalsePositive = craft.entries.section('logsQA').logGeneral_statusWorkflow('reviewed').search('logReviewer_interpretation:falsePositive -logReviewer_interpretation:0 logGeneral_studyType:'~keywordStudyType).relatedTo(
                { targetElement: currentUser, field: 'logGeneral_sonographerName' }
            ).total() %}

            {% set entriesLogInterpretationFalseNegative = craft.entries.section('logsQA').logGeneral_statusWorkflow('reviewed').search('logReviewer_interpretation:falseNegative -logReviewer_interpretation:0 logGeneral_studyType:'~keywordStudyType).relatedTo(
                { targetElement: currentUser, field: 'logGeneral_sonographerName' }
            ).total() %}

            
            {% set interpretationSensitivity = entriesLogInterpretationTruePositive / (entriesLogInterpretationTruePositive + entriesLogInterpretationFalseNegative) %}
            {% set interpretationSpecificity = entriesLogInterpretationTrueNegative / (entriesLogInterpretationTrueNegative + entriesLogInterpretationFalsePositive) %}
            {% set interpretationAccuracy = (entriesLogInterpretationTruePositiveAorta + entriesLogInterpretationTrueNegative) / (entriesLogInterpretationTruePositive + entriesLogInterpretationTrueNegative + entriesLogInterpretationFalsePositive + entriesLogInterpretationFalseNegative) %}


            
            
                {{ interpretationSensitivity|round(3, 'common') }}
                {{ interpretationSpecificity|round(3, 'common') }}
                {{ interpretationAccuracy|round(3, 'common') }}
            

        {% endfor %}

**Tags:** templating, filter, logic


---

## 2 answers

### Answer by username_0 on 2016-08-27. Score: 1 | Accepted answer

Turns out my code was correct.  My problem was that the test data I had entered in my Interpretation field just happened to calculate where the denominator was "0" which was causing a mathematical error.
What I ended up doing was checking to make sure the denominators sum were greater than 0. If they were then run the calculation code.

{% if (entriesLogInterpretationTruePositive + entriesLogInterpretationFalseNegative > 0) and (entriesLogInterpretationTrueNegative + entriesLogInterpretationFalsePositive > 0) %}

    
    {% set interpretationSensitivity = entriesLogInterpretationTruePositive / (entriesLogInterpretationTruePositive + entriesLogInterpretationFalseNegative) %}
    {% set interpretationSpecificity = entriesLogInterpretationTrueNegative / (entriesLogInterpretationTrueNegative + entriesLogInterpretationFalsePositive) %}
    {% set interpretationAccuracy = (entriesLogInterpretationTruePositiveAorta + entriesLogInterpretationTrueNegative) / (entriesLogInterpretationTruePositive + entriesLogInterpretationTrueNegative + entriesLogInterpretationFalsePositive + entriesLogInterpretationFalseNegative) %}

{% endif %}

Output section:
    
    
    {% if (entriesLogInterpretationTruePositive + entriesLogInterpretationFalseNegative > 0) and (entriesLogInterpretationTrueNegative + entriesLogInterpretationFalsePositive > 0) %}
        {{ interpretationSensitivity|round(3, 'common') }}
        {{ interpretationSpecificity|round(3, 'common') }}
        {{ interpretationAccuracy|round(3, 'common') }}
    {% else %}
        N/A
        N/A
        N/A
    {% endif %}

{% endfor %}


---

### Answer by username_1 on 2016-08-26. Score: 1

Calling total() is already going to return a 0 if no results are found.
I believe you've just got a math problem.  In your denominators, the sums of either:
entriesLogInterpretationTruePositive + entriesLogInterpretationFalseNegative

or
entriesLogInterpretationTrueNegative + entriesLogInterpretationFalsePositive

or 
entriesLogInterpretationTruePositive + entriesLogInterpretationTrueNegative + entriesLogInterpretationFalsePositive + entriesLogInterpretationFalseNegative

are returning 0 calling the division to fail (since you can't divide by 0).
I'm not sure what the actual number you're trying to calculate here it, but you can guard against dividing by 0 with something like:
{% set denominator = entriesLogInterpretationTruePositive + entriesLogInterpretationFalseNegative %}

{% if denominator > 0 %}
    {% set interpretationSensitivity = entriesLogInterpretationTruePositive / denominator %}
{% endif %}

> **Comments:**
> **username_0**: Thanks @bradbell. Let's say I have 3 entries. To calculate Sensitivity, I need to count all entries that have a drop down value of "true positive" and "false negative". If any of the entries don't contain either value then default value is 0 for that item. Lets say out of 3 entries, 2 have a value of "true positive", 1 entry has "true negative" (which is not used in the Sensitivity calculation so it's ignored). My code breaks because I don't have a result for "false negative" which is needed. If it worked it should calculate, 2 "true positive" / (2 "true positive" + 0 "false negative") = 1.  
> **username_1**: Yeah, I get that.  But the error indicates the math isn't working out like you're expecting.  Enable devMode and when it blows up, it'll show a stack trace where you can figure out the line number of your template it's choking on. (Can add a pic of it to your original question if you want). Also, try outputting the values into your template to make sure they're what you expect. i.e. entriesLogInterpretationTruePositive = {{ entriesLogInterpretationTruePositive }}, etc.  
> **username_0**: Brad @bradbell, looks like it's if none of the entries have results.  So in my example, if Sensitivity = 0 / (0 + 0) then I get the Division by zero error.  
> **username_0**: It just hit me what you said Brad.  I have the template working just fine and that its actually impossible in the math realm to divide by zero. I have test data in and didn't account for that when I entered in test data. So sorry about that. Should I delete my post since it isn't really a problem?  
> **username_1**: Ahhh... no worries.  I'd probably just add your solution as an answer. Entirely possible someone else might have a similar situation.  Glad you're all sorted!  

---

