I discovered, that I can write 1E5
or 1e5
instead of 100000
. Is there a way to use this operator/method/something with variables? I know I can use Math.Pow(10, 5);
, but I wanted to know if this this E
works with variables, too.

- 1,891
- 3
- 31
- 52
-
I'm not sure the compiler can tell the difference between "MyVariableNameE5" which means to exponentiate the value of "MyVariableName" by 10 x 10^5 and "MyVariableNameE5" which is just a variable name. – NWard Feb 28 '14 at 14:55
-
As a workaround, you could write an extension method to simulate similar syntax/usage: `myNumber.E(5)` – Chris Sinclair Feb 28 '14 at 14:58
-
2'e' here is not an operator or method but rather just part of the lexical specification of a floating-point value. "1e5" is a single indivisible token, not three. – Trillian Feb 28 '14 at 15:00
5 Answers
The e
is not an operator. Is is part of the real literal syntax of the C# language. It is evaluated when the code is parsed and can only be used to express a constant literal.
Just like you can use suffixes f
to denote a float, or d
to denote a double, the XeY
syntax allows you to define doubles with the value X * 10^Y
. It’s simply meant as a convenience when you’re writing code with constant numbers.
Here are some more examples on what you can do with all these different syntaxes, and which type and constant literal they are equivalent to:
var a = 2e5; // (double) 200000
var b = 2.3e3; // (double) 2300
var c = 13f; // (single) 13
var d = 53d; // (double) 53
var e = 1.2e3f; // (single) 1200
var f = 2.3m; // (decimal) 2.3
var g = 1.23e-4m; // (decimal) 0.000123
However, none of that is exposed to the .NET runtime, it is all evaluated at compile time and stored as exposed as constant literals—there is absolutely no difference between the short syntax and the full constant. As such, this functionality is not available for variables which only exist at run-time. If you need to exponentiate those, you will have to use other methods, for example myVariable * 10e5
.

- 369,085
- 72
- 557
- 602
The E notation is only for constants. Math.Pow(x, 5)
is the only built-in way to exponent a variable.
EDIT
However, Math.Pow
accepts double
s and returns a double
, so if you want to exponent an integer, you should look at this answer: this answer
-
1`Math.Pow` is **not** the only way. See [this answer.](http://stackoverflow.com/a/384695/3010968) – Selman Genç Feb 28 '14 at 15:02
-
Okay, yes.. You can also say `x * x * x * x * x`, but that's not exponential, that's repeated multiplication. And that's all the other answer is. – wizulus Feb 28 '14 at 15:04
-
1@alancnet It is the integer equivalent of `Math.Pow(double,double)`; as C# does not have deterministic floating point arithmetic, that answer is an alternative with significance. – Mr. Smith Feb 28 '14 at 15:08
-
Note that `Math.Pow(x, 5)` wouldn’t be even remotely the same as “`Xe5`”. – poke Feb 28 '14 at 15:11
You could use the bit shift operator to do something similar:
int i = 1;
Console.WriteLine(i << 3); // 8
The bitshift operator <<
does exactly the same thing as the E in 1E5. Only in base 2, not 10.

- 11,558
- 1
- 40
- 60
-
`3 << 2` gives 12, not 27. Bit shift equals exponentiation for base 2 **only** – J0HN Feb 28 '14 at 15:05
-
1Do you mean to say `i^3 = 8`? Bit shifting is not a general purpose exponentiation operation. – Simon Belanger Feb 28 '14 at 15:07
-
@alancnet: it is exponentiation in the sense that it doubles each time you increment by one. – heijp06 Feb 28 '14 at 15:09
-
The 1E5 notation refers to a numeric value, which, by definition is a value. Your variable might have a numeric value provided that it is of numeric type (like double), but it can change its value (this is why it is called a variable). So, your variable is a placeholder for values, not a value and you cannot use this notation, as it is reserved only for values.

- 64,414
- 37
- 100
- 175
The use of "e" as an operator(ish) is restricted to constants only.
If this worked on variables, it would cause havoc...eg:
Hello = 10;
H = 1;
llo = 5;
x = Hello
Does "x" now equal 10 or 1e5?
Math.Pow(x,y)
is your best bet.

- 749
- 7
- 21
-
But if `const int x = 10;` could you then write something like `const int y = x e 5;`? The answer is "no", because *Integer Literals*, and *Floating-Point Literals* are the restrictions, rather than *constants*. – Mr. Smith Feb 28 '14 at 15:33