X^2 to Math.pow(x,2)

I am writing a program that takes an argument equation and calculates a Riemann sum. I've got the bulk of the coding written. I have no problem converting any type of trig functions in the argument to their proper string representations of the java.lang.Math class and method, ie if user enters sin(x)/cos(x), the program converts it to a string of Math.sin(x)/Math.cos(x). However, when I try to convert x^2 (x squared) to Math.pow(x,2), the computer just leaves it as it is. This is the code i've tried so far:
args[0] = sin(x)/cos(x)  // user input at command line
args[0].replaceAll("sin","Math\u002Esin");
args[0].replaceAll("cos","Math\u002Esin");However, when I try the same with x^2 as input, even with the unicode instead of a carat, the output is still just x^2. Any idea how I can get a string of x^2 converted to Math.pow(x,2) ?

then you're going to need a regular expression
something
like.replaceAll("(.*)x\\^([0-9]).*(.*)", "\\1
Math.pow(x, \\2) \\3")
Solved the problem with
for(int i = 0; i < 10000; i++){
   s = s.replaceAll(String.format("x\\^"+i),String.format("Math\u002Epow("+i+")") );
}covers integer exponents up to 10000, and barely noticeable increase in calculation time. thanks to all for the help!

Similar Messages

  • Math power function in script logic

    Hello all!
    Can anybody help me how  I can do math "power" function in BPC 5.1 Script Logic?
    Thanks

    Let's say you have a source account FOO and you want to calculate FOO to the 5th power, posting the result to FOOFIVE.
    In BPC SQL scripting, there are no mathematical functions to play with (powers, roots, sine, cosine, etc.) except for add, subtract, multiply and divide -- plus you can round results in your Factor or Expression. So that means, we must use multiplication.
    You could certainly do this using a bunch of intermediate accounts, one each for foo squared, foo cubed, foo to the 4th, and foo to the 5th. That would require *GO in between each *WHEN/REC block, and could be slow.
    I haven't tried this, but I think this should work, all in one statement:
    *XDIM_ACCOUNT = FOO
    *WHEN *
    *IS *
    REC(Factor=GET(Account="FOO")GET(Account="FOO")GET(Account="FOO")GET(Account="FOO")*GET(Account="FOO"),Account=FOOFIVE)
    *ENDWHEN
    If you want FOO to the 50th power, copy & paste.
    If you want square root of FOO, that could be a real challenge. I think to use straight BPC SQL logic it wouldn't be possible. I would probably write a stored procedure using SQL's SQRT(), and call that from the BPC logic file. Likewise for any other geometric functions, complex algebra, etc.
    There also may be a way to calculate powers in MDX -- that's more mathematically straightforward -- but as a BPC on MS person, I really never ever use MDX due to performance.
    Regards,
    Tim

  • Is using Math.pow faster than multiplying myself?

    Hi all,
    Is it faster to use Math.pow to calculate a power than to, say, multiply it myself in a loop? How about if I'm just doing a square -- is it faster to use Math.pow(x,2) than x * x?
    I noticed that the pow method in StrictMath has the keyword "native." Does this mean it's being done by the operating system? Does it mean it's being done directly in the hardware?
    Thanks!
    Tim

    TimQuinn wrote:
    This is in an inner-loop deep in a simulation, so I'd like it to be as fast as possible. From the above, I'm gathering that I should keep using pow for my ^8 calculation, but substitute a simple multiplication for my ^2 calculation.Not necessarily. Remember that N^8 = ((N^2)^2)^2. The Math.pow algorithm will very likely use this optimization, but you can too.
    Related question: does it make any difference at all in these kinds of operations if I use floats or doubles? In terms of speed, I mean, not precision.Actual answer to all of your questions: try all of the possibilities in your simulation and see what actually happens.

  • How do you calculate a (base * power) sans math.pow?

    I've been tasked with trying to figure out how to do an exponent question without using the math.pow(3,2) method. How would I do that?

    Yeah it's a non negative number but I don't
    understand how a loop would complete this.Say you want to find the value of 10^5, you just do:
    int product = 1;
    product *= 10; // this is the same as product = product  * 10
    product *= 10;
    product *= 10;
    product *= 10;
    product *= 10; Use a for statement for this:
    http://java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html

  • Math.pow versus multiply for squaring

    Does anyone know which is faster for squaring doubles? Using math.pow to raise the value to the power of two, or to simply multiply the value by itself.
    thanks.
    -Brian

    ^ As was my ill-advised original response to the @OP. :D

  • Taking artibitary roots with Math.pow

    Greetings,
    I am a high school math teacher, implementing a Java version of the testing calculator used in state standardized tests. I'm hoping it will help teachers demonstrate certain features of the (not very user friendly) calculator to students.
    I'm having a problem taking arbitrary roots. The calculator allows you to take the nth root of any number. At first glance, the solution is obvious
    If I want to take the nth root of x,
    Math.pow(x,1/n)
    And this works fine so long as x is postive. If x becomes negative, however, as in
    Math.pow(-27,1/3.0)
    pow bails out, even though the answer is -3.
    Any advice? I could use an if to catch x being negative and flip it (flipping it back later), but that introduces other issues. I would just be better if there was another function that worked, or if I am missing something silly.
    All help would be appreciated.

    Math.pow(-27,1/3.0)
    pow bails out, even though the answer is -3.This behavior is documented.
    If the first argument is finite and less than zero ...
    if the second argument is finite and not an integer, then the result is NaN.
    Brynjar's advice looks good to me.
    db

  • Forcing server VM not to optimise Math.pow ?

    Deep in my system I have a set of libraries for which I don't have the source and which I have been unsuccessful in decompiling. The problem is that these libraries call Math.pow and Math.exp, which are defined in terms of allowed uncertainity in the results.
    Unfortunately, my application requires strict reproducibility in its results, so this is a little irritating :( Is there any way to force the server VM (I'm using JDK 1.5.0_03 under RH Linux FC3) not to take advantage of its optimisations for the java.lang.Math class? I would like it to optimise everything else that it can, but not the operations that are defined in terms of ulp (Math.pow, Math.exp, Math.sin, etc.).
    As I mentioned, it is not possible for me to replace the calls to java.lang.Math with calls to java.lang.StrictMath (although I have done this where it is possible). I guess I'm looking for a VM flag that will allow me to do this.
    Thanks in advance,
    R

    I haven't worked in this area, but was curious of the answer myself....
    This is an old reference, but I think it's still valid...
    Do a search for "strictfp".
    At quick glance, it looks like you can prefix your class or interface declaration with "strictfp" and it uses the strict math version.
    Eg.
    class strictfp MyClass
      // blah blah...
    }http://java.sun.com/developer/JDCTechTips/2001/tt0410.html#using
    regards,
    Owen

  • Decimal power without using Math.pow()

    how do you raise a decimal number (3.543) to a decimal power (4.235) without using Math.pow()?

    Use Math.log() and Math..exp().
    Math.exp(4.235*Math.log(3.543))

  • Confuzed about Math.pow

    Hello all its me again -_-
    I have a simple question, I am trying to get an algorithm to function like this :
    15000 ( 0.07/ 12 )
    -36
    (1 - ( 1 + 0.07 / 12 ) )
    where the -36 is the -36th power
    I have tried this:
    answer = (15000 * (.07 / 12)) / (1 - ( 1 + 0.07 / 12 ) * Math.pow(-36));
    but alas i get this error message :
    exam5.java:14: pow(double,double) in java.lang.Math cannot be applied to (int)
    answer = (15000 * (.07 / 12)) / (1 - ( 1 + 0.07 / 12 ) * Math.pow(-36));
    I do not understand this because the answer is a double and i have to int data types in this problem O_0
    So the question is:
    How can I fix my algorithm to function like the one I posted at the beginning, or how would I go about writing an algorithm like that.
    your regards
    Drag

    Oops just noticed that when I posted the algorithm it was
    15000 ( 0.07/ 12 )
    36
    (1 - ( 1 + 0.07 / 12 ) )
    where the -36 is the -36th power
    it is supossed to be
    15000 ( 0.07/ 12 )
    -----------------------> -36
    (1 - ( 1 + 0.07 / 12 ) )
    where the -36 is the -36th power
    and it is between the closing two parentheses
    very sorry , that is probably why your suggestion did give me the answer I was looking for
    Your Regards,
    Drag

  • Math.pow

    Hi all, I've got a simple question:
    4 to the power of 29 is 288230376151711744.
    The following results in 288230376151711740 though.
    double t=Math.pow(4,29);
    Why? (Writing your own pow-method naturally solves the problem, but what's happening here?)

    For instance:
        public static void main(String[] args)
            BigInteger myBigInt = new BigInteger("4");
            BigInteger resultBigInt = myBigInt.pow(29);
            double resultDbl = resultBigInt.doubleValue();
            System.out.println("BigInteger Result:  " + resultBigInt.toString());
            System.out.println("Double Result:      " + String.valueOf(resultDbl));
        }

  • Math.pow(x,2) v.s x*x

    Hi everybody
    There is a consideration of which of below expressions will be faster:
    Math.pow(x,2) or
    x*x
    What's your opinion? and why?
    Thanks
    Message was edited by:
    pupice

    What's your opinion? I like jello.and why?

  • Math.pow problem

    Hello guys, I trying to get math.pow of specific value but it returns me unusual ouput which i don't understand. Here is the formula
    Math.pow(3333.625, 2));
    Output:
    1.1113055640625E7
    when i used google to compute it here is the result
    Result:
    11 113 055.6
    I also notice the E7 in the end of the value. Can any help undertand what is it and how can i convert it into normal output like in google. Thank for your time.

    Use java.math.BigDecimal.
    Here's a sample
    import java.math.*;
    public class MathPow
         public static void main(String args[])
              //System.out.println( Math.pow(3333.625, 2) );
              BigDecimal bd = new BigDecimal(Math.pow(3333.625, 2));
              System.out.println(bd.toString());
    }

  • Math.pow(Math.E, time)

    Hi folks!
    The expression
    "Math.pow( Math.E, time )" keeps giving me a "divide by zero" error.
    This seems to be similar to problems others have had, but the other solutions don't quite work in my case, since Math.E isn't negative.  "time" of course is a non-integer, but the expression doesn't even work for me if I replace it with "Math.ceil(time)." 
    Can anybody tell me what I'm missing?  Thanks in advance!
    --S

    Thanks for the reply!
    My original expression was
    thet = time;
    a = 2;
    b = 0.5;
    exp = Math.pow(Math.E, b*thet);
    posx = a*exp*Math.cos(thet);
    posy = a*exp*Math.sin(thet);
    [640, 640] + [posx, posy]
    except that a and b were slider values.  If I use Math.exp,
    thet = time;
    a = 2;
    b = 0.5;
    exp = Math.exp(b*thet);
    posx = a*exp*Math.cos(thet);
    posy = a*exp*Math.sin(thet);
    [640, 640] + [posx, posy]
    I get the same error.  I found a work-around by attaching theta to a slider instead, but I'm still curious to know what the problem was.
    Thanks again!
    --S

  • Math.pow in MIDP

    Is Math.pow supported in MIDP ?
    If not, what to use instead ?
    Thanks.

    Is Math.pow supported in MIDP ?{color:#000080}No.{color}
    If not, what to use instead ?{color:#000080}Roll your own, if you can't find anything on the net. I would start by examining the j2se source for the method, and if it doesn't involve calls to other methods I would adopt it.
    Just be sure whatever you write or adopt can run in the limited capability of a mobile device :-)
    db{color}

  • How do I use sqrt() and pow() math functions?

    I tried starting with import java.lang.Math.*; and using sqrt() and pow(), but I get an error response in compilation.
    //** 1/pi calculation program using Ramanujan's forumula **//
    //** by n=2 iteration, double precision value for 1/pi repeats **//
    import java.lang.Math.*;
    import java.io.*;
    public class CalcInversePiRamanujan {
      public static void main(String args[]) {
        double n=0, term, four_n_factorial=1, n_factorial=1, inverse_pi=0;
        inverse_pi = four_n_factorial * 1103 * 2 * sqrt(2) / 9801; //** initial value for n=0 **//
        System.out.println(inverse_pi);
        for (n=1, four_n_factorial=1, n_factorial=1; n<=3; n++) {
            four_n_factorial *= (4 * n);
            four_n_factorial *= (4*n-1);
            four_n_factorial *= (4*n-2);
            four_n_factorial *= (4*n-3);
            term = four_n_factorial;
            n_factorial *= n;
            term /= pow(n_factorial, 4);
            term *= 1103 + 26390 * n;
            term /= pow(396, 4 * n);
            term *= 2 * sqrt(2);
            term /= 9801;
            inverse_pi += term;
            System.out.println(n, term, inverse_pi);
        System.exit(0);
    }

    all methods in math class are static so you have to call them as
    Math.pow() and Math.sqrt() you dont have to import any additional packages as Math is a class available in java.lang package
    you have to change your
    System.out.println(n, term, inverse_pi); to
    System.out.println(n+ term+ inverse_pi);
    math class also provides the value of Pi you can access it using Math.PI

Maybe you are looking for

  • Just bought a mid 2012 13 in macbook pro. Battery life is only 3 hours. Thoughts?

    Just bought a mid 2012 13 in macbook pro. Battery life is only 3 hours. I was under the impression that it would last 7 hours. This is with normal internet browsing and text editing. my brightness is not all the up and bluetooth is off. Thoughts?

  • How To Save Form Data

    Hello experts, champions and everyone else [Ref: ApEx 3.0.1.00.07 && IE 6 sp2 ] I need to save a lot of data from HTML input elements that are generated/rendered using the APEX_ITEM API. eg TEXT, RADIOGROUP, LOV, HIDDEN etc How is it posible to pass

  • AS3 wont publish

    Hi, I am trying to modify a FLA file, which is already done. Problem I am haveing is when I publish the FLA file,into a SWF, the AS3 file wont publish . Am a newbie with this so not sure what I am doing wrong, Without the AS3 file, the movie just loo

  • NullPointerException Using PrintStream or PrintWriter

    Here is the code that I am using. I am taking the output of a program and trying to write that output to a file. When outputting to the file I receive a null pointer exception that I don't know why I am getting. I am using PrintStream here but I also

  • Nokia C1-02 Not identifying caller (CLI problem)

    Hi everyone, I have bought Nokia c1-02 v5.40 Some Numbers are saved in phonebook with country code +923xx and some are saved without country code 03xx. When some caller calls me, his name is not displayed (identified) but when this caller sends me sm