In calculating the percent of a mathematical expression that calculates without any problems in the views, but when saving and viewing pages from the formation of views, an error:
User warning: division by zero as a function of ctools_math_expr-> trigger () (line 360 in file sites / all / modules / ctools / includes / math-expr.inc).
And the values ​​are empty. How can this be solved?
The formula is a: [nothing_1] / [nothing] * 100 Example: 7/9 * 100.

CommentFileSizeAuthor
#11 2021-01-03_5-19-19.png52.72 KBVM
#10 division-by-zero-1662420-10-D7.patch874 bytesaludescher
#4 divide_by_zero.patch812 bytesAnonymous (not verified)
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

vlooivlerke’s picture

Issue summary: View changes

I have the same problem.

vlooivlerke’s picture

Solution is simple

In the view field "no results behavior"

Set it as 1
And hide rewriting

So if the field is empty, it will divide by 1

Sandip Choudhury’s picture

I have also this same problem - the error is showing -

"User warning: division by zero in ctools_math_expr->trigger() (line 360 of D:\xampp\htdocs\accounts\sites\all\modules\contrib\ctools\includes\math-expr.inc)"

In "No result Behavior" I have checked - Count the number 0 as empty, Hide if empty, Hide rewriting if empty.
With this view I am using Math field and Views Aggregate Module.

The view is given below -

/* Display: Profit per Client */
$handler = $view->new_display('page', 'Profit per Client', 'page_7');
$handler->display->display_options['defaults']['title'] = FALSE;
$handler->display->display_options['title'] = 'Profit per Client';
$handler->display->display_options['defaults']['group_by'] = FALSE;
$handler->display->display_options['defaults']['query'] = FALSE;
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['defaults']['pager'] = FALSE;
$handler->display->display_options['pager']['type'] = 'none';
$handler->display->display_options['pager']['options']['offset'] = '0';
$handler->display->display_options['defaults']['style_plugin'] = FALSE;
$handler->display->display_options['style_plugin'] = 'views_aggregator';
$handler->display->display_options['style_options']['columns'] = array(
'field_income_company_name' => 'field_income_company_name',
'field_profit' => 'field_profit',
'field_income_amount' => 'field_income_amount',
);
$handler->display->display_options['style_options']['default'] = 'field_profit';
$handler->display->display_options['style_options']['info'] = array(
'field_income_company_name' => array(
'sortable' => 0,
'default_sort_order' => 'asc',
'align' => '',
'separator' => '',
'empty_column' => 0,
'has_aggr' => 1,
'aggr' => array(
'views_aggregator_group_and_compress' => 'views_aggregator_group_and_compress',
'views_aggregator_tally' => 'views_aggregator_tally',
),
'aggr_par' => '',
'has_aggr_column' => 0,
'aggr_column' => 'views_aggregator_sum',
'aggr_par_column' => '',
),
'field_profit' => array(
'sortable' => 1,
'default_sort_order' => 'desc',
'align' => '',
'separator' => '',
'empty_column' => 0,
'has_aggr' => 0,
'aggr' => array(
'views_aggregator_first' => 'views_aggregator_first',
),
'aggr_par' => '',
'has_aggr_column' => 1,
'aggr_column' => 'views_aggregator_sum',
'aggr_par_column' => '',
),
'field_income_amount' => array(
'sortable' => 0,
'default_sort_order' => 'asc',
'align' => '',
'separator' => '',
'empty_column' => 0,
'has_aggr' => 0,
'aggr' => array(
'views_aggregator_first' => 'views_aggregator_first',
),
'aggr_par' => '',
'has_aggr_column' => 1,
'aggr_column' => 'views_aggregator_sum',
'aggr_par_column' => '',
),
);
$handler->display->display_options['style_options']['sticky'] = TRUE;
$handler->display->display_options['style_options']['column_aggregation']['totals_per_page'] = '1';
$handler->display->display_options['style_options']['column_aggregation']['totals_row_position'] = array(
1 => 0,
2 => '2',
);
$handler->display->display_options['style_options']['column_aggregation']['precision'] = '2';
$handler->display->display_options['defaults']['style_options'] = FALSE;
$handler->display->display_options['defaults']['row_plugin'] = FALSE;
$handler->display->display_options['defaults']['row_options'] = FALSE;
$handler->display->display_options['defaults']['fields'] = FALSE;
/* Field: Content: Income - Company Name */
$handler->display->display_options['fields']['field_income_company_name']['id'] = 'field_income_company_name';
$handler->display->display_options['fields']['field_income_company_name']['table'] = 'field_data_field_income_company_name';
$handler->display->display_options['fields']['field_income_company_name']['field'] = 'field_income_company_name';
$handler->display->display_options['fields']['field_income_company_name']['type'] = 'taxonomy_term_reference_plain';
/* Field: Content: Income - Amount */
$handler->display->display_options['fields']['field_income_amount']['id'] = 'field_income_amount';
$handler->display->display_options['fields']['field_income_amount']['table'] = 'field_data_field_income_amount';
$handler->display->display_options['fields']['field_income_amount']['field'] = 'field_income_amount';
$handler->display->display_options['fields']['field_income_amount']['hide_empty'] = TRUE;
$handler->display->display_options['fields']['field_income_amount']['empty_zero'] = TRUE;
$handler->display->display_options['fields']['field_income_amount']['settings'] = array(
'thousand_separator' => '',
'decimal_separator' => '.',
'scale' => '2',
'prefix_suffix' => 1,
);
/* Field: Content: Profit */
$handler->display->display_options['fields']['field_profit']['id'] = 'field_profit';
$handler->display->display_options['fields']['field_profit']['table'] = 'field_data_field_profit';
$handler->display->display_options['fields']['field_profit']['field'] = 'field_profit';
$handler->display->display_options['fields']['field_profit']['hide_empty'] = TRUE;
$handler->display->display_options['fields']['field_profit']['empty_zero'] = TRUE;
$handler->display->display_options['fields']['field_profit']['settings'] = array(
'thousand_separator' => '',
'decimal_separator' => '.',
'scale' => '2',
'prefix_suffix' => 1,
);
/* Field: Global: Math expression */
$handler->display->display_options['fields']['expression']['id'] = 'expression';
$handler->display->display_options['fields']['expression']['table'] = 'views';
$handler->display->display_options['fields']['expression']['field'] = 'expression';
$handler->display->display_options['fields']['expression']['label'] = 'Profit Percent';
$handler->display->display_options['fields']['expression']['alter']['max_length'] = '5';
$handler->display->display_options['fields']['expression']['alter']['word_boundary'] = FALSE;
$handler->display->display_options['fields']['expression']['alter']['ellipsis'] = FALSE;
$handler->display->display_options['fields']['expression']['hide_empty'] = TRUE;
$handler->display->display_options['fields']['expression']['empty_zero'] = TRUE;
$handler->display->display_options['fields']['expression']['set_precision'] = TRUE;
$handler->display->display_options['fields']['expression']['precision'] = '2';
$handler->display->display_options['fields']['expression']['expression'] = '[field_profit] / [field_income_amount] * 100';
$handler->display->display_options['defaults']['sorts'] = FALSE;
$handler->display->display_options['defaults']['filter_groups'] = FALSE;
$handler->display->display_options['defaults']['filters'] = FALSE;
/* Filter criterion: Content: Published */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'node';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = 1;
$handler->display->display_options['filters']['status']['group'] = 1;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
/* Filter criterion: Content: Type */
$handler->display->display_options['filters']['type']['id'] = 'type';
$handler->display->display_options['filters']['type']['table'] = 'node';
$handler->display->display_options['filters']['type']['field'] = 'type';
$handler->display->display_options['filters']['type']['value'] = array(
'accounting' => 'accounting',
);
$handler->display->display_options['filters']['type']['group'] = 1;
$handler->display->display_options['path'] = 'profit-client';
$handler->display->display_options['menu']['type'] = 'normal';
$handler->display->display_options['menu']['title'] = 'Profit Client';
$handler->display->display_options['menu']['weight'] = '0';
$handler->display->display_options['menu']['name'] = 'main-menu';
$handler->display->display_options['menu']['context'] = 0;
$handler->display->display_options['menu']['context_only_inline'] = 0;

Can anyone please help me to solve the problem?

Anonymous’s picture

FileSize
812 bytes

Here's a patch that returns 0 if division is done with 0 and removes the error.

joelpittet’s picture

Assigned: peluhnya » Unassigned
Status: Active » Needs review

Setting to needs review to trigger testbot. And unassigning because that rarely is useful.

Status: Needs review » Needs work

The last submitted patch, 4: divide_by_zero.patch, failed testing. View results

joelpittet’s picture

Priority: Critical » Normal

Not sure what makes this critical, it looks like it's working as designed. If the value is 0 then trigger "division by zero". Can someone explain the situation where the patch would be the correct way to do it?

Anonymous’s picture

In my case I have a "statistics" view that calculates average "price per hour" for services sold.
Display also has other statistics values before first entityreference -> service brings numbers to average price/h calculation.

So the result of calculation for the average price per hour is 0 divided by 0 until user adds one or more services.

The only problem is that the error is exposed to average-joe users.
I think the best possible solution would be a warning in view create page and empty result or "-" in a view.

Rudi Teschner’s picture

Patch works. Even though it just should be a special case, its a nice little change because it can not always be predicted if a divisor is set to 0.

in my case i have a simple example of counting wins, draws and losses and divide wins by the total amount of wins, draws and losses to basically get a win rate percentage value.

now if someone was not active and has 0 wins, 0 draws and 0 losses, the error is thrown and altering any "no result" to 1 would basically change the end results.

aludescher’s picture

Re-rolled patch for includes/math-expr.inc

VM’s picture

FileSize
52.72 KB

I can reproduce this issue.

I have an h5p content type of quizzes.

A new user has not taken the quizzes yet and there is yet to be a value contained in the h5p scoring fields within the view upon which the global math expression field tokens are acting. as such it's dividing an empty result by an empty result and throwing the User warning: division by zero in ctools_math_expr->trigger()

The fields without results used in the global math expression are both set with 'Hide if empty'.

see screenshot for visual aid related to view that is output.

I venture the a cause could be that the h59 does not tally the total number of questions but a total number of questions with a submitted response. Thus if there were no submitted responses, then there is a 0 / 0 division expression.

I can deal with the empty results for the time being but I'd like to suppress the warning 'User warning: division by zero in ctools_math_expr->trigger()'. I've attempted the .patch files included here and they were of no help. They don't apply using patch. Applying manually throws the same warning.

VM’s picture

Category: Bug report » Feature request

Correct me if I am wrong here but after digging deeper into this it seems more like an issue with views not respecting the 'hide if empty' setting?

VM’s picture

Component: Views Content » Code
VM’s picture

Category: Feature request » Bug report