Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Math.pow() function

Tags:

java

math

I'm trying to make a program in Java to calculate the formula for the Ricker wavelet:

enter image description here

But the results are not matching to the real ones.

This is what I'm using:

private static double rickerWavelet(double t, double f){

   double p = (Math.pow(Math.PI, 2))*(Math.pow(f, 2))*(Math.pow(t, 2));

   double lnRicker = 1 - (2 * p) * Math.exp(-p);

   return lnRicker;
}

Am I using the Math functions wrongly?

like image 917
Rikkin Avatar asked Oct 17 '13 12:10

Rikkin


People also ask

What is pow () function?

The pow() function returns the value of x to the power of y (xy). If a third parameter is present, it returns x to the power of y, modulus z.

What does POW () do in C++?

pow() is function to get the power of a number, but we have to use #include<math. h> in c/c++ to use that pow() function. then two numbers are passed. Example – pow(4 , 2); Then we will get the result as 4^2, which is 16.

What is the use of POW () in PHP?

The pow() function in PHP is used to calculate a base raised to the power of exponent. It is a generic function which can be used with number raised to any value.It takes two parameters which are the base and exponent and returns the desired answer.


1 Answers

To match the formula,

double lnRicker = 1 - (2 * p) * Math.exp(-p);

needs to be

double lnRicker = (1 - (2 * p)) * Math.exp(-p);

Since * has higher operator precedence than -, in your expression the multiplication of (2 * p) with Math.exp(-p) will be done first, which is not what you want.

like image 117
AakashM Avatar answered Sep 17 '22 21:09

AakashM