{"record":{"id":"22c988c2410fd8d9","repo":"LykosAI/StabilityMatrix","slug":"cannot-convert-nan-to-a-numeric-type","errorCode":null,"errorMessage":"Cannot convert NaN to a numeric type","messagePattern":"Cannot convert NaN to a numeric type","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Avalonia/Converters/NullableDefaultNumericConverter.cs","lineNumber":34,"sourceCode":"    public ReturnBehavior NanHandling { get; set; } = ReturnBehavior.DefaultValue;\n\n    /// <summary>\n    /// Unboxes a nullable value type\n    /// </summary>\n    private TSource Unbox(TTarget? value)\n    {\n        if (!value.HasValue)\n        {\n            return default;\n        }\n\n        if (TTarget.IsNaN(value.Value))\n        {\n            return NanHandling switch\n            {\n                ReturnBehavior.DefaultValue => default,\n                ReturnBehavior.Throw\n                    => throw new InvalidCastException(\"Cannot convert NaN to a numeric type\"),\n                _\n                    => throw new InvalidEnumArgumentException(\n                        nameof(NanHandling),\n                        (int)NanHandling,\n                        typeof(ReturnBehavior)\n                    )\n            };\n        }\n\n        return (TSource)System.Convert.ChangeType(value.Value, typeof(TSource));\n    }\n\n    /// <summary>\n    /// Convert a value type to a nullable value type\n    /// </summary>\n    public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)\n    {\n        if (targetType != typeof(TTarget?) && !targetType.IsAssignableTo(typeof(TTarget)))","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Avalonia/Converters/NullableDefaultNumericConverter.cs#L16-L52","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Configure the converter with ReturnBehavior.DefaultValue so NaN maps to the default value","Make the bound property nullable and handle empty input before conversion","Add ValidationRule/property validation to prevent NaN from reaching ConvertBack","Switch the binding to use a value that cannot be NaN (e.g. clamp in the ViewModel)"],"exampleFix":"// before\nnew NullableDefaultNumericConverter<double, double>(ReturnBehavior.Throw)\n// after\nnew NullableDefaultNumericConverter<double, double>(ReturnBehavior.DefaultValue)","handlingStrategy":"fallback","validationCode":"if (double.IsNaN(value)) value = 0d; // sanitize before ConvertBack","typeGuard":"bool IsFinite(double d) => !double.IsNaN(d) && !double.IsInfinity(d);","tryCatchPattern":"try { result = converter.ConvertBack(value, targetType, param, culture); }\ncatch (InvalidCastException) { result = 0d; }","preventionTips":["Configure NanHandling = ReturnBehavior.DefaultValue for user-input bindings","Make bound properties nullable (double?) to absorb empty input","Clamp/sanitize numeric inputs in the ViewModel"],"tags":["avalonia","converter","nan","binding"],"backgroundTag":"type-mismatch","analyzedSha":"af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002","analyzedAt":"2026-09-12T19:02:43.389Z","contentChangedAt":"2026-09-12T19:02:43.389Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}