TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# Math.Sqrt Method

Use the Math.Sqrt method from the System namespace to compute square roots.
Math.Sqrt computes a square root value at runtime. A square root is the number that, when multiplied by itself, equals the original number. Sqrt is a slower computation. It can be cached for a performance boost.
Example. The Math.Sqrt method is called with a double argument. In this program, the integer arguments to Math.Sqrt are converted to doubles at compile-time. The Sqrt method returns another double.Double
C# program that uses Math.Sqrt using System; class Program { static double[] _lookup = new double[5]; static void Main() { // Compute square roots by calling Math.Sqrt. double a = Math.Sqrt(1); double b = Math.Sqrt(2); double c = Math.Sqrt(3); double d = Math.Sqrt(4); // Store square roots in lookup table. var lookup = _lookup; lookup[1] = a; lookup[2] = b; lookup[3] = c; lookup[4] = d; Console.WriteLine(a); Console.WriteLine(b); Console.WriteLine(c); Console.WriteLine(d); Console.WriteLine(lookup[1]); Console.WriteLine(lookup[2]); Console.WriteLine(lookup[3]); Console.WriteLine(lookup[4]); } } Output 1 1.4142135623731 1.73205080756888 2 1 1.4142135623731 1.73205080756888 2
Lookup tables. Here is an effective tip for performance optimization. You can cache the results of functions such as Sqrt in arrays or other lookup structures. This sometimes yields impressive performance boosts.

So: To get the Sqrt(1), you can instead use lookup[1]. This reduces a complex mathematical routine to a simple memory access.

More information about this optimization is available in the book Code Complete. And a short review of Code Complete is available on this website. As a developer, you should use a variety of reference books.
Summary. The Sqrt method implements classic mathematical functionality in the C# language. This logic can be slow. It can be effectively stored in an array (or other lookup table) and then instantly fetched.

Tip: You can research this by studying the memory hierarchy. You can also use the Math.Pow method to reverse Sqrt.

Locality of ReferenceMath.Pow
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf