Power function introduction into calculator
I have this Java code but I do not know how to introduce power, sin, cos or other functions in it. I looked for it various websites but no luck finding it
could someone say how to introduce power function?
To answer the question I think you're really wanting to know, which is how does THAT code calculate math functions without using Java's math package...
You will probably want to look at Java's Math package. It has lots of math functions including the ones you are looking for. You may need to add an import of import java.lang.Math; at the beginning of the file you use the Math functions in, if the import is not already there.
For example, double answer = Math.pow(firstnum,secondnum); is the function to raise one number to the power of another, or double answer = Math.sin(anum) to give the sin of a value in the variable anum, and so on.
As you read through the javadoc for that class you will see additional functions that should supply the rest of the "scientific" calculator features you want.
Keep in mind also that integers and doubles do have limits to their storage capacities, so you may want to consider how to handle overflow if someone decides to do something crazy like 100,000^100,000,000. If having enough precision for really really large numbers is important to you, you may also want to look into classes such as BigDecimal or BigInteger.
Java has these functions built-in in the java.lang.Math class as static methods. You can cal them just like this:
Power, using Math.pow(double, double)
double number = 3.42d; double exponent = 5.14d; double power = Math.pow(number, exponent);
Square Root, using Math.sqrt(double)
double shortSide1 = 4.0d; double shortSide2 = 3.0d; double longSide = Math.sqrt(shortSide1 * shortSide1 + shortSide2 * shortSide2); // All credits goto Pythagoras.
Sin, cos, tan
double angle = 2.13d; double sin = Math.sin(angle); double cos = Math.cos(angle); double tan = Math.tan(angle);
Arcsin, arccos, arctan
double value = 0.13d; double asin = Math.asin(value); double acos = Math.acos(value); double atan = Math.atan(value);
All these functions are Native (not written in Java), which mean that they will perform as good as possible on your platform.