6

I wanna know if it is possible to use the scientific notation with variables?

For example:

int n;
cin >> n;
int x = 1en;

instead of

int x = 1e8

Is it possible? If yes, how?

toxicate20
  • 5,270
  • 3
  • 26
  • 38
Milad R
  • 1,854
  • 9
  • 25
  • 36
  • I know that, but "pow" takes O(logn) and I wanted to avoid using it here. – Milad R Nov 24 '12 at 11:09
  • 4
    What makes you think that *"pow" takes O(logn)* ? – Paul R Nov 24 '12 at 11:09
  • Write your own power function, as per requirement – asheeshr Nov 24 '12 at 11:11
  • What makes you think that `1eN` would perform better than `pow(1,N)`? – tenfour Nov 24 '12 at 11:15
  • you mean that it takes less than O(logn)? – Milad R Nov 24 '12 at 11:16
  • 2
    I mean, since both would be evaluated at runtime, they would theoretically perform exactly the same. It doesn't matter what the algorithm is. – tenfour Nov 24 '12 at 11:18
  • 1
    pow is imlemented using very little machine instructions on x86. have a look at http://stackoverflow.com/questions/4638473/how-to-powreal-real-in-x86 – Sebastian Cabot Nov 24 '12 at 11:20
  • I thought it uses something like "shifting the digits" for scientific notation. But as you give me negatives for such question which wasn't asked by another person here, and it is not trivial, I will try not to ask questions here, anymore. Why are you so nervous and unhappy against beginner's questions?! – Milad R Nov 24 '12 at 11:29
  • 1
    "shifting the digits" can only be used if the exponent is 2 (or 4,8,16,etc.) since numbers are represented binary. Then it would be `1< – ipc Nov 24 '12 at 11:33

2 Answers2

9

No. Scientific notation is only for constant values. Those values are determined at compile time, while the value you want to get is determined at runtime.

You'll have to use something like int result = pow(10,n). Keep in mind that std::pow returns double values.

Zeta
  • 103,620
  • 13
  • 194
  • 236
0

The closest you can do is define a macro in the following way:

You cannot avoid using pow since n is evaluated at run time. This just like C works.

#define e(n) *pow(10,n)

And you use it:

int n; cin >> n; int x = 1 e(n);

Sebastian Cabot
  • 1,812
  • 14
  • 18