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

  • SEARCH
  • Home
  • 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 801951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:31:21+00:00 2026-05-14T23:31:21+00:00

I was always wondering how I can make a function which calculates the power

  • 0

I was always wondering how I can make a function which calculates the power (e.g. 23) myself. In most languages these are included in the standard library, mostly as pow(double x, double y), but how can I write it myself?

I was thinking about for loops, but it think my brain got in a loop (when I wanted to do a power with a non-integer exponent, like 54.5 or negatives 2-21) and I went crazy 😉

So, how can I write a function which calculates the power of a real number? Thanks


Oh, maybe important to note: I cannot use functions which use powers (e.g. exp), which would make this ultimately useless.

  • 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-05-14T23:31:22+00:00Added an answer on May 14, 2026 at 11:31 pm

    Negative powers are not a problem, they’re just the inverse (1/x) of the positive power.

    Floating point powers are just a little bit more complicated; as you know a fractional power is equivalent to a root (e.g. x^(1/2) == sqrt(x)) and you also know that multiplying powers with the same base is equivalent to add their exponents.

    With all the above, you can:

    • Decompose the exponent in a integer part and a rational part.
    • Calculate the integer power with a loop (you can optimise it decomposing in factors and reusing partial calculations).
    • Calculate the root with any algorithm you like (any iterative approximation like bisection or Newton method could work).
    • Multiply the result.
    • If the exponent was negative, apply the inverse.

    Example:

    2^(-3.5) = (2^3 * 2^(1/2)))^-1 = 1 / (2*2*2 * sqrt(2))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm always wondering who should do it. In Ruby , we have the Daemons
I was always wondering if there is operator for deleting multi dimensional arrays in
I was always wondering why it is a big deal having version control support
I was always wondering why such a simple and basic operation like swapping the
I was always wondering about this seemingly utopic world of open source. Assuming the
I've been a Delphi (D7) developer for many sometime already, I've always been wondering
I always use unix timestamps for everything, but am wondering if there is a
The comment to this answer got me wondering. I've always thought that C was
very quick one, I was always wondering, do internet browsers use for rendering OS
Wondering if I should ALWAYS use the respond_to/format.xxx block in ALL of my actions

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.