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".
<table id="" class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th class="c-center c-font-sbold">Sensitivity</th>
<th class="c-center c-font-sbold">Specificity</th>
<th class="c-center c-font-sbold">Accuracy</th>
</tr>
</thead>
<tbody>
<!-- Loop through all study type groups. (Global Variable set in Base Layout). -->
{% for keywordStudyType in GVstudyTypeGroup %}
<!-- Get count total for result for logged in sonographer. -->
{% 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() %}
<!-- Get interpretation score. -->
{% set interpretationSensitivity = entriesLogInterpretationTruePositive / (entriesLogInterpretationTruePositive + entriesLogInterpretationFalseNegative) %}
{% set interpretationSpecificity = entriesLogInterpretationTrueNegative / (entriesLogInterpretationTrueNegative + entriesLogInterpretationFalsePositive) %}
{% set interpretationAccuracy = (entriesLogInterpretationTruePositiveAorta + entriesLogInterpretationTrueNegative) / (entriesLogInterpretationTruePositive + entriesLogInterpretationTrueNegative + entriesLogInterpretationFalsePositive + entriesLogInterpretationFalseNegative) %}
<!-- Table Rows -->
<tr>
<td class="c-center">{{ interpretationSensitivity|round(3, 'common') }}</td>
<td class="c-center">{{ interpretationSpecificity|round(3, 'common') }}</td>
<td class="c-center">{{ interpretationAccuracy|round(3, 'common') }}</td>
</tr>
{% endfor %}
</tbody>
</table>