Math.Pow gives “Cannot implicitly convert type 'double' to 'float' ” error

前端 未结 4 901
暖寄归人
暖寄归人 2021-01-21 12:23

In this program I am trying to create a simple calculator. However, I can\'t seem to find a way to overcome the aforementioned error when reaching the Math.Pow line

4条回答
  •  轮回少年
    2021-01-21 13:21

    Use double instead of float for your variables.

    The float data type has quite limited precision, so the rounding errors (that are always present in floating point arithmetics) are relatively large.

    The reason that you see the wrong result of the square and square root, is that you never show the result at all. Change one of the {0} in each format string into {1}:

    Console.WriteLine(" {0} squared results in {1}",x, power);
    Console.WriteLine(" Square root of {0} is: {1}", x, sqrt);
    

提交回复
热议问题