error while using math.pow in java code - java

i have a java code for finding out netsalary . I keep Getting "bad operand type for binary operator ' / '" error . The Line goes like this
netSalary = Double.parseDouble(principle2*rate2/12*Math.pow(rate2/12+1))/(Double.parseDouble(Math.pow(rate2/12+1)-1));
Could this be solved . Thanks in advance!

It should be commas you use, not slashes.
Math.pow(rate2/12+1) should syntactically be in the form of Math.pow(x,y) where both x and y are doubles. The first arg is the base, and second arg is the index you're raising it to. As the comment below mentioned, it's difficult to understand what you're trying to achieve, and you'll have to substitute x and y for the correct values - make sure they're of type double (you cannot use, for example, 12+1 as a parameter because it is an integer). If it is an integer, then type cast it using (double) in front of the value.
You also do not need to parseDouble everywhere since Math.pow will return double values anyways; it is redundant.
I agree with the comment above; please read the javadocs for any problems you're having with a method before posting here.

You mistake in using power function your power function should be like this
Math.pow((rate2/12+1),1)

Related

java.math.BigDecimal.scale() equivalent for double

I've got a matrix of values like the one below that I need to scale. I've been looking around for an inbuilt function if there is one that could do this for me. I haven't found one & so have ended up writing code to do the scaling using the below formula
scaledMatrix = (Matrix - MeanMatrix)/Standard Deviation
This code is a bit buggy & I'm working on correcting it. While I do that, I happened to bump on java.math.BigDecimal.scale() & did look up an equivalent for double as the matrix I have is double type numbers
If someone could please help me with details on
1) If there is an inbuilt function that accepts matrix of values & returns me the scaled matrix
2) `java.math.BigDecimal.scale()` equivalent for `double` type data
Any help would be much appreciated please.
The BigDecimal.scale() method does not do what you seem to think it is doing. A BigDecimal value is stored as a * 10^b (where ^ denotes exponentiation). The BigDecimal.scale() method basically returns the b part of that.
I do not know of a similar method for double values, nor do I know of a method which performs the function you need. Since you put apache-commons in the tags, I suggest you look into Apache Commons's extensive statistical library.

Changing Doubles To Ints

Im working on a homework assignment for my intro to computer science class and we are are inputting basic commands to get the percentage of people who drink a certain kind of energy drink. We used JOptionPane to make text boxes and you can input the amount of people and the computer has a set percentage to get the output. My problem is i set up my variables as doubles and my answers are very long decimals. I want to convert the answers to Ints so i can get whole numbers. I have tried to do this through casting but i keep getting the error message" EnergyDrink.java:14: error: variable citrusEnergyDrinkers might not have been initialized". What can i do?
This can't be solved without code. The error is not due to any problem with the conversion, but simply as the compiler-error says:
variable citrusEnergyDrinkers might not have been initialized
This means that the variable might not hold a value at the time you attempt to convert it, which results in undefined behaviour, which java-designers didn't allow for a reason.
The problem is as the error-message tells: citrusEnergyDrinkers gets its value inside some try-catch-block or a block that is only run under certain conditions, like if. One way to work around this would be to simply initialize citrusEnergyDrinkers as 0:
double citrusEnergyDrinkers = 0;.
Note though that this might produce incorrect results depending upon what happens when the value isn't set in case the above mentioned block of code isn't entered/breaks off before setting a value.
For the conversion:
Math.round(citrusEnergyDrinkers) is most likely preferable to a simple cast to int, since double most of the time has some imprecision due to the way it's stored in memory and round will actually round the value, while a cast will simply remove the frictional part. For example:
(int) 0.75 //produces 0
Math.round(0.75) //produces 1
You could multiply the double by 100 and then cast to an int:
double d = .77583495;
int perc = (int) Math.round( d );
I prefer to not cast like that, but it works.
Good luck.

I am running JAVA. When I try to create this method i get errors. The IDE told me to use an array I did and it still didnt work

I created the method below but it has red lines under it in netBeans. the IDE told me to use an array and that still didnt work so i went back to this.
private double getPaymentAmount(double loanValue, double paymentAmount, double numOfPayments, double periodInterestRate){
paymentAmount = loanValue [periodInterestRate(1+periodInterestRate)^numOfPayments]/[(1+periodInterestRate)^numOfPayments-1];
return paymentAmount;
You're confusing Java syntax with mathematical notation. While you might try to multiply variables as someVar(someVar2+someVar3) that's actually a method call. Additionally, square brackets have special meaning, and ^ is XOR and not power (use Math.pow instead).
loanValue *
(periodInterestRate * Math.pow(1+periodInterestRate, numOfPayments)) /
(Math.pow(1+periodInterestRate, numOfPayments)-1);
The code above has been revised to be syntactically valid. I've also taken the liberty of splitting it over multiple lines to make it more readable. However, because your original code was extremely unclear, it is possible that the mathematical meaning of my expression is different from your intention. I'm assuming that you intended to write the following:
Also, you declare paymentAmount as a parameter, although it is not a parameter, but rather a return value.
private double getPaymentAmount(double loanValue, double numOfPayments, double periodInterestRate) {
double paymentAmount = loanValue *
(periodInterestRate * Math.pow(1+periodInterestRate, numOfPayments)) /
(Math.pow(1+periodInterestRate, numOfPayments)-1);
return paymentAmount;
}
What is this syntax with loanValue [calculation1]/[calculation2]?:
paymentAmount = loanValue[periodInterestRate(1+periodInterestRate)^numOfPayments]/[(1+periodInterestRate)^numOfPayments-1];
This does not look like correct use of [] and if loanValue is a function maybe it should be more like:
paymentAmount = loanValue(periodInterestRate((1+periodInterestRate)^numOfPayments)/((1+periodInterestRate)^numOfPayments-1));
Or if loanValue is an array then maybe:
paymentAmount = loanValue[periodInterestRate(1+periodInterestRate)^numOfPayments]/((1+periodInterestRate)^numOfPayments-1);
But it is hard to believe that
periodInterestRate(1+periodInterestRate)
would match an array index or even a hash key.
Then maybe loanValue is just a number, as some others have suggested.
Please clarify: What is the type of loanValue?
Thanks
Square brackets don't mean the same thing as they do in math.Nethier does the caret. Caret is XOR. I think this is what you want:
loanValue * (Math.pow(periodInterestRate(1+periodInterestRate), numOfPayments)) / (Math.pow(1+periodInterestRate, numOfPayments-1))

Unexplained parenthesise in Java

What do parenthesis do in Java other than type casting.
I've seen them used in a number of confusing situations, here's one from the Java Tutorials:
//convert strings to numbers
float a = (Float.valueOf(args[0]) ).floatValue();
float b = (Float.valueOf(args[1]) ).floatValue();
I only know only two uses for parenthesis, calls, and grouping expressions. I have searched the web but I can't find any more information.
In the example above I know Float.valueOF(arg) returns an object. What effect does parenthesize-ing the object have?
Absolutely nothing. In this case they are not necessary and can be removed. They are most likely there to make it more clear that floatValue() is called after Float.valueOf().
So this is a case of parenthesis used to group expressions. Here it's grouping a single expression (which does obviously nothing).
It can be shortened to:
float a = Float.valueOf(args[0]).floatValue();
float b = Float.valueOf(args[1]).floatValue();
which can then be logically shortened to
float a = Float.parseFloat(args[0]);
float b = Float.parseFloat(args[1]);
I dont believe they serve any purpose here. Maybe left over after some refactoring
None other than to confuse you. It's as good as saying
float a = Float.valueOf(args[0]).floatValue();
directly.
I suspect the programmer just found it more readable. I don't agree with him in this particular case, but often use parentheses to make it clearer. For example, I find
int i = 3 + (2 * 4);
clearer than
int i = 3 + 2 * 4;
The extra parentheses in your code sample do not add anything.
//Your example:
float a = (Float.valueOf(args[0]) ).floatValue();
// equivalent:
float a = Float.valueOf(args[0]).floatValue();
It could be that the original programmer had done something more elaborate within the parentheses and so had them for grouping, and neglected to remove them when simplifying the code. But trying to read intent into old source is an exercise in futility.
The extra space in args[0]) ) is pretty odd looking, too, as it is unmatched in the opening paren.
Here they are used for grouping. What's inside one of those expression if of type Float, so you can apply the method floatValue() to the whole content of the parenthesis.
They could be removed here as there is no ambiguity. They would have been mandatory with an expression using another operator of higher preseance order. But according to the docs, there is no such operator, the dot/projector has highest priority. So they are really useless here.
Regards,
Stéphane

Strange Java error- float getting set to Infinity

I'm new to Java, and I'm using Processing to make some data visualizations. I'm getting this strange error in my code though, was wondering if anyone could help me out. It seems the Xspacing float keeps getting set to Infinity, however when I print out the expression it gets set to the proper value gets printed...
float Xspacing = (endX-(width*.04) - startX)/ values;
println((endX-(width*.04) - startX)/ values);
println(Xspacing);
Result is:
49.0
Infinity
Any help would be appreciated!
Sorry, I wrote this out very quickly and omitted some pretty necessary info:
49.0 IS what is should be. All other types are floats, besides values which is an integer.
The code DOES compile, and println is build into Processing, which is the framework (correct term?) that I'm using. It is basically a function that prints to the console in the Processing GUI.
Xspacing was intended to be data for my class "Graph," however when I define the variable within a public function "drawBasic" everything works fine. Now I am just curious....
Using System.out.println(0 yields the same results. Initial values or variables are:
float startX = 120.00001
float endX = 740.0
int values = 12
width is an integer (although not explicit) that is set to 800
The odd thing seems to be that within a function definition this works fine, its only when I try to define it within the class that it doesn't work...
Your code couldn't be like that because a number *.04 creates a double, and that would mean you'd need to cast the expression into a float.
For your code to compile it would have to be something like
float Xspacing = (float)((endX-(width*.04) - startX)/ values);
println((endX-(width*.04) - startX)/ values);
println(Xspacing);
Now, on the result. If your code had, for example:
System.out.println(3/0);
Java would give you a java.lang.ArithmeticException: / by zero
However, if you have
System.out.println(3f/0);
Then Java will give you "Infinity". Why? http://grouper.ieee.org/groups/754/
Try this:
float Xspacing = (endX-(width*.04) - startX)/ values;
println((float)((endX-(width*.04) - startX)/ values));
println(Xspacing);
float Xspacing = (endX-(width*.04) - startX)/ values;
Even assuming the variables are floats that line does not compile, because of the 0.4 double literal.
Also 'println' is not a standalone method, so you must have written your own.
What is your actual code?
you forget a ) and you should've put System.out.println(xspacing);
fyi you can also just type syso and ctrl spacebar and it will print out the print statement for you.

Categories

Resources