TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java Math.pow Method

Use the Math.pow method to raise numbers to powers and compute exponents.
Math.pow, exponents. A number is squared. A number is cubed. With Math.pow we raise a number to any exponent. We use an exponentiation function.Math
Pow stands for power. If you have ever been in a math course, you (probably) already know what Math.pow does. It raises the first number to the power of the second number.
A simple example. Let us begin with this simple Java program. We square 5, which gives us 25. And we square 3 which yields 9. We print the results.

Double: The result of Math.pow is a double. We can cast this to an int if we know that no data loss due to narrowing will occur.

Argument 1: This is the base number we want to raise to a power. So to find the square of 5 we use 5 as the first argument to Math.pow.

Argument 2: This is the exponent. This can be fractional, but it is usually an int like 2 (which means square).

Integer, max
Java program that uses Math.pow method public class Program { public static void main(String[] args) { // Raise 5 to the power of 2. // ... Then raise 3 to the power of 2. double result1 = Math.pow(5, 2); double result2 = Math.pow(3, 2); // ... Display our results. System.out.println(result1); System.out.println(result2); } } Output 25.0 9.0
Square. With methods we can add abstractions to our programs to make them easier to reason about. For example this program uses a square() method. It wraps a call to Math.pow.Methods

Tip: It may be easier to call square() than use Math.pow with specific arguments. The performance cost here is minimal or none.

Java program that uses square method public class Program { static double square(int base) { // We use Math.pow with a second argument of 2 to square. return Math.pow(base, 2); } public static void main(String[] args) { // Use square method. double result1 = square(2); double result2 = square(3); double result3 = square(4); System.out.println(result1); System.out.println(result2); System.out.println(result3); } } Output 4.0 9.0 16.0
A review. With Math.pow we compute power functions. We square and cube numbers. With Math.pow we have a reliable and heavily-tested method in the Java framework to use.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf