LykosAI/StabilityMatrix · error · InvalidCastException

Cannot convert NaN to a numeric type

Error message

Cannot convert NaN to a numeric type

What it means

NullableDefaultNumericConverter.Unbox handles NaN of the target numeric type via a NanHandling option. With ReturnBehavior.Throw and a NaN input, it throws InvalidCastException('Cannot convert NaN to a numeric type') during ConvertBack, signaling that NaN cannot be represented in the requested conversion.

Solutions

  1. Configure the converter with ReturnBehavior.DefaultValue so NaN maps to the default value
  2. Make the bound property nullable and handle empty input before conversion
  3. Add ValidationRule/property validation to prevent NaN from reaching ConvertBack
  4. Switch the binding to use a value that cannot be NaN (e.g. clamp in the ViewModel)

Example fix

// before
new NullableDefaultNumericConverter<double, double>(ReturnBehavior.Throw)
// after
new NullableDefaultNumericConverter<double, double>(ReturnBehavior.DefaultValue)
Defensive patterns

Strategy: fallback

Validate before calling

if (double.IsNaN(value)) value = 0d; // sanitize before ConvertBack

Type guard

bool IsFinite(double d) => !double.IsNaN(d) && !double.IsInfinity(d);

Try / catch

try { result = converter.ConvertBack(value, targetType, param, culture); }
catch (InvalidCastException) { result = 0d; }

Prevention

When it happens

Trigger: ConvertBack receives a value (e.g. double.NaN from an empty/reset TextBox bound to a non-nullable numeric property) while the converter was constructed with NanHandling = ReturnBehavior.Throw.

Common situations: User clears a numeric input, producing NaN in the binding pipeline; a slider or spinner emits NaN; converter configured with Throw mode but the UI legitimately produces NaN.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of LykosAI/StabilityMatrix@af93d6ef57 (2026-09-12). Data as JSON: /api/errors/22c988c2410fd8d9. Report an issue: GitHub.

Appendix: source

Thrown at StabilityMatrix.Avalonia/Converters/NullableDefaultNumericConverter.cs:34

    public ReturnBehavior NanHandling { get; set; } = ReturnBehavior.DefaultValue;

    /// <summary>
    /// Unboxes a nullable value type
    /// </summary>
    private TSource Unbox(TTarget? value)
    {
        if (!value.HasValue)
        {
            return default;
        }

        if (TTarget.IsNaN(value.Value))
        {
            return NanHandling switch
            {
                ReturnBehavior.DefaultValue => default,
                ReturnBehavior.Throw
                    => throw new InvalidCastException("Cannot convert NaN to a numeric type"),
                _
                    => throw new InvalidEnumArgumentException(
                        nameof(NanHandling),
                        (int)NanHandling,
                        typeof(ReturnBehavior)
                    )
            };
        }

        return (TSource)System.Convert.ChangeType(value.Value, typeof(TSource));
    }

    /// <summary>
    /// Convert a value type to a nullable value type
    /// </summary>
    public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        if (targetType != typeof(TTarget?) && !targetType.IsAssignableTo(typeof(TTarget)))

View on GitHub (pinned to af93d6ef57)