Math.Sqrt would give NaN if delta is negative.
As a sidenote, Math.Sqrt returns double. You do not have to convert that to double after.
Nice! I forgot about that one. It’s indeed possible that D (delta) < 0. Considering that this solution tries to apply the quadratic formula, there is still a solution if D < 0 but it has to be handled as a special case which produces two complex roots.
Edit: mobile typos
First, please provide your code as text instead of a screenshot.
Can you show us your inputs and the actual output? I assume the program prints NaN because it cannot actually return anything.
Also, after reading the inputs (when you assign delta, e, f) you don’t need the Convert.ToDouble
wrappers because the invoked Math methods already return double
.
You may be better off avoiding Convert.ToDouble
entirely because it depends on the culture of your system (or current thread) and may produce different results on another computer.
Lol im a bigger newbie then, sorry!
i doubt the program would return NaN because c# is a type safe language.
Edit: Apparently NaN is actually a thing! https://docs.microsoft.com/en-us/dotnet/api/system.double.nan?view=net-5.0
However, if you’re somehow getting it, i would recommend setting a breakpoint at the start, and inspecting the variables (by hovering your cursor over it) as you step through it (with f10).
Good luck!
nan is a valid floating point value, being type safe doesn’t in anyway way relate to whether you can get a nan
They are all declared. They are not initialized at declaration, but that is not necessary.
C# devs
null reference exceptions