#13603 Fix silent wrong results when combining aggregating functions with if in calculator expressions#692
Draft
#13603 Fix silent wrong results when combining aggregating functions with if in calculator expressions#692
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When using
min,max,sum, oravgwith a single vector argument inside anifexpression,expandIfStatements()was producing silently wrong results — no error, just incorrect data.Root Cause
expandIfStatements()blindly replaces every vector variable with its indexed formvar[i]. This corrupts aggregating calls:Expanded (buggy):
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 scalarvardeclarations outside the for loop: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 expansionApplicationLibCode/UnitTests/RicExpressionParser-Test.cpp: AddExpandIfWithMinAggregationtest verifyingc := 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.