Skip to content

Compiling over the reals can refuse what is not real - #2297

Merged
Rafael-SOWNet merged 2 commits into
masterfrom
a-real-compilation-can-refuse-what-is-not-real
Oct 31, 2026
Merged

Rafael-SOWNet merged 2 commits into
masterfrom
a-real-compilation-can-refuse-what-is-not-real

Conversation

@Rafael-SOWNet

Copy link
Copy Markdown
Member

This answers the question left on #2286. A function compiled to return double or float computes every intermediate value as a real number. So sin(arcsin(x)) at 2, whose value is 2, comes out as NaN, just as 1/x does at 0, where there is no value at all. That behaviour stays as it is, and it is now documented on every Compile overload.

MathS.Settings.RefuseNonRealCompilation, off by default, refuses such an expression instead. With it on, an expression that can't be shown to be real wherever it is defined throws UncompilableNodeException, so a NaN from the compiled function means only that there is no value. The check is TreeAnalyzer.IsRealValued, with every argument written as one fresh variable. It is conservative: sqrt(x) and ln(x) are refused too. The compilers over Complex, and statements, are unaffected.

Console.WriteLine("sin(arcsin(x))".Compile<double, double>("x")(2));   // NaN
using var _ = MathS.Settings.RefuseNonRealCompilation.Set(true);
"x / (1 + x^2)".Compile<double, double>("x");                          // compiles
"sin(arcsin(x))".Compile<double, double>("x");                         // throws UncompilableNodeException

IsRealValued now also reads floor, ceil, round, min and max of real arguments as real. Without that, the setting would refuse floor(x). The change also gives answers where the derivative of a modulus or a sign needs to know its argument is real:

Input 2.6.0 and master Now
derivative(abs(floor(x)), x) unevaluated 0 provided not floor(x) = 0 and not x in ZZ
derivative(abs(min(x, 1)), x) unevaluated sgn(min(x, 1)) * (1 - sgn(x - 1)) / 2 provided ...
limit(sin(floor(x)) / x, x, +oo) 0 in 2.6.0, unevaluated on master since #2284 0

RefuseNonRealCompilationTest covers:

  • the default NaN
  • the refusals over double and float
  • real expressions still compiling
  • division by zero still giving NaN
  • the Complex compiler and a statement left alone

PublicApi.txt gains the setting. The setting's documented example runs under DocumentationExamplesRunTest. The suite (18,460), the trio, F#, AOT and the eight harnesses pass locally.

Fixes #2286.

🤖 Generated with Claude Code

Compiled into a function returning double or float, every value
computed on the way is real, so sin(arcsin(x)) at 2, which is 2, is
NaN, as 1/x is at 0 where there is no value. That is the behaviour, and
it is now written on every Compile overload.

MathS.Settings.RefuseNonRealCompilation, off by default, refuses such
an expression instead: with it on, an expression that cannot be shown
real wherever it is defined throws UncompilableNodeException, so a NaN
from the compiled function means only that there is no value. The
showing is TreeAnalyzer.IsRealValued, with each argument written as
one fresh variable, and it is conservative: sqrt(x) and ln(x) are
refused too. The Complex compilers and statements are unaffected.

IsRealValued now reads floor, ceil, round, min and max of real
arguments as real, which the setting needs and which also answers the
derivative of a modulus or a sign of one: d/dx abs(floor(x)) was left
unevaluated. limit(sin(floor(x)) / x, x, +oo) is 0 again, as in 2.6.0:
#2284 took away the l'Hopital route that had answered it.

RefuseNonRealCompilationTest takes the default NaN, the refusals over
double and float, what is real still compiled, a division by zero still
NaN, and the Complex compiler and a statement unaffected. PublicApi.txt
gains the setting. The suite, the trio, F#, AOT and the eight harnesses
pass.

Fixes #2286.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@Rafael-SOWNet Rafael-SOWNet added this to the 2.7.0 milestone Oct 31, 2026
@Rafael-SOWNet Rafael-SOWNet moved this from Todo to In review in AngouriMath Oct 31, 2026
@codecov

codecov Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.61538% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.72%. Comparing base (17cd9da) to head (7fdea1a).

Files with missing lines Patch % Lines
...s/AngouriMath/Functions/TreeAnalyzer/RealValued.cs 33.33% 1 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2297      +/-   ##
==========================================
- Coverage   86.73%   86.72%   -0.01%     
==========================================
  Files         310      310              
  Lines       67960    67973      +13     
  Branches    19740    19744       +4     
==========================================
+ Hits        58944    58952       +8     
- Misses       4884     4887       +3     
- Partials     4132     4134       +2     
Flag Coverage Δ
cpp 37.77% <ø> (ø)
csharp 87.27% <84.61%> (-0.01%) ⬇️
fsharp 32.97% <ø> (ø)
interactive 40.72% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...s/AngouriMath/Convenience/CompilationExtensions.cs 100.00% <ø> (ø)
Sources/AngouriMath/Convenience/MathS.cs 90.51% <100.00%> (+0.02%) ⬆️
...h/Functions/Compilation/Compile.Linq.Definition.cs 47.05% <ø> (ø)
...Functions/Compilation/IntoLinq/IntoLinqCompiler.cs 97.40% <100.00%> (+0.34%) ⬆️
...s/AngouriMath/Functions/TreeAnalyzer/RealValued.cs 80.00% <33.33%> (-1.53%) ⬇️

... and 5 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

CompilationExtensions.cs and Compile.Linq.Definition.cs are generated
from their .tt templates, and the remark had been written into the
output only, so regenerating dropped it and Generated Files Up To Date
failed. regenerate_all.sh now reproduces the committed files exactly.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@Rafael-SOWNet
Rafael-SOWNet merged commit c633c41 into master Oct 31, 2026
36 checks passed
@github-project-automation github-project-automation Bot moved this from In review to Done in AngouriMath Oct 31, 2026
@Rafael-SOWNet
Rafael-SOWNet deleted the a-real-compilation-can-refuse-what-is-not-real branch October 31, 2026 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Compiled over double, a division by zero is Infinity where evaluation says NaN

1 participant