-1

I have the following:

        int val1 = 2;
        float val2 = (float)val1;

        decimal val3 = 3.2m;
        float val4 = (float)val3;

        float Result2 = Math.Pow(val4, val2);

It seems to me supposedly val4 and val2 are both in the float type. However i get a error on Math.Pow everytime signaling it can't convert type double to type float implicitly. I think i'm casting everything correctly, am I missing something?

user2864740
  • 60,010
  • 15
  • 145
  • 220
ng80092a
  • 77
  • 1
  • 1
  • 9
  • The error message means exactly what it says: "can't convert [*double* -> *float*]". It can trivially reproduced: `double d = 1; float f = d;` So then find out which double expression is attempted to be used as a float. Now the problem is isolated a simple solution is present, as shown by R.T. Alternatively, use `double` over `float` everywhere and avoid the conversions. – user2864740 Aug 31 '14 at 18:20

1 Answers1

6

Math.Pow takes two double arguments and returns a double - there is no implicit conversion from double to float. And you cannot put the double in a float variable so you need to typecast that to float.

You may try this:

float Result2= (float)Math.Pow(val4, val2);
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331