Implement Square Root Method using C#

Hello friends,

I am here again with another example where we will see how to find square root of a number without using .NET library method.

Let’s start with basic one though it’s not efficient as it considers all the number starting from 0.001 till the difference of multiplying both numbers and the number given is less than the previous iteration.

It’s not efficient as it took 3003 iterations to find the square root of 9.

public static float FindSquareRoot1 (int number)
{
float result = 0;
float diff = 0;
float minDiff = Math.Abs(result * result – number);
int count = 0;
float tempResult = 0;
while (true)
{
tempResult = Convert.ToSingle(count) / 1000;
diff = Math.Abs(tempResult * tempResult – number);
if (diff <= minDiff)
{
minDiff = diff;
result = tempResult;
}
else
return result;
count++;
}
}
Now let’s see another way which is efficient as it uses Binary Search.

It’s really efficient as It took just 17 iterations to find the square root of 9.

public static float FindSquareRoot_BS(int number)
{
float precision = 0.0001f;
float min = 0;
float max = number;
float result = 0;
while (max-min > precision)
{
result = (min + max) / 2;
if ((result*result) >= number)
{
max = result;
}
else
{
min = result;
}
}
return result;
}

Please let me know your comments/suggestion or if you have any better ways to do that.

Hope you liked the post. Please share your comments.

Row Versioning Based Isolation Levels in SQL Server

Hello guys,

Row Versioning based Isolation levels are very useful in reducing blocking. Which are.

  • Read Committed (default) Isolation level with READ_COMMITTED_SNAPSHOT database option is ON.
  • Snapshot Isolation level with the ALLOW_SNAPSHOT_ISOLATION database option is ON.

Read committed isolation using row versioning provides statement-level read consistency however Snapshot isolation provides transaction-level read consistency.

Statement-level read consistency Vs. Transaction-level read consistency

In Statement-level read consistency when each statement within the transaction executes, a new data snapshot is taken and remains consistent for each statement until the statement finishes execution. Enable this when:

  • When concurrency benefits is more important than increased overhead of creating and managing row versions.
  • An application requires absolute accuracy for long-running queries where data values must be consistent to the point in time when a query starts.

In Transaction-level read consistency, a data snapshot is taken when the snapshot transaction starts, and remains consistent for the duration of the transaction, due to that it provides the benefit of repeatable without using shared locks Use snapshot isolation when:

  • Optimistic concurrency control is desired.
  • Probability is low that a transaction would have to be rolled back because of an update conflict (As it happens here, when transaction running under snapshot isolation reads data that is then modified by another transaction, an update by the snapshot transaction to the same data causes an update conflict and the transaction terminates and rolls back. This is not an issue with read committed isolation using row versioning).
  • An application needs to generate reports based on long-running queries that must have point-in-time consistency.

Hopefully you liked the blog. Please share your comments.

Direct Casting, IS and AS Operator in C#

Hello friends,

Below are the available ways in C# when you cast one data type to another.

Direct casting

Direct cast is most common way of casting one type to another, however it yields exception if casting can’t be done.

Below is the example to demonstrate the same.

object length = 3.2;

int test = (int)length; //Fails in runtime

Console.WriteLine(test);

is operator

is operator checks whether an object is compatible with a given type (on the right side) and the result of the evaluation is either true (if casting can be done) or false (if casting can’t be done) and since the result is always a Boolean value so there is no chance of runtime exception.

Below is the example to demonstrate the same.

object length = 3.2;

if (length is int) //Check if length can be cast to int

{

Console.WriteLine(length);

}

as operator

The as operator is like a casting except that it yields null on conversion failure instead of raising an exception. So runtime exception can be avoided using null check.

Below is the example to demonstrate the same.
class MyClass //Sample class to test the scenario

{

int a;

string b;

}

static void CastMe()

{

name = new MyClass();

string test = name as string;

if (test != null) //Check if test is not null means i.e. casting can be done

{

Console.WriteLine(name);

}

}

Hope you liked the post. Please share your comments.