您的位置:首页 > 编程语言 > Java开发

《JAVA编程技巧1001条》第334条:数学函数 求绝对值.

2017-12-17 09:17 441 查看
《JAVA编程技巧1001条》第334条:数学函数 求绝对值
MATH FUNCTIONS IN JAVA
Java中的数学函数                            

 

334 Getting theAbsolute Value of a Number

334 求数的绝对值

 

Javas java.lang.Math class provides avariety of static methods for exponentiation, floating-point trigonometry, minand max functions, as well as other operations. Because they are all staticmethods, your programs can reference
them by the class name without having tocreate an instance of the class. Several of the following Tip discuss thesemathematical methods in detail.

Java的类java.lang.Math为我们提供了多种类型的静态方法,可用来实现指数、浮点三角函数、最大和最小、以及其它运算。 由于它们都是静态方法,在程序中你可通过类名来引用它们,无需创建类的实例。以下诀窍详细讨论这些数学方法。

 

To return the absolute value of a number, the Math classprovides four overloaded methods all named abs. The methods support the fournumeric types, int, long, float, and double:

为了返回一个数的绝对值,由Math类提供了四个都叫abs()的相同方法,分别用来支持int、

long、float以及double四种数据类型:

public static intabs(int a);
public static longabs(long a);
public static floatabs(float a);
public static doubleabs(double a);
 

For example, to return the absolute value of a float, you canuse the abs method, as shown:

例如,为了返回一个浮点数的绝对值,你可按下面的方式使用abs()方法:

 

float num = -5.8F;
num =Math.abs(num);  // Assigns the value 5.8F
 

335 Using the sqrt Method

335 sqrt方法的使用

When your programs perform arithmetic operations,there will betimes when you will need to take the square root of a value. In such cases,your programs can use the Math class sqrt method. The
format of the sqrt methodis as follows:

当你的程序在执行算术运算时,有时需要计算一个数的平方根。这时,你的程序可使用Math类中的sqrt方法。方法sqrt的格式如下:

public static doublesqrt(double a) throws ArithmeticException;
 

The following statements, for example, use the Math class sqrtmethod to return the square root of the value 7.9:

例如,以下语句是利用Math类中的sqrt方法来返回数7.9的平方根:

 

float num = 7.9F;
num = (float) Math.sqrt(num);  // return the square root value
num = (float)Math.sqrt(num);  // 返回平方根值
 

Notice that this statement uses an explicit cast to assign thereturn value to a float because the sqrt method returns a double. In this case,the variable num will have a value of 2.81069 after the assignment.

注意,这一语句用强制类型转换方式指定返回数值为float型,因为在缺省时sqrt方法返回的值为double型。在本例中,变量num在赋值后所得到的值将是2.81069。

 

336 Using the cos Method

335 cos方法的使用(译著:以下一段中原文不确切)

For a triangle, the cosine of an angle is the ratio of theangles adjacent edge to the hypotenuse. The Math class cos method returns thecosine of an angle expressed in radians. The declaration of the cos function isas follows:

在一直角三角形中,一个角的余弦(cosine)就是此角的邻边与斜边的比。Math类中的cos方法返回用弧度表示的角的余弦值。cos函数的说明如下:

public static doublecos(double a);
 

To return the cosine of an angle, you can use the cos method, asshown:

要得到一个角的余弦,你可按以下形式来使用cos方法:

 

double num, angle =0.785398D;   // angle in radians
num =Math.cos(angle);           // return thecosine of the angle
double num, angle =0.785398D;   // 角的弧度值
num =Math.cos(angle);           // 返回角的余弦
 

In this case, the statements will assign the variable num thevalue of 0.707107。

在本例子中,语句将把0.707107赋给变量num作为其值。

 

337 Using the sin Method

337sin方法的使用(译著:以下一段中原文不确
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: