double is just a larger float. Math.Sqrt() returns a double, not a float. You can fit more numbers in a double than the float type can accurately represent, and so in your code the compiler can't promise that an automatic conversion won't not lose important data, hence the exception.
To get around this, you have two options:
- explicit cast:
float ans = (float)Math.Sqrt(a*b);
- use a double:
double ans = Math.Sqrt(a*b);
Of the two, I recommend the latter.
As a side note, the reverse conversion is okay because double can always accurately represent anything you might find in a float variable. For example, this is perfect okay from the type system's point of view:
float divide(int a, int b) { return a/(float)b;}
double ans = divide(5,2);