Monday, December 9, 2013

In c++ any value like 4.12 is treated as a double by default. 

Comparing floats and doubles can be a tricky business because of the difference in 

precision leading to minute errors. For example: 

float a = 4.12;

if(a==4.12)
{


cout<<"hello"<<a;
}


else
{


cout<<"bye "<<a;

}

This will show you the output as "bye 4.12"

Why?

Because by default 4.12 is a double (such as in the if statement or in the assignment to our variable), but storing it in a float it loses some precision, and so comparing then comparing a double with a float lead to microscopic changes in the precision of the number--remember that floats and doubles are not precise.

Two lessons here: one is that floating point numbers shouldn't be compared directly most of the time, and the other is that the default size and type of a hard-coded floating point number is double.

simple code in .Net to lock your system like 

screensaver's do.

using System;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{


class Program
{


static void Main(string[] args)
{


LockWorkStation();
}


[DllImport("user32.dll")]
static extern void LockWorkStation();
}


}