Skip to content

#13603 Fix silent wrong results when combining aggregating functions with if in calculator expressions#692

Draft
Copilot wants to merge 2 commits intodevfrom
copilot/investigate-issue-13603
Draft

#13603 Fix silent wrong results when combining aggregating functions with if in calculator expressions#692
Copilot wants to merge 2 commits intodevfrom
copilot/investigate-issue-13603

Conversation

Copy link

Copilot AI commented Feb 19, 2026

When using min, max, sum, or avg with a single vector argument inside an if expression, expandIfStatements() was producing silently wrong results — no error, just incorrect data.

Root Cause

expandIfStatements() blindly replaces every vector variable with its indexed form var[i]. This corrupts aggregating calls:

c := if(a > min(b), a, b)

Expanded (buggy):

for (var i := 0; i < min(c[], a[], b[]); i += 1)
{
    c[i] := if(a[i] > min(b[i]), a[i], b[i]);  // min(scalar) = scalar, not global min!
}

In exprtk, min(scalar) returns the scalar unchanged (vararg_min_op::process_1), so the global minimum threshold silently becomes a per-element comparison.

Fix

Before the element-wise replacement loop, detect func(single_var) patterns (min, max, sum, avg) and pre-compute them as scalar var declarations outside the for loop:

var ri_agg_0 := min(b);
for (var i := 0; i < min(c[], a[], b[]); i += 1)
{
    c[i] := if(a[i] > ri_agg_0, a[i], b[i]);
}

Multi-argument calls like min(a, b) are unaffected — only sole-argument aggregations are pre-computed.

Changes

  • ThirdParty/expressionparser/ExpressionParserImpl.cpp: Pre-compute aggregating function calls with a single vector argument as scalars before the for loop expansion
  • ApplicationLibCode/UnitTests/RicExpressionParser-Test.cpp: Add ExpandIfWithMinAggregation test verifying c := if(a > min(b), a, b) uses the global minimum, not per-element comparison

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…h if in calculator expressions

When aggregating functions (min, max, sum, avg) with a single vector argument
appear inside if-expressions, the expandIfStatements() function was incorrectly
replacing the vector variable with an indexed version (min(b) -> min(b[i])).

In exprtk, min(b[i]) with a single scalar argument returns b[i] unchanged,
so instead of getting the global minimum of b as the threshold, the comparison
was done element-wise against each b[i]. This produced wrong results silently.

Fix: Before the element-wise expansion, detect and pre-compute aggregating
function calls with single vector arguments as scalar values using exprtk's
var declarations before the for loop.

Added test: ExpandIfWithMinAggregation verifies the fix

Co-authored-by: magnesj <1793152+magnesj@users.noreply.github.com>
Copilot AI changed the title [WIP] Investigate issue related to ResInsight functionality #13603 Fix silent wrong results when combining aggregating functions with if in calculator expressions Feb 19, 2026
Copilot AI requested a review from magnesj February 19, 2026 13:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments