1

How can I do this in C#? Is the only way to use Math.Pow?

Levi H
  • 3,426
  • 7
  • 30
  • 43
  • 2
    5*10*10*10*10 :) What is wrong with Math.Pow? http://blogs.msdn.com/b/csharpfaq/archive/2004/03/07/why-doesn-t-c-have-a-power-operator.aspx – amit_g Nov 10 '11 at 21:47
  • 2
    Do you need a literal like that, or do you need to perform exponentiation using variables? The former can be done quite simply: 5*(10^4) can be written in C# as `5E4`. – dlev Nov 10 '11 at 21:47
  • See also: http://stackoverflow.com/questions/64639/convert-from-scientific-notation-string-to-float-in-c-sharp – Rick Liddle Nov 10 '11 at 21:48
  • @dlev The `^` symbol in C# is a bitwise XOR, not a power operator. So instead of `5*(10^4)` being 5000 as you suggest, it is actually 70. – Peter Morris Nov 10 '17 at 11:26

2 Answers2

9

You can write the literal float constant 5.0e4 (and it works in C, C++, Java, Fortran also).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
2

You can create your own method that does it for you if you don't wish to use Math.Pow.

C# Efficient Algorithm Integer Based Power Function

Community
  • 1
  • 1
5StringRyan
  • 3,604
  • 5
  • 46
  • 69