SweepFormula: treat non-finite literals (NaN/Inf/-inf) as numeric operands#2762
Conversation
Co-authored-by: t-b <1185677+t-b@users.noreply.github.com>
Co-authored-by: t-b <1185677+t-b@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR updates SweepFormula’s primitive operation execution so that bare non-finite literals (NaN, inf, -inf, …) that are parsed as strings are coerced to numeric operands at operation time, aligning behavior with existing mixed-array handling.
Changes:
- Exposes and reuses the executor’s non-finite conversion helper (with an added empty-input guard) as the single conversion implementation.
- Adds operand-side coercion for primitive operations so
+,-,*,/accept bare non-finite literals as numeric operands. - Re-enables and expands tests for primitive operations with non-finite literals and documents the behavior in SweepFormula docs.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| Packages/tests/Basic/UTF_SweepFormula.ipf | Re-enables primitive-op tests to cover 1±NaN, 1±inf, 1--inf, 1*inf, 1/inf. |
| Packages/MIES/MIES_SweepFormula_Operations.ipf | Coerces per-dataset operands in primitive ops by converting text-only non-finite datasets to numeric waves. |
| Packages/MIES/MIES_SweepFormula_Executor.ipf | Promotes the non-finite conversion helper to file-external and adds an empty-input guard. |
| Packages/doc/SweepFormula.rst | Documents non-finite literals as valid operands for primitive operations. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| /// @brief Convert a text wave that consists solely of the non-finite literals inf/-inf/NaN into a numeric wave | ||
| /// | ||
| /// @return numeric wave with the converted values or a null wave reference if at least one element is not a | ||
| /// non-finite literal |
| The non-finite literals `inf`, `-inf` and `NaN` are also accepted as operands of the primitive | ||
| operations `+`, `-`, `*` and `/`, so that e.g. `1 - NaN` and `1 - inf` are valid and evaluate them | ||
| as numeric values. |
Co-authored-by: t-b <1185677+t-b@users.noreply.github.com>
Co-authored-by: t-b <1185677+t-b@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.
Suppressed comments (3)
Packages/MIES/MIES_SweepFormula_Executor.ipf:701
- The doc comment lists only
inf/-inf/NaN, but the implementation also recognizes-NaN(seeSFE_ConvertNonFiniteElementsImpl). Please include-NaNin the comment to avoid misleading future readers.
/// @brief Convert a text wave that consists solely of the non-finite literals inf/-inf/NaN into a numeric wave
///
/// @return numeric wave with the converted values or a null wave reference if at least one element is not a
/// non-finite literal
Packages/MIES/MIES_SweepFormula_Operations.ipf:2574
- The docstring says only
inf/-inf/NaN, but the conversion routine also supports-NaNand the user-facing docs mention it. Please update this comment so the accepted literals are accurately documented.
/// @brief Convert a dataset that consists solely of the non-finite literals inf/-inf/NaN into a numeric wave
///
/// This allows primitive operations like `1 - NaN` or `1 - inf` where a non-finite value is entered as bare
/// literal and is therefore kept as string by the parser. Returns the input unchanged if it is not textual or if
/// it contains at least one element that is not a non-finite literal.
Packages/MIES/MIES_SweepFormula_Helpers.ipf:70
- This helper comment mentions only
inf/-inf/NaN, butSFE_ConvertNonFiniteElementsalso accepts-NaN. Keeping the list in sync with the conversion logic avoids confusion.
// the non-finite literals inf/-inf/NaN are kept as text by the parser (see #1863),
// coerce them to numeric so they are accepted wherever a numeric argument is taken
|
@copilot The case where the formula is just one of inf/-inf/NaN like gives "cannot plot a single text wave" whereas this works with just |
Co-authored-by: t-b <1185677+t-b@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.
Suppressed comments (3)
Packages/MIES/MIES_SweepFormula_Operations.ipf:2604
- This comment says inf/-inf/NaN, but "-NaN" is also treated as a non-finite literal by SFE_ConvertNonFiniteElements. Please include -NaN to match the actual accepted set.
// treat the non-finite literals inf/-inf/NaN as numeric operands, see #1863
Packages/MIES/MIES_SweepFormula_Helpers.ipf:70
- The comment mentions inf/-inf/NaN, but the implementation also accepts "-NaN" (see SFE_ConvertNonFiniteElementsImpl). Please include -NaN here to keep the documentation accurate.
// the non-finite literals inf/-inf/NaN are kept as text by the parser (see #1863),
// coerce them to numeric so they are accepted wherever a numeric argument is taken
Packages/doc/SweepFormula.rst:201
- The array-evaluation docs currently state that
["NaN"]returns a text wave. With this PR, JSON strings equal to non-finite literals are coerced to numeric (via SFE_FormulaExecutorStringOrVariable), so["NaN"]will evaluate to a numeric wave{NaN}. Please update this example/text to match the new behavior.
The array evaluation supports numeric and text data. The interpretation of the JSON arrays as
text data is preferred. This means that `["NaN"]` returns a one element text wave `{"NaN"}`,
whereas `[1, "NaN"]` returns a two element numeric wave `{1, NaN}`. If one element can not be
parsed as string then it is assumed that the array contains numeric data.
The non-finite literals `inf`, `-inf`, `NaN` and `-NaN` are also accepted as standalone expressions and as operands
Thanks for opening a PR in MIES ✨!
here for the detailed explanation.
Description
Bare non-finite literals as operands of primitive operations (
1 - NaN,1 - inf, ...) aborted withOperand for - must be numeric.. JSON can't hold non-finite numbers, so the parser keeps them as strings; the executor now coerces them to numeric at operation time — matching how arrays like[1, "NaN"]already behave.Changes:
SFE_ConvertNonFiniteElementsto file-external and added an empty-input guard, so the existinginf/-inf/NaN/-NaNrecognition is the single source of truth.SFO_ConvertNonFiniteText; the primitive-op dispatcherSFO_IndexOverDataSetsForPrimitiveOperationconverts both operands before applying+,-,*,/. Text that isn't purely non-finite is left untouched (still errors as before).TestNonFiniteValuesPrimitiveOperationscovering1±NaN,1±inf,1--inf,1*inf,1/inf.SweepFormula.rst.