0% found this document useful (0 votes)
42 views5 pages

Java Math Class Functions Overview

This document discusses various math functions in the java.lang.Math class. It provides examples of functions like Math.abs(), Math.ceil(), Math.floor(), Math.sqrt(), Math.cbrt(), Math.min(), Math.max(), Math.round(), Math.random(), and Math.pow(). It also includes exercises to test the understanding of using these functions together and predicting the outputs.

Uploaded by

rkpost1703
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views5 pages

Java Math Class Functions Overview

This document discusses various math functions in the java.lang.Math class. It provides examples of functions like Math.abs(), Math.ceil(), Math.floor(), Math.sqrt(), Math.cbrt(), Math.min(), Math.max(), Math.round(), Math.random(), and Math.pow(). It also includes exercises to test the understanding of using these functions together and predicting the outputs.

Uploaded by

rkpost1703
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JAVA - Math Functions

The [Link] contains a set of basic math functions. Return type of the function is
given in red.
[Link]() depends on the type of parameter passed
It returns the absolute value of the parameter passed to it.
Examples:
int a = [Link](10); // a = 10 ....since the parameter is already positive no changes occur
double b= [Link](-20.3); // b = 20.3
The [Link]() method is overloaded in 4 versions:
[Link](int)
[Link](long)
[Link](float)
[Link](double)
The return type of these depend on the type of the parameter passed it.

[Link]() double
This function rounds a floating point value up to the nearest integer value. The rounded
value is returned as a double type.
Example:
double c= [Link](7.343); // c = 8.0
double d=[Link](-7.343) // d=-7.0

[Link]() double
This function rounds a floating point value down to the nearest integer value. The rounded
value is returned as a double type .
Example:
double f = [Link](7.343); // f = 7.0
double f=[Link](-7.343); // f = -8.0

[Link]() double
This method calculates the square root of the parameter given to it.

SUJATHA SRIDHAR
Example:
double sq = [Link](4); // sq=2.0
double sq=[Link](-4): //NaN, since sqrt cannot operate on negative value.

[Link]() double
This method calculates the cuberoot of the parameter given to it.
Example:
double cq = [Link](64); // cq=4.0
double cq=[Link](-64): // cq=-4.0 (-4.0*-4.0*-4.0=-64.0)

[Link]() depends on the type of parameter passed


This method returns the smallest of two values passed to it as parameter.
Example:
int m = [Link](10, 20); // m=10
double m=[Link](0.0,-0.0) // m=-0.0
int m=[Link](-3,3) //m = -3

[Link]() depends on the type of parameter passed


This method returns the largest of two values passed to it as parameter.
Example:
int m = [Link](10, 20); // m= 20
double m=[Link](0.0,-0.0) // m= 0.0
int m=[Link](-3,3) // m = 3

[Link]() int
This method rounds a float or double to the nearest integer using normal math round rules
(either up or down). The return type is an integer.
Example:
int roundDown = [Link](23.445); // roundDown = 23
int roundUp = [Link](23.545); // roundUp = 24

SUJATHA SRIDHAR
[Link]() double
This method returns a random floating point number between 0 and 1.
Example:
double r = [Link]() // returns a random number between 0 and 1 example
0.89878....
If you need an integer value, use the round() along with [Link]()

(int)([Link]() * ((max - min) + 1)) + min


To get a random numbers between 1 and 10
int x=(int)([Link]() * ((10 - 1) + 1)) + 1;
[Link](x);
To get a random number between 1 and 100
int x=(int)([Link]() * ((100 - 1) + 1)) + 1;
[Link](x);
To get a random number between 20 and 30
int x=(int)([Link]() * ((30 - 20) + 1)) + 20;
[Link](x);

[Link]() double
This function takes two parameters. The method returns the value of the first parameter
raised to the power of the second parameter.
Example:
double p1 = [Link](2,2); p1=4.0
double p2 = [Link](2,0); // p2=1.0
double p3= [Link](9,0.5) // p3=3.0 // any number raised to power of 0.5 will return its
squareroot.

SUJATHA SRIDHAR
MATH METHOD -EXERCISES
SET 1

SET 2 Predict the output:


1. [Link]([Link](9.0))
2. [Link]([Link](9,3),8)
3. [Link]([Link](-64))
4. [Link]([Link](-67.8))
5. [Link]([Link](8.7))
6. [Link]([Link](0.0,-0.0),-0.0)
7. [Link]([Link](9.9)+6)
8. [Link]([Link](-64))
9. [Link]([Link](81))
[Link]([Link](12,[Link](-12)),2)
[Link]([Link](64,0.5),2)
[Link]([Link](9,0.5),[Link](9,0))
13.(int)([Link]()*10)
14. [Link]([Link](20.3),[Link](20.6))
15. [Link](-9.4)+[Link](10.5)

SUJATHA SRIDHAR
SET 3

SUJATHA SRIDHAR

Common questions

Powered by AI

Math.pow() calculates a number raised to a power: Math.pow(base, exponent). It returns a double that represents 'base' raised to 'exponent'. Special cases include Math.pow(x, 0), which always returns 1.0 due to any number to the power of 0 equaling 1 . Another case is Math.pow(x, 0.5), effectively calculating the square root of x, such as Math.pow(9, 0.5) returning 3.0 . Handling includes considering floating-point precision and range validity, especially with non-integer bases and negative exponents.

The Math.abs() method in Java returns the absolute value of its parameter, handling different primitive types like int, long, float, and double. The method is overloaded accordingly: Math.abs(int), Math.abs(long), Math.abs(float), and Math.abs(double). Each returns the same type as the parameter. For instance, Math.abs(-20.3) will return 20.3 as a double, preserving the type and converting the negative value to positive .

Math.random() generates a random double between 0 and 1 . To create bounded integers, it must be scaled and offset by a range. For instance, to generate an integer between 1 and 10, the expression (int)(Math.random() * ((10 - 1) + 1)) + 1 is used . Math.round() can adjust floating results to integers, ensuring proper bounding when combining with other expressions, like ensuring edge-case rounding near boundaries . This utilizes Math.random()'s continuous distribution to create discrete results fitting specified ranges.

Math.round() rounds to the nearest integer, significantly impacting numerical processing stages by truncating fractional data, consequently affecting accuracy and precision. For instance, Math.round(23.445) results in 23, discarding the fractional detail . Using this in early processing stages may lead to cumulative rounding errors, reducing data reliability over multiple operations. Subsequently, processing errors may be compounded across operations or datasets. Thus, Math.round() applications should consider where minimal precision is acceptable and when rounded results align with the target precision level adequately.

Math.min() returns the smaller of two numbers, while Math.max() returns the larger. In cases with zeros and negatives, Math.min(0.0, -0.0) results in -0.0 because -0.0 is considered smaller . For Math.max(0.0, -0.0), the result is 0.0 since positive zero is larger . Additionally, Math.min(-3, 3) returns -3, and Math.max(-3, 3) returns 3, perfectly illustrating how signs affect comparisons . These methods can handle floating-point types conserving the type and handling sign nuances appropriately.

Methods like Math.sqrt() handle problematic inputs by considering logical conditions such as non-negative constraints. Math.sqrt(negative number) returns NaN since theoretical mathematics prohibits square roots of negatives unless complex numbers are used . Error avoidance involves pre-checks or alternative calculations (e.g., using Math.abs() first: Math.sqrt(Math.abs(-4)) results in 2.0 but changes input meaning). Java's robust error-proof math framework encourages preventative measures, perhaps checking inputs before calculations to ensure valid operations.

Applying varied Math methods like Math.min() on composites of Math.max() calculations evaluates nested hierarchy problems. For instance, Math.min(Math.max(9,3),8) ends with 8. Here, Math.max(9,3) gives 9; then Math.min(9,8) returns 8 . As composites integrate within Math expressions, outcomes affect efficient computational paths. They develop multiple layer evaluations, testing priorities in relations: precise adjustments result from knowing inner dependencies. This subtle interplay between outcomes offers insights into sophisticated solutions, ensuring controllable and reliable expression through layered Math methods.

The methods Math.ceil() and Math.floor() impact floating-point calculations by rounding in opposite directions, affecting hierarchical and sequential operations. For instance, Math.ceil(Math.floor(-67.8)) results in -68.0. Initially, Math.floor(-67.8) yields -68, and subsequent Math.ceil(-68.0) provides -68.0, influencing calculations like subsequent Math operations . This pairing can generate non-intuitive results, especially when mixed with other operations, demonstrating significant effects from precision and numerical hierarchy in computations.

Math.ceil() rounds a floating-point number up to the nearest integer and returns it as a double. For example, Math.ceil(7.343) returns 8.0 . With negative numbers, it moves towards zero, such as Math.ceil(-7.343) returning -7.0 . Conversely, Math.floor() rounds down, away from zero for negative numbers, such as Math.floor(-7.343) returning -8.0 . For positive numbers, it rounds down towards zero, so Math.floor(7.343) returns 7.0 . These methods differ mainly in handling negatives, influencing rounding direction relative to zero.

Boolean logic can be integrated with Math methods like comparing results or conditional executions to refine complex calculations. For example, using conditionals like if-else with Math.min() and Math.max() can streamline decision-making in dynamic ranges or limits determination: if(Math.min(a, b) < 0), adjust flow, handling negatives differently. Combined methods can then dictate variable assignments or further Math method cascading to ensure logical conclusions adhere to criteria, e.g., ensuring Math.sqrt(x) only after confirmed valid x parameters, promoting structured and efficient numerical logic formulations.

You might also like