i am trying to find the power of a value.But the problem is my exponent is a fractional value.power function does not suppporting any datatype other than int.
BigDecimal fd_returns_at_time_of_replace=(BigDecimal.valueOf(capitalDiff).multiply((BigDecimal.valueOf((long)constant1+.09)).pow(temp)));
here temp is a fractional value.given below is the eror message i am getting.
The method pow(int) in the type BigDecimal is not applicable for the arguments (double)
please anybody help me to do this.
BigDecimal.pow() only takes an int. To see a cool example of writing BigDecimal.pow() that accepts a double, see this question How to do a fractional power on BigDecimal in Java?
Common Sense
Consider you want to raise the number x to the power y
If both are integers:
for(int i=0 ; i<y ; i++)
answer = answer * x;
Problems are only when y is a decimal!
So we first change y to the form of y = n + 1/d
How to do that:
n = floor of y
d = 1 / (y - n) << integer
Now x^y = x^n * x^1/d
x to the power n is simple using the usual method
x to the power 1/d is simply the d th root of x
Note: You can increase the precision of your function by reducing the error factor induced by makind d an integer. How! 1/d can be multiplied by powers of 10.
Related
I am trying to divide two integer and get a decimal number
I am keep getting 0 result when I divide 10/29
I would like to get 0.34
my code is :
private int totalCount_Games;
private int totalCount_Hints_Statistics;
double avgHints;
avgHints=totalCount_Hints_Statistics/totalCount_Games;
In Java, when you divide two integers, the result is another integer.
In your case, 10/29 will result in 0 as you mentioned. If you want to get the results of these in floating digits, then change the above two integers to float or double.
In that case, the result for the above calculation will be 0.34.
PS: This is really basic. You should do more research in the official java site for documentation on datatypes.
The result of a int division is another int, rounded.
Casting int to double (or float) in the expression part will make the division occurs on doubles instead of int, note that this is different from casting the result from the int division to double.
int i = 5, b = 10;
double result = ((double)i)/((double)b);
result is 0.5
The above code will result in 0.0 as int/int will always be a int which is further type casted into double thus output is 0.0
Use the below code, Using big decimal for Rounding
double totalCount_Games_double = totalCount_Games;
double totalCount_Hints_Statistics_double = totalCount_Hints_Statistics;
double value = totalCount_Hints_Statistics/totalCount_Games_double;
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(2, RoundingMode.HALF_UP);
i am trying to implement following equation but not showing exact value.
Suppose the value of data1 to data9 are:5,1,1,1,2,1,3,1,1 and the value of y will be 2.
but showing null.original equation link:https://www.researchgate.net/publication/303232034_Mathematical_Model_Development_to_Detect_Breast_Cancer_Using_Multigene_Genetic_Programming
double term3 =Math.cos(Math.log10(data7))/(data6+data7-6.419);
double y=0.05321*Math.log10(Math.exp(data2-1.0*Math.sin(Math.cos(data8))+Math.cos(data6)-(1.0*data6)/(data1*data3*data9*Math.sin(data8)*Math.tanh(data8))))-1.573*Math.sin(Math.sqrt(data4+data6+term3))
-0.07468*Math.sin(Math.exp(data2*data9))
-0.9181*Math.log10(2.0*data4+data6+Math.sin(Math.tanh(data7)+5.626*data4*data9)+Math.sin(Math.sin(Math.sin(data8)))
+Math.sin(Math.sin(2.0*data6-7.128))+(Math.cos(data4)/(data6+data7-6.649))+(Math.cos(data4)/(data6+data7-6.634))+(Math.cos(data4)/(data4*(2.0*data6-1.479))))
+0.2919*Math.sin(Math.sin(Math.cos(data2+data5+data8)/data2+data7-6.76))
-0.07468*Math.sin(Math.sqrt(2.0*data4+data7-1.0*Math.sqrt(data6)))
-0.07468*Math.sin(data6)-((5.381*Math.pow(10, 15)*Math.cos(Math.sqrt(data2*Math.pow(data3, 2))))/(7.206*Math.pow(10, 16)*data3+7.206*Math.pow(10, 16)*data5-4.934*Math.pow(10, 17)))
-((5.381*Math.pow(10, 15)*Math.cos(Math.sqrt(data1*data3*data4)))/(7.206*Math.pow(10, 16)*data5+Math.sqrt(Math.tanh(data7))*(data7-1.0*data4+7.206*Math.pow(10, 16)*Math.cos(data6))-4.836*Math.pow(10, 17)))
+((5.258*Math.pow(10, 15)*Math.cos(data4))/(1.801*Math.pow(10, 16)*data2+1.801*Math.pow(10, 16)*data7-1.2*Math.pow(10, 17)))
+(0.4014*Math.cos(data6+data7)+Math.sqrt(data7+5.446))/((data5+Math.log10(data1)-6.585)*(Math.log10(data7)-1.0*Math.tanh(data5)-5.646*data4*data9)
+Math.sqrt(Math.tanh(data8))*(data7-1.0*data4+Math.cos(data6))+data2*Math.pow(data4, 2)*Math.cos(data2)*Math.cos(data6))-0.003903*data1*Math.cos(Math.sqrt(data2+data4+data6-1.0*Math.sqrt(data4)))*(
3.057*data1+2.0*data2-1.0*data4+data6+data7+data9+Math.cos(6.478*data6)+Math.cos(data6)-1.0*Math.sqrt(data1*data3*data6)-6.419)+((0.2919*Math.cos(Math.sqrt(data1*data3*data6)))/(2.0*data6-7.218)*(data5+data7-6.76))+5.235;
the value of y must be either 2 or 4 .but it is not intering in the if else condition
int x= (int) y;
if(x==2 )
{
condition="B";
Toast.makeText(getApplicationContext(),"value of x is "+x,Toast.LENGTH_LONG).show();
}
else if(x==4)
{
condition="M";
Toast.makeText(getApplicationContext(),"value of x is "+x,Toast.LENGTH_LONG).show();
}
cann't understand where is the problem....help please...
Since y is a double, calculations are extremely unlikely to give exactly 2 or 4. What's probably happening is that y is a tiny bit smaller than 2 or 4 when the first condition fails.
Instead of casting y directly to an int, you could try using long x = Math.round(y);. Alternatively (and perhaps better), just check that Math.abs(y - 2) or Math.abs(y - 4) is smaller than some small threshold (e.g., .0001), so as to allow for possible rounding error.
I'm doing a math operation beetwen hours, but I've a problem with the float (the java virtual machine approximate is not perfect). So, I decided to convert this operation in BigDecimal...but I've some problems with results..
This is the original code:
public float ConvertTo100(float input)
{
float output = 0.0f;
int hh;
float mm;
hh = (int)input;
mm = input - hh;
output = hh + ((input - hh)/60) * 100;
return output;
}
This is my convertion, but doesn't works:
public float ConvertTo100(float input)
{
BigDecimal inputBD = new BigDecimal(Float.toString(input));
String inpString = String.valueOf(input);
String[] inpsplit = inpString.split("\\.");
BigDecimal hh = new BigDecimal(Float.toString(Integer.parseInt(inpsplit[0])));
BigDecimal output = hh.add((inputBD.subtract(hh).divide(BigDecimal.valueOf(60))).multiply(BigDecimal.valueOf(1000)));
return Float.parseFloat(String.valueOf(output));
}
Where is the problem?? Thank you so much!!! :)
Division by 60 cannot be done exactly in decimal for many inputs. To continue using BigDecimal, you will need to pick a scale and rounding mode for the division. Here's one way of doing it, but read the BigDecimal documentation to see all the options.
BigDecimal output = hh.add((inputBD.subtract(hh).divide(
BigDecimal.valueOf(60), 10, BigDecimal.ROUND_HALF_EVEN))
.multiply(BigDecimal.valueOf(1000)));
However, the main point of using BigDecimal is to get exact representation of decimal fractions. You are not getting that benefit, but are getting the messy code that results from doing arithmetic using method calls instead of infix operators. Unless you need a specific rounding mode, or some finite precision greater than 16.9 significant digits, you would be much better off using double.
I have been working on an Android project for awhile that displays the fundamental frequency of an input signal (to act as a tuner). I have successfully implemented the AudioRecord class and am getting data from it. However, I am having a hard time performing an FFT on this data to get the fundamental frequency of the input signal. I have been looking at the post here, and am using FFT in Java and Complex class to go with it.
I have successfully used the FFT function found in FFT in Java, but I am not sure if I am obtaining the correct results. For the magnitude of the FFT (sqrt[rere+imim]) I am getting values that start high, around 15000 Hz, and then slowly diminish to about 300 Hz. Doesn't seem right.
Also, as far as the raw data from the mic goes, the data seems fine, except that the first 50 values or so are always the number 3, unless I hit the tuning button again while still in the application and then I only get about 15. Is that normal?
Here is a bit of my code.
First of all, I convert the short data (obtained from the microphone) to a double using the following code which is from the post I have been looking at. This snippet of code I do not completely understand, but I think it works.
//Conversion from short to double
double[] micBufferData = new double[bufferSizeInBytes];//size may need to change
final int bytesPerSample = 2; // As it is 16bit PCM
final double amplification = 1.0; // choose a number as you like
for (int index = 0, floatIndex = 0; index < bufferSizeInBytes - bytesPerSample + 1; index += bytesPerSample, floatIndex++) {
double sample = 0;
for (int b = 0; b < bytesPerSample; b++) {
int v = audioData[index + b];
if (b < bytesPerSample - 1 || bytesPerSample == 1) {
v &= 0xFF;
}
sample += v << (b * 8);
}
double sample32 = amplification * (sample / 32768.0);
micBufferData[floatIndex] = sample32;
}
The code then continues as follows:
//Create Complex array for use in FFT
Complex[] fftTempArray = new Complex[bufferSizeInBytes];
for (int i=0; i<bufferSizeInBytes; i++)
{
fftTempArray[i] = new Complex(micBufferData[i], 0);
}
//Obtain array of FFT data
final Complex[] fftArray = FFT.fft(fftTempArray);
final Complex[] fftInverse = FFT.ifft(fftTempArray);
//Create an array of magnitude of fftArray
double[] magnitude = new double[fftArray.length];
for (int i=0; i<fftArray.length; i++){
magnitude[i]= fftArray[i].abs();
}
fft.setTextColor(Color.GREEN);
fft.setText("fftArray is "+ fftArray[500] +" and fftTempArray is "+fftTempArray[500] + " and fftInverse is "+fftInverse[500]+" and audioData is "+audioData[500]+ " and magnitude is "+ magnitude[1] + ", "+magnitude[500]+", "+magnitude[1000]+" Good job!");
for(int i = 2; i < samples; i++){
fft.append(" " + magnitude[i] + " Hz");
}
That last bit is just to check what values I am getting (and to keep me sane!). In the post referred to above, it talks about needing the sampling frequency and gives this code:
private double ComputeFrequency(int arrayIndex) {
return ((1.0 * sampleRate) / (1.0 * fftOutWindowSize)) * arrayIndex;
}
How do I implement this code? I don't realy understand where fftOutWindowSize and arrayIndex comes from?
Any help is greatly appreciated!
Dustin
Recently I'm working on a project which requires almost the same. Probably you don't need any help anymore but I will give my thoughts anyway. Maybe someone need this in the future.
I'm not sure whether the short to double function works, I don't understand that snippet of code neither. It is wrote for byte to double conversion.
In the code: "double[] micBufferData = new double[bufferSizeInBytes];" I think the size of micBufferData should be "bufferSizeInBytes / 2", since every sample takes two bytes and the size of micBufferData should be the sample number.
FFT algorithms do require a FFT window size, and it has to be a number which is the power of 2. However many algorithms can receive an arbitrary of number as input and it will do the rest. In the document of those algorithms should have the requirements of input. In your case, the size of the Complex array can be the input of FFT algorithms. And I don't really know the detail of the FFT algorithm but I think the inverse one is not needed.
To use the code you gave at last, you should firstly find the peak index in the sample array. I used double array as input instead of Complex, so in my case it is something like: double maxVal = -1;int maxIndex = -1;
for( int j=0; j < mFftSize / 2; ++j ) {
double v = fftResult[2*j] * fftResult[2*j] + fftResult[2*j+1] * fftResult[2*j+1];
if( v > maxVal ) {
maxVal = v;
maxIndex = j;
}
}
2*j is the real part and 2*j+1 is the imaginary part. maxIndex is the index of the peak magnitude you want (More detail here), and use it as input to the ComputeFrequency function. The return value is the frequency of the sample array you want.
Hopefully it can help someone.
You should pick an FFT window size depending on your time versus frequency resolution requirements, and not just use the audio buffer size when creating your FFT temp array.
The array index is your int i, as used in your magnitude[i] print statement.
The fundamental pitch frequency for music is often different from FFT peak magnitude, so you may want to research some pitch estimation algorithms.
I suspect that the strange results you're getting are because you might need to unpack the FFT. How this is done will depend on the library that you're using (see here for docs on how it's packed in GSL, for example). The packing may mean that the real and imaginary components are not in the positions in the array that you expect.
For your other questions about window size and resolution, if you're creating a tuner then I'd suggest trying a window size of about 20ms (eg 1024 samples at 44.1kHz). For a tuner you need quite high resolution, so you could try zero-padding by a factor of 8 or 16 which will give you a resolution of 3-6Hz.
I don't know why this is generating an ArrayOutOfBoundsException! It's likely I've overlooked something because I'm so tired but if someone could tell me what is wrong I'd appreciate it. Thanks.
// array declaration
public int CircleLeft[] = new int[mIPAWidth];
// this is my loop that generates the error:
for(px = 0; px <= mIPAWidth - 1; px++){
py =(int) m.sqrt(r^2 - (px-mIPAWidthHalf)^2) + mIPAHeightHalf;
Log.w(getClass().getName(), "mIPAWidth = " + mIPAWidth + ", index is: " + px);
CircleLeft[px] = py; }
Along with Luther Blissett's answer...
Are you modifying mIPAWidth between when the array is instantiated and when you begin the for loop? I would change your for conditional to something like this:
for(int px = 0; px < CircleLeft.length; px++)
That way you know the type of px and you know you're referencing the actual length of the CircleLeft array. It may not fix your error, but it's still a good idea.
What is the type of px? If it's a double, then you could suffer from a mis-compare with the <=(comparing for equality with double is almost never correct). Try the canonical loop for(px = 0; px < mIPAWidth ; px++) instead or better yet, make the loop index an integer.
Also, what's the type of mIPAWidth (for the same reasons)?