For instance, in my coding I using the VB function
Round(1.325, 2)
Which produces the number 1.33. I am wondering if there is a place to view the code behind the "Round" function so that I can produce my own.
Reason for the question:
I was recently asked in an interview to produce a reverse function that does not use the .Reverse function from the .net library. I nailed it, but there are other basic functions to learn such as formatting a number to a set amount of decimal places. Being able to look at the code behind the .net library functions would aide me in this pursuit.
TIA - Thanks in advance.
James
Code-behind is a technique used in Microsoft programming platforms, including ASP.NET and XAML, where the practice is facilitated by separating the visual and code aspects through the use of partial classes.
Code Behind refers to the code for an ASP.NET Web page that is written in a separate class file that can have the extension of . aspx. cs or . aspx.
The code-behind feature of ASP.NET enables you to divide an ASP.NET page into two files - one consisting of the presentation data, and the second, which is also called the code-behind file, consisting of all the business logic.
. NET has an expansive standard set of class libraries, referred to as either the base class libraries (core set) or framework class libraries (complete set). These libraries provide implementations for many general and app-specific types, algorithms, and utility functionality.
For the portions of the .Net Framework that are implemented in C#, it is easiest to review them using http://referencesource.microsoft.com/. For the rest, you will need to either download a copy of the SSCLI or browse the coreclr repository on GitHub.
There was a free eBook published which tries to explain the inner workings of much of the SSCLI2, which is a slimmed down version of .Net 2.
InternalCall
is handled by the runtime using mappings defined in the file ecalllist.h
in coreclr, or in ecall.cpp
for sscli. For example, Math.Round is mapped as:
// snip
FCFuncStart(gMathFuncs)
FCIntrinsic("Sin", COMDouble::Sin, CORINFO_INTRINSIC_Sin)
FCIntrinsic("Cos", COMDouble::Cos, CORINFO_INTRINSIC_Cos)
FCIntrinsic("Sqrt", COMDouble::Sqrt, CORINFO_INTRINSIC_Sqrt)
FCIntrinsic("Round", COMDouble::Round, CORINFO_INTRINSIC_Round)
// snip
If we then look at the class COMDouble
, whose implementation we can find in comfloat.cpp
, we see the code behind Math.Round
:
/*====================================Round=====================================
**
==============================================================================*/
FCIMPL1_V(double, COMDouble::Round, double x)
FCALL_CONTRACT;
// If the number has no fractional part do nothing
// This shortcut is necessary to workaround precision loss in borderline cases on some platforms
if (x == (double)((INT64)x)) {
return x;
}
// We had a number that was equally close to 2 integers.
// We need to return the even one.
double tempVal = (x + 0.5);
double flrTempVal = floor(tempVal);
if ((flrTempVal == tempVal) && (fmod(tempVal, 2.0) != 0)) {
flrTempVal -= 1.0;
}
return _copysign(flrTempVal, x);
FCIMPLEND
As mentioned in the comments you can download and browse the source code of the .NET Framework at https://referencesource.microsoft.com/.
Note that some managed methods are just wrappers for native methods. You can find the implementation of some of these which are part of .NET Core at GitHub: https://github.com/dotnet/coreclr/tree/32f0f9721afb584b4a14d69135bea7ddc129f755
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With