TheDeveloperBlog.com

Home | Contact Us

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

<< Back to VBNET

VB.NET Math.Sqrt Function

Get square roots by invoking the Math.Sqrt Function on Double arguments.
Math.Sqrt takes the square root of a number. With it, you pass one Double value as an argument, and receive the square root of that as another Double. This method requires more time to compute fractional square roots.Double
Example. This program is self-explanatory: it takes the square roots of four Doubles and prints them with Console.WriteLine. Notably, we can see that the square root of 1 is 1, and the square root of four is two.
VB.NET program that calls Math.Sqrt Module Module1 Sub Main() Dim result1 As Double = Math.Sqrt(1) Dim result2 As Double = Math.Sqrt(2) Dim result3 As Double = Math.Sqrt(3) Dim result4 As Double = Math.Sqrt(4) Console.WriteLine(result1) Console.WriteLine(result2) Console.WriteLine(result3) Console.WriteLine(result4) End Sub End Module Output 1 1.4142135623731 1.73205080756888 2
Internals. In the .NET Framework, the Math.Sqrt function is not implemented in managed code. Rather, it calls into an unmanaged DLL. This means we cannot see its implementation in IL Disassembler.IL Disassembler Tutorial
Implementation of Sqrt: IL .method public hidebysig static float64 Sqrt(float64 d) cil managed internalcall { } // end of method Math::Sqrt
Performance. Let's look into the performance characteristics of Math.Sqrt. Some arguments to Math.Sqrt are much faster than others. For example, Math.Sqrt(4) only takes two nanoseconds, while Math.Sqrt(4.1) takes six nanoseconds.

Tip: The performance of Math.Sqrt largely depends on what arguments you pass to it.

Benchmarks
VB.NET program that times Math.Sqrt Module Module1 Sub Main() Dim m As Integer = 10000000 Dim s1 As Stopwatch = Stopwatch.StartNew For i As Integer = 0 To m - 1 If Math.Sqrt(4) = 1 Then Throw New Exception End If Next s1.Stop() Dim s2 As Stopwatch = Stopwatch.StartNew For i As Integer = 0 To m - 1 If Math.Sqrt(4.1) = 1 Then Throw New Exception End If Next s2.Stop() Dim u As Integer = 1000000 Console.WriteLine(((s1.Elapsed.TotalMilliseconds * u) / m).ToString("0.00 ns")) Console.WriteLine(((s2.Elapsed.TotalMilliseconds * u) / m).ToString("0.00 ns")) End Sub End Module Output 2.61 ns 6.20 ns
Summary. We examined several aspects of Math.Sqrt. We called it in a program. We learned where it is implemented in the .NET Framework. And we were intrigued by how much the argument to the method affects the time it requires to compute its result.
© 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