+ Reply to Thread
Results 1 to 4 of 4

Need to be able to raise a negative number to a non-integer power

  1. #1
    Registered User
    Join Date
    10-23-2017
    Location
    Boston, MA
    MS-Off Ver
    Excel 365
    Posts
    73

    Need to be able to raise a negative number to a non-integer power

    Windows Calculator app has no difficulty calculating (0.438 * -16) ^ 2.2. But VBA says NO WAY. After googling, it appears that VBA requires when using the syntax a ^ b, if a is negative, b must be an integer.

    As a workaround, I tried the Excel Power function. However, when I tried Application.WorksheetFunction.Power(0.438 * -16, 2.2), it gave the error message that it was unable to find the Power property of the WorksheetFunction class, despite that when I was typing it out it offered it up as a recognized function.

    Also, obviously one of these values is actually a variable (the -16 part), I'm just using the literal values for the last value that caused a problem to create this example.

    Any suggestions?

  2. #2
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,238

    Re: Need to be able to raise a negative number to a non-integer power

    The POWER sheet function is one of the few functions that cannot be used in VBA by referring to it through the WorksheetFunction collection because it has its counterpart in VBA function. This is one of the exceptions.
    Using the formula
    Please Login or Register  to view this content.
    in the sheet also reports an error.
    However, if you want to calculate very much the same as Windows Calculator then you can use absolute value:
    Please Login or Register  to view this content.
    Artik

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,607

    Re: Need to be able to raise a negative number to a non-integer power

    I can't answer your question definitively. But maybe I can add some rationale. First, VBA won't be able to do anything that the worksheet function and/or symbol can't do. That is, POWER and/or "^".

    I would guess that Excel can cope with raising a negative number to an integer power because it can determine the sign. And it can raise a positive number to non-integer power because the result will always be positive.

    Presumably, it can't calculate partial powers of negative numbers.

    Can't even suggest a workaround. Even if you take out the -1, you can't raise that to the power of 2.2.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,858

    Re: Need to be able to raise a negative number to a non-integer power

    Part of the problem here is that exponentiation with a negative base and a rational exponent is somewhat challenging mathematically. Irrational exponents are even worse, if that is possible. When I approach these kinds of problems, I usually try to be clear exactly what mathematical situation I am working with and exactly what solution I expect from the problem. You might review the Wikipedia article for exponentiation for rational and real exponents: https://en.wikipedia.org/wiki/Expone...onal_exponents

    You only give one example. How representative is this example? 2.2 (assuming this is 2.2000000000....) is a rational number (11/5), so can we assume that you will always be working with rational numbers for the exponent? Does your sheet include the numerator and denominator values somewhere, or will we need to come up with a formula to convert the decimal number into numerator and denominator? Or is the exponent potentially irrational, so that we cannot assume a rational exponent?

    When I program this sort of thing in the spreadsheet, I end up choosing between different strategies:

    1) The IMPOWER() function can usually calculate these without erroring. However, it also tends to return a complex number when the base is negative. Which is sometimes just fine when you only need an answer, but not very useful if you want the real solution and not a complex solution.

    2) When I know there is a solution (because I know that the denominator of a rational exponent will always be odd), sometimes I will simplify the problem down to something like =SIGN(value)*POWER(ABS(value),exponent) or something similar. Obviously, this is not a generic solution, but it can work pretty well when I know the exponent will be rational and the denominator will be odd.

    3) When I need more generic solutions, I often end up developing something based off of Euler's formula https://www.math.uvic.ca/~ibrahims/m...ex_numbers.pdf This often means acknowledging that there are multiple possible solutions and trying multiple solutions until I find the one I want.

    I hesitate to provide a final solution, because I am not sure what kind of problem(s) you will need to solve based on only one example. Usually I end up working with one of those approaches, depending on what I know about the problems I expect to encounter.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Sub-Forum for Excel Power Tools (Power Query, Power Pivot & Power BI)
    By chullan88 in forum Suggestions for Improvement
    Replies: 10
    Last Post: 06-28-2018, 02:25 PM
  2. Decimal power of a negative value
    By hinubhai in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-06-2014, 01:27 PM
  3. Negative Power Square Roots
    By stevehk in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-10-2013, 08:01 PM
  4. Count the number of occurrences of an integer withing a larger integer
    By nnktran in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-26-2010, 01:04 PM
  5. How to raise power to a formula along column
    By iamsevenup in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 03-06-2009, 04:09 AM
  6. Replies: 1
    Last Post: 11-20-2008, 01:52 AM
  7. 2003= negative number&2004= negative number How Do I Calculate gro
    By Jason in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-14-2005, 02:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1