Skip to main content
Code Review

Return to Answer

typo
Source Link
Toby Speight
  • 87.3k
  • 14
  • 104
  • 322

#Improve the algorithm For large power, the loop is executed many times. We can use binary exponentation to reducedreduce that to log2 power iterations of the loop.

#Improve the algorithm For large power, the loop is executed many times. We can use binary exponentation to reduced that to log2 power iterations of the loop.

#Improve the algorithm For large power, the loop is executed many times. We can use binary exponentation to reduce that to log2 power iterations of the loop.

Remove duplication; not sure how that got in. Also, don't call function twice.
Source Link
Toby Speight
  • 87.3k
  • 14
  • 104
  • 322

#Modified code #include

double raiseToPower(double x, int power)
{
 if (power < 0) {
 return raiseToPower(1/x, -power);
 }
 double result = 1.0;
 double m = x;
 for (; power; power /= 2) {
 if (power % 2) {
 result *= m;
 }
 m *= m;
 }
 return result;
}
constexpr double floor0(double num)
{
 return int(num + 0.5);
}

#Consider full range of types raiseToPower() only works with non-negative exponents; it should either accept an unsigned type or be modified to work correctly with negative inputs. Perhaps like this:

 if (power < 0) {
 return raiseToPower(1/x, -power);
 }

#Improve the algorithm For large power, the loop is executed many times. We can use binary exponentation to reduced that to log2 power iterations of the loop.

#Avoid over-complication floor0() doesn't need that if/else; simply add 0.5 before truncating:

constexpr double floor0(double num)
{
 return int(num + 0.5);
}

It might be better to use long or long long there; in any case, you'll still suffer bugs when the value is too big for the integer type. std::floor() doesn't have that problem.

#Validate inputs If I enter a non-number, I don't get a clear error message. Instead, the program uses uninitialised values, which is Undefined Behaviour. Don't do that; instead check that std::cin is still good before using x or i.

#Avoid std::endl unless you need output flushing None of the uses of std::endl here are necessary, and we can use \n instead. (Remember that using std::cin flushes the output streams, and returning from main() also flushes outputs).


#Modified code #include

double raiseToPower(double x, int power)
{
 if (power < 0) {
 return raiseToPower(1/x, -power);
 }
 double result = 1.0;
 double m = x;
 for (; power; power /= 2) {
 if (power % 2) {
 result *= m;
 }
 m *= m;
 }
 return result;
}
constexpr double floor0(double num)
{
 return int(num + 0.5);
}

#Consider full range of types raiseToPower() only works with non-negative exponents; it should either accept an unsigned type or be modified to work correctly with negative inputs. Perhaps like this:

 if (power < 0) {
 return raiseToPower(1/x, -power);
 }

#Improve the algorithm For large power, the loop is executed many times. We can use binary exponentation to reduced that to log2 power iterations of the loop.

#Avoid over-complication floor0() doesn't need that if/else; simply add 0.5 before truncating:

constexpr double floor0(double num)
{
 return int(num + 0.5);
}

It might be better to use long or long long there; in any case, you'll still suffer bugs when the value is too big for the integer type. std::floor() doesn't have that problem.

#Validate inputs If I enter a non-number, I don't get a clear error message. Instead, the program uses uninitialised values, which is Undefined Behaviour. Don't do that; instead check that std::cin is still good before using x or i.

#Avoid std::endl unless you need output flushing None of the uses of std::endl here are necessary, and we can use \n instead. (Remember that using std::cin flushes the output streams, and returning from main() also flushes outputs).


#Modified code double raiseToPower(double x, int power) { if (power < 0) { return raiseToPower(1/x, -power); }

#include <iostream>

int main()
{
 double x;
 int i;
 std::cout << "Please enter the number\n";
 std::cin >> x;
 std::cout << "Please enter the integer power that "
 "you want this number raised to\n";
 std::cin >> i;
 if (!std::cin) {
 std::cerr << "Input format error\n";
 return 1;
 }
 auto const result = raiseToPower(x,i);
 std::cout << x << " raised to power " << i << " is equal to "
 << raiseToPower(x,i)result << '\n';
 std::cout << "The result rounded is "
 << floor0(raiseToPower(x,i)result) << '\n';
}

#Modified code #include

double raiseToPower(double x, int power)
{
 if (power < 0) {
 return raiseToPower(1/x, -power);
 }
 double result = 1.0;
 double m = x;
 for (; power; power /= 2) {
 if (power % 2) {
 result *= m;
 }
 m *= m;
 }
 return result;
}
constexpr double floor0(double num)
{
 return int(num + 0.5);
}

#Consider full range of types raiseToPower() only works with non-negative exponents; it should either accept an unsigned type or be modified to work correctly with negative inputs. Perhaps like this:

 if (power < 0) {
 return raiseToPower(1/x, -power);
 }

#Improve the algorithm For large power, the loop is executed many times. We can use binary exponentation to reduced that to log2 power iterations of the loop.

#Avoid over-complication floor0() doesn't need that if/else; simply add 0.5 before truncating:

constexpr double floor0(double num)
{
 return int(num + 0.5);
}

It might be better to use long or long long there; in any case, you'll still suffer bugs when the value is too big for the integer type. std::floor() doesn't have that problem.

#Validate inputs If I enter a non-number, I don't get a clear error message. Instead, the program uses uninitialised values, which is Undefined Behaviour. Don't do that; instead check that std::cin is still good before using x or i.

#Avoid std::endl unless you need output flushing None of the uses of std::endl here are necessary, and we can use \n instead. (Remember that using std::cin flushes the output streams, and returning from main() also flushes outputs).


#Modified code #include

double raiseToPower(double x, int power)
{
 if (power < 0) {
 return raiseToPower(1/x, -power);
 }
 double result = 1.0;
 double m = x;
 for (; power; power /= 2) {
 if (power % 2) {
 result *= m;
 }
 m *= m;
 }
 return result;
}
constexpr double floor0(double num)
{
 return int(num + 0.5);
}

#Consider full range of types raiseToPower() only works with non-negative exponents; it should either accept an unsigned type or be modified to work correctly with negative inputs. Perhaps like this:

 if (power < 0) {
 return raiseToPower(1/x, -power);
 }

#Improve the algorithm For large power, the loop is executed many times. We can use binary exponentation to reduced that to log2 power iterations of the loop.

#Avoid over-complication floor0() doesn't need that if/else; simply add 0.5 before truncating:

constexpr double floor0(double num)
{
 return int(num + 0.5);
}

It might be better to use long or long long there; in any case, you'll still suffer bugs when the value is too big for the integer type. std::floor() doesn't have that problem.

#Validate inputs If I enter a non-number, I don't get a clear error message. Instead, the program uses uninitialised values, which is Undefined Behaviour. Don't do that; instead check that std::cin is still good before using x or i.

#Avoid std::endl unless you need output flushing None of the uses of std::endl here are necessary, and we can use \n instead. (Remember that using std::cin flushes the output streams, and returning from main() also flushes outputs).


#Modified code double raiseToPower(double x, int power) { if (power < 0) { return raiseToPower(1/x, -power); }

#include <iostream>
int main()
{
 double x;
 int i;
 std::cout << "Please enter the number\n";
 std::cin >> x;
 std::cout << "Please enter the integer power that "
 "you want this number raised to\n";
 std::cin >> i;
 if (!std::cin) {
 std::cerr << "Input format error\n";
 return 1;
 }
 std::cout << x << " raised to power " << i << " is equal to "
 << raiseToPower(x,i) << '\n';
 std::cout << "The result rounded is "
 << floor0(raiseToPower(x,i)) << '\n';
}

#Modified code double raiseToPower(double x, int power) { if (power < 0) { return raiseToPower(1/x, -power); }

#include <iostream>

int main()
{
 double x;
 int i;
 std::cout << "Please enter the number\n";
 std::cin >> x;
 std::cout << "Please enter the integer power that "
 "you want this number raised to\n";
 std::cin >> i;
 if (!std::cin) {
 std::cerr << "Input format error\n";
 return 1;
 }
 auto const result = raiseToPower(x,i);
 std::cout << x << " raised to power " << i << " is equal to "
 << result << '\n';
 std::cout << "The result rounded is "
 << floor0(result) << '\n';
}
Reduce line length
Source Link
Toby Speight
  • 87.3k
  • 14
  • 104
  • 322
double raiseToPower(double x, int power)
{
 if (power < 0) {
 return raiseToPower(1/x, -power);
 }
 double result = 1.0;
 double m = x;
 for (; power; power /= 2) {
 if (power % 2) {
 result *= m;
 }
 m *= m;
 }
 return result;
}
constexpr double floor0(double num)
{
 return int(num + 0.5);
}

#Consider full range of types raiseToPower() only works with non-negative exponents; it should either accept an unsigned type or be modified to work correctly with negative inputs. Perhaps like this:

 if (power < 0) {
 return raiseToPower(1/x, -power);
 }

#Improve the algorithm For large power, the loop is executed many times. We can use binary exponentation to reduced that to log2 power iterations of the loop.

#Avoid over-complication floor0() doesn't need that if/else; simply add 0.5 before truncating:

constexpr double floor0(double num)
{
 return int(num + 0.5);
}

It might be better to use long or long long there; in any case, you'll still suffer bugs when the value is too big for the integer type. std::floor() doesn't have that problem.

#Validate inputs If I enter a non-number, I don't get a clear error message. Instead, the program uses uninitialised values, which is Undefined Behaviour. Don't do that; instead check that std::cin is still good before using x or i.

#Avoid std::endl unless you need output flushing None of the uses of std::endl here are necessary, and we can use \n instead. (Remember that using std::cin flushes the output streams, and returning from main() also flushes outputs).


#Modified code #include

double raiseToPower(double x, int power)
{
 if (power < 0) {
 return raiseToPower(1/x, -power);
 }
 double result = 1.0;
 double m = x;
 for (; power; power /= 2) {
 if (power % 2) {
 result *= m;
 }
 m *= m;
 }
 return result;
}
constexpr double floor0(double num)
{
 return int(num + 0.5);
}

#Consider full range of types raiseToPower() only works with non-negative exponents; it should either accept an unsigned type or be modified to work correctly with negative inputs. Perhaps like this:

 if (power < 0) {
 return raiseToPower(1/x, -power);
 }

#Improve the algorithm For large power, the loop is executed many times. We can use binary exponentation to reduced that to log2 power iterations of the loop.

#Avoid over-complication floor0() doesn't need that if/else; simply add 0.5 before truncating:

constexpr double floor0(double num)
{
 return int(num + 0.5);
}

It might be better to use long or long long there; in any case, you'll still suffer bugs when the value is too big for the integer type. std::floor() doesn't have that problem.

#Validate inputs If I enter a non-number, I don't get a clear error message. Instead, the program uses uninitialised values, which is Undefined Behaviour. Don't do that; instead check that std::cin is still good before using x or i.

#Avoid std::endl unless you need output flushing None of the uses of std::endl here are necessary, and we can use \n instead. (Remember that using std::cin flushes the output streams, and returning from main() also flushes outputs).


#Modified code double raiseToPower(double x, int power) { if (power < 0) { return raiseToPower(1/x, -power); }

 double result = 1.0;
 double m = x;
 for (; power; power /= 2) {
 if (power % 2) {
 result *= m;
 }
 m *= m;
 }
 return result;
}
constexpr double floor0(double num)
{
 return int(num + 0.5);
}
#include <iostream>
int main()
{
 double x;
 int i;
 std::cout << "Please enter the number\n";
 std::cin >> x;
 std::cout << "Please enter the integer power that you"
  "you want this number raised to\n";
 std::cin >> i;
 if (!std::cin) {
 std::cerr << "Input format error\n";
 return 1;
 }
 std::cout << x << " raised to power " << i << " is equal to " << raiseToPower(x,i) << '\n';
 std::cout << "The result rounded is " << << floor0(raiseToPower(x,i)) << '\n';
}
double raiseToPower(double x, int power)
{
 if (power < 0) {
 return raiseToPower(1/x, -power);
 }
 double result = 1.0;
 double m = x;
 for (; power; power /= 2) {
 if (power % 2) {
 result *= m;
 }
 m *= m;
 }
 return result;
}
constexpr double floor0(double num)
{
 return int(num + 0.5);
}
int main()
{
 double x;
 int i;
 std::cout << "Please enter the number\n";
 std::cin >> x;
 std::cout << "Please enter the integer power that you want this number raised to\n";
 std::cin >> i;
 if (!std::cin) {
 std::cerr << "Input format error\n";
 return 1;
 }
 std::cout << x << " raised to power " << i << " is equal to " << raiseToPower(x,i) << '\n';
 std::cout << "The result rounded is " << floor0(raiseToPower(x,i)) << '\n';
}
double raiseToPower(double x, int power)
{
 if (power < 0) {
 return raiseToPower(1/x, -power);
 }
 double result = 1.0;
 double m = x;
 for (; power; power /= 2) {
 if (power % 2) {
 result *= m;
 }
 m *= m;
 }
 return result;
}
constexpr double floor0(double num)
{
 return int(num + 0.5);
}

#Consider full range of types raiseToPower() only works with non-negative exponents; it should either accept an unsigned type or be modified to work correctly with negative inputs. Perhaps like this:

 if (power < 0) {
 return raiseToPower(1/x, -power);
 }

#Improve the algorithm For large power, the loop is executed many times. We can use binary exponentation to reduced that to log2 power iterations of the loop.

#Avoid over-complication floor0() doesn't need that if/else; simply add 0.5 before truncating:

constexpr double floor0(double num)
{
 return int(num + 0.5);
}

It might be better to use long or long long there; in any case, you'll still suffer bugs when the value is too big for the integer type. std::floor() doesn't have that problem.

#Validate inputs If I enter a non-number, I don't get a clear error message. Instead, the program uses uninitialised values, which is Undefined Behaviour. Don't do that; instead check that std::cin is still good before using x or i.

#Avoid std::endl unless you need output flushing None of the uses of std::endl here are necessary, and we can use \n instead. (Remember that using std::cin flushes the output streams, and returning from main() also flushes outputs).


#Modified code #include

double raiseToPower(double x, int power)
{
 if (power < 0) {
 return raiseToPower(1/x, -power);
 }
 double result = 1.0;
 double m = x;
 for (; power; power /= 2) {
 if (power % 2) {
 result *= m;
 }
 m *= m;
 }
 return result;
}
constexpr double floor0(double num)
{
 return int(num + 0.5);
}

#Consider full range of types raiseToPower() only works with non-negative exponents; it should either accept an unsigned type or be modified to work correctly with negative inputs. Perhaps like this:

 if (power < 0) {
 return raiseToPower(1/x, -power);
 }

#Improve the algorithm For large power, the loop is executed many times. We can use binary exponentation to reduced that to log2 power iterations of the loop.

#Avoid over-complication floor0() doesn't need that if/else; simply add 0.5 before truncating:

constexpr double floor0(double num)
{
 return int(num + 0.5);
}

It might be better to use long or long long there; in any case, you'll still suffer bugs when the value is too big for the integer type. std::floor() doesn't have that problem.

#Validate inputs If I enter a non-number, I don't get a clear error message. Instead, the program uses uninitialised values, which is Undefined Behaviour. Don't do that; instead check that std::cin is still good before using x or i.

#Avoid std::endl unless you need output flushing None of the uses of std::endl here are necessary, and we can use \n instead. (Remember that using std::cin flushes the output streams, and returning from main() also flushes outputs).


#Modified code double raiseToPower(double x, int power) { if (power < 0) { return raiseToPower(1/x, -power); }

 double result = 1.0;
 double m = x;
 for (; power; power /= 2) {
 if (power % 2) {
 result *= m;
 }
 m *= m;
 }
 return result;
}
constexpr double floor0(double num)
{
 return int(num + 0.5);
}
#include <iostream>
int main()
{
 double x;
 int i;
 std::cout << "Please enter the number\n";
 std::cin >> x;
 std::cout << "Please enter the integer power that "
  "you want this number raised to\n";
 std::cin >> i;
 if (!std::cin) {
 std::cerr << "Input format error\n";
 return 1;
 }
 std::cout << x << " raised to power " << i << " is equal to " << raiseToPower(x,i) << '\n';
 std::cout << "The result rounded is " << floor0(raiseToPower(x,i)) << '\n';
}
Source Link
Toby Speight
  • 87.3k
  • 14
  • 104
  • 322
Loading
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /