C
C#7mo ago
Paradoxial

Why does this function return NaN?

Hi, I am writing a Sin function in C#, the function uses the taylor series algorithm to calculate the sine of a given angle. However, when given the angle, it returns NaN. Reason as to why?
13 Replies
Paradoxial
ParadoxialOP7mo ago
public static double Sin(double angle) {
double result = 0;
double sign = 1;
angle %= 360;
angle = Radians(angle);
for (int i = 0; i < 20; i++) {
double term = Power(angle, 2 * i + 1) / IntFactorial(2 * i + 1);
result += sign * term;
sign *= -1;
}
return result;

}
public static double Sin(double angle) {
double result = 0;
double sign = 1;
angle %= 360;
angle = Radians(angle);
for (int i = 0; i < 20; i++) {
double term = Power(angle, 2 * i + 1) / IntFactorial(2 * i + 1);
result += sign * term;
sign *= -1;
}
return result;

}
This uses the exactly same algorithm and steps as the one I have written in Python and that works just fine. Around i < 20 causes the issue, however at i <5 - 10, the function just doesn't give a percise answer Feel free to ping me if someone finds a solution
Jimmacle
Jimmacle7mo ago
have you stepped through with a debugger to see where the NaN first gets introduced?
Paradoxial
ParadoxialOP7mo ago
------------------------------------------------------------------------------
You may only use the Microsoft Visual Studio .NET/C/C++ Debugger (vsdbg) with
Visual Studio Code, Visual Studio or Visual Studio for Mac software to help you
develop and test your applications.
------------------------------------------------------------------------------
Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.9\System.Private.CoreLib.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Loaded 'C:\Programming\C# Projects\paramath_c_sharp\bin\Debug\net7.0\paramath_c_sharp.dll'. Symbols loaded.
Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.9\System.Runtime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.9\System.Console.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.9\System.Threading.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.9\System.Text.Encoding.Extensions.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.9\System.Runtime.InteropServices.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Hello from Tamer Alssaleh - Thank you for using Paramath C# Edition
NaN
The program '[976] paramath_c_sharp.exe' has exited with code 0 (0x0).
------------------------------------------------------------------------------
You may only use the Microsoft Visual Studio .NET/C/C++ Debugger (vsdbg) with
Visual Studio Code, Visual Studio or Visual Studio for Mac software to help you
develop and test your applications.
------------------------------------------------------------------------------
Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.9\System.Private.CoreLib.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Loaded 'C:\Programming\C# Projects\paramath_c_sharp\bin\Debug\net7.0\paramath_c_sharp.dll'. Symbols loaded.
Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.9\System.Runtime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.9\System.Console.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.9\System.Threading.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.9\System.Text.Encoding.Extensions.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.9\System.Runtime.InteropServices.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Hello from Tamer Alssaleh - Thank you for using Paramath C# Edition
NaN
The program '[976] paramath_c_sharp.exe' has exited with code 0 (0x0).
this is all i get doesnt really tell me where NaN occurs when i debug it
Jimmacle
Jimmacle7mo ago
$debug
MODiX
MODiX7mo ago
Tutorial: Debug C# code and inspect data - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
Jimmacle
Jimmacle7mo ago
you need to set breakpoints and inspect the values of the variables at different points in the code's execution
Paradoxial
ParadoxialOP7mo ago
I should be able to do this in VSC right? Because thats what im using rn not VS
Jimmacle
Jimmacle7mo ago
i haven't tried to use a debugger in VSC, but if you can there should be resources online about it worst case you can do some good old Console.WriteLine() debugging
Paradoxial
ParadoxialOP7mo ago
yeah the vsc debugger is just shit :/ doesnt tell me anything
Jimmacle
Jimmacle7mo ago
just use VS if that's an option
Cattywampus
Cattywampus7mo ago
Possibly due to the radian conversion is invalid. For instance, negative degrees just a wild guess, you should debug it and see the conversion value
reflectronic
reflectronic7mo ago
the problem is that your IntFactorial overflows to be clear, an int can hold 12! but not 13! a ulong can hold 20! but not 21! because your loop goes up to i = 20, you try evaluating factorials which produce results that do not fit into int this would cause accuracy issues in general, but unfortunately large enough factorials are congruent with 0 mod 2^31, so e.g. IntFactorial(2 * 17 + 1) = 0 this means that you get a divide-by-zero when computing term, which introduces Infinity. the divide-by-zero keeps producing Infinity, but since you alternate signs, you end up with -Infinity + Infinity, which is NaN you can fix this by not doing 20 iterations (e.g. limit i <= 5) if you change it to a ulong instead of an int (i'm assuming it's an int, you did not really share the code for that), then you can change it to i <= 9 and, to be clear, the VS Code debugger is fine. i debugged this issue using it
Paradoxial
ParadoxialOP7mo ago
alright ima try to change that
Want results from more Discord servers?
Add your server