0

I am trying to write the following method in Java

double doublePow(double x, double pow); 

Any pointers? Since both the base and the exponent can be double this is quite tricky for me. Any help would be appreciated.

examples:

x = doublePow (4, 0.5) ; // x = 2.0
x = doublePow (8, 0.333333333) ; // x = 1.9999999986137069

do not want to use the library function

Thank you in advance for any help!

user6904265
  • 1,938
  • 1
  • 16
  • 21
  • 5
    Uh... `return Math.pow(x, pow);`? – Andy Turner Nov 16 '16 at 21:58
  • Thanks! Don't want to use the library function. – user7169931 Nov 16 '16 at 22:00
  • Help how? You haven't said anything about what you're stuck on, or provided any code. – azurefrog Nov 16 '16 at 22:00
  • `return Math.exp(pow * Math.log(x));`? Unless you use some library function somewhere, you're going to have to implement some real ballaching code (which is why the libraries exist). – Andy Turner Nov 16 '16 at 22:04
  • @AndyTurner How would you even write it yourself? Can you get direct access to the FPU from Java? Even `Math.pow()` just delegates to a native implementation. – azurefrog Nov 16 '16 at 22:06
  • 1
    "How would you even write it yourself?" I'm not even going to try. But you could do it using series expansions, for example. – Andy Turner Nov 16 '16 at 22:06
  • 1
    I would give up pretty quickly; this is not a practical method to implement by yourself if you don't have at least a week to devote to it and nothing else. – Louis Wasserman Nov 16 '16 at 22:08
  • Oh Yes! This is possible. Though, I am trying to do it without using any built in function/log/exp etc. But no luck yet.. – user7169931 Nov 16 '16 at 22:09
  • not using standard math libs will bring you to use numeric aproximations by implementing Taylor series or similar alg. such a waste of computational resources.... – ΦXocę 웃 Пepeúpa ツ Nov 16 '16 at 22:10
  • @LouisWasserman I have to agree to what you said! – user7169931 Nov 16 '16 at 22:14
  • Start here: http://stackoverflow.com/questions/4638473/how-to-powreal-real-in-x86 – bradimus Nov 16 '16 at 22:15
  • Doing this without any library functions would be horrific. I hate to think of the potential loss of precision you would get without having to represent everything as Strings as well. – d.j.brown Nov 16 '16 at 22:22
  • 3
    Well, [here](http://repo.or.cz/glibc.git/blob/HEAD:/sysdeps/ieee754/dbl-64/e_pow.c) is a raw C implementation. Good luck. – Louis Wasserman Nov 16 '16 at 22:22

0 Answers0