r/ProgrammingLanguages • u/Dry_Day1307 • Jun 17 '26
Handling NaN and Infinity normalization in a NaN-boxed VM: Why I made NaN == NaN evaluate to true
Yesterday I shared my open-source language DinoCode. Today I want to discuss a specific design choice I made in my runtime regarding eager NaN and Infinity normalization within my range-based NaN-boxing implementation.
In standard IEEE 754, checking if NaN equals NaN is always false, and there are many bit patterns for it. However, for a bytecode interpreter where execution overhead matters, I wanted to avoid dragging dirty float states through the engine.
The Implementation
In my DinoRef type, which is a transparent wrapper over a u64, I implemented a number constructor that acts as the entry point for raw f64 values.
Rust
#[inline(always)]
pub fn number(value: f64) -> Self {
if !value.is_finite() {
if value.is_nan() {
return Self::NAN;
}
return if value.is_sign_positive() {
Self::INFINITY
} else {
Self::NEG_INFINITY
};
}
Self::float(value)
}Instead of letting dynamic NaN bit-patterns propagate, this constructor eagerly catches them using Rust's native is_finite method. If it is NaN or Infinity, it immediately maps to a predefined raw bit-pattern constant. For example, Self NAN is hardcoded as 0x7FF8000000000000.
Eager Validation Advantage
Because every NaN or Infinity in the VM is strictly normalized to the exact same u64 bit pattern at birth, checking for equality becomes incredibly cheap.
We do not need complex float validations during runtime execution. To see if a value is NaN, we just perform a raw bitwise comparison of the underlying data. As a side effect, NaN equals NaN natively evaluates to true in DinoCode because they share the exact same raw constant.
Encapsulating this validation inside the low-level type abstraction keeps the core execution loop clean and fast.
The Trade-offs
The obvious downside here is the risk of human error. As the VM developer, I have to remember to explicitly route any potentially dangerous math operation through the number constructor. If I forget just once and push a raw f64 directly to the stack, a dynamic NaN could bypass normalization and corrupt the boxing logic.
Besides this explicit maintenance cost, do you identify any other real downsides to this approach?
How do you balance IEEE 754 compliance versus VM performance when designing your type system?
Edit: Thank you so much to everyone who commented and shared their insights on this post! I really appreciate the feedback regarding the IEEE standard and the hardware level implications. I will be meditating on this and potentially transitioning the VM to full NaN payload preservation in a next release since refactoring my internal Rust helpers to use a bitwise mask won't be a catastrophic performance hit anyway. I am wrapping up this discussion for now to process all your great points. Thanks again for helping me look at this from so many different perspectives!
7
u/yuehuang Jun 17 '26
FYI, LLVM has -ftrapping-math to catch NaN usage. Depending on your system, it is settable per OS thread.
6
u/yuri-kilochek Jun 17 '26
You only need
andandcmpto test for any NaN. You've reduced it to justcmpwith the fixed pattern, is that really such a big win that it's worth violating the least surprise principle?24
u/ultrasquid9 Jun 17 '26
You should look into the official total-ordering of IEEE floats. It means your language will be standards-compliant, and lets you use existing comparison logic (for instance, Rust has a
total_cmpmethod on floats) which may be more performant19
u/jsshapiro Jun 18 '26
Breaking the semantics of numbers for the sake of performance is a pretty dumb decision. Why not just declare that the result of all numeric computations is 1? Think of all the computations you would get to elide…
4
u/garnet420 Jun 17 '26
What about negative zero?
1
u/librasteve Jun 18 '26
Good question. I have been noodling a bit with Inf and NaN in raku (see https://cragcli.info) also I wrote an IEEE P754 test suite in my youth. My opinion is that P754 is very, very well engineered. I would advise to stick to the spec. I guess that making NaN == NaN has the same cost as NaN != NaN in your language, so just go with that.