K
K
kate2019-01-20 22:44:43
C++ / C#
kate, 2019-01-20 22:44:43

Is the result correct?

Binary exponentiation algorithm for long numbers.

double binpow (double number, long int power)
{
  double res = 1;
  while (power)
    if (power & 1)
    {
      res *= number;
      --power;
    }
    else 
    {
      number *= number;
      power >>= 1;
    }
    std::cout << "Result: " << res;
  }

int main()
{
  double a;
  long int n;
  std::cout << "Input number: ";
  std::cin >> a;
  std::cout << "Input power: ";
  std::cin >> n;

  binpow (a, n);
}

The result is very strange, my calculator calculates these numbers without e, but here with e. I can’t figure out what’s wrong ..
5c44cfa212ab1113360443.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2019-01-20
@vt4a2h

Everything is correct. This is called exponential notation .
If you don't like this output format, you can use std::fixed: Predicting the next question, the precision can be adjusted with std::setprecision(n).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question