Created
December 10, 2023 22:07
-
-
Save DoCTheBest01/8697fa8284847c8f24821e7bacccc86e to your computer and use it in GitHub Desktop.
CSharp Nth Root Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static Type Root<Type>(dynamic number, int root) where Type : struct | |
| { | |
| const double epsilon = 1e-10; // Tolerance level for convergence | |
| Type guess = (Type)Convert.ChangeType(number / root, typeof(Type)); // Initial guess | |
| while (Absolute<Type>(Power<Type>(guess, root) - number) > epsilon) | |
| { | |
| guess = (Type)Convert.ChangeType(((dynamic)(root - 1) * (dynamic)guess + (dynamic)number / Power<Type>((dynamic)guess, root - 1)) / root, typeof(Type)); | |
| } | |
| return guess; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment