Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9211813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:22:57+00:00 2026-06-18T01:22:57+00:00

I was playing with an implementation of a power function: #include <iostream> using namespace

  • 0

I was playing with an implementation of a power function:

#include <iostream>
using namespace std;

int pow(int b, int e)
{
  int result;

  if (e == -1 && b != 0)
  {
    cout << "b = " << 1/b << endl;
    return 1/b;
  }

  else if (b != 0 && e != 0)
  {
    //int e_int(int (e));
    bool e_bool(e < 0);
    e = (e_bool*-e + !e_bool*e);
    result = b = pow(b, -e_bool)*(b*!e_bool + +e_bool);
    cout << endl << "\"result\" = " << result << " " << e << endl;

    for(int i = 1; i < e; i += 1)
    {
      result *= b;
    }
    return result;
  }

  else if (e != 0 && b == 0)
  {
    return 0;
  }

  else if (e == 0 && b != 0)
  {
    return 1;
  }

  else if (b == 1)
  {
    return 1;
  }

  else if (e == 1)
  {
    return b;
  }

  else
  {
    cout << endl << "Error";
    return -1;
  }
}

int main ()
{
  cout << endl << pow(-2, -1);

  return 0;
}

The output is:

b = 0

0

Why does b get set to 0?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:22:58+00:00Added an answer on June 18, 2026 at 1:22 am

    Integer division can only produce integer results. Your program causes this line:

    cout << "b = " << 1/b << endl;
    

    To run, and if b is any integer larger than 1, the result will be 0.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm playing around with Domain Driven Development. I'm using a generic repository implementation that
I've been playing with the ATI OpenCL implementation in their Stream 2.0 beta. The
I am playing with a basic implementation of the Query Object pattern (in quotes
I've been playing around with RB tree implementation in Haskell but having difficulty changing
I've been playing around a little with the Exocortex implementation of the FFT, but
I've been playing around with Bouncy Castle's implementation of RSA (Lightweight API) and got
I've been playing with blocks and encountered a weird behavior. This is the interface/implementation,
I have been playing with my own version of this, using 'if', and all
In my Silverlight application I'm using mostly XmlReader but I'm playing with idea to
For fun, I'm playing with a class to easily cache function results. The basic

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.