New features in the Java Math class, part 1th: real numbers

Source: Internet
Author: User
Tags abstract mathematical functions

Sometimes you will be familiar with a class to forget its existence. If you can write a Java.lang.Foo document, Eclipse will help you automate the functions you need, and you do not need to read its Javadoc. For example, I used Java.lang.Math (a Class I thought I knew very well), but to my surprise, I recently stumbled upon its javadoc--this is probably the first time I've read it in almost five years, and I've found that the size of this class is almost doubled, containing 20 A new way I've never heard of. Looks like I'm going to make a look at it.

The 5th edition of the Java™ language specification adds 10 new methods to Java.lang.Math (and its sister edition Java.lang.StrictMath), and Java 6 adds 10 more. In this article, I'll focus on some of the more mundane mathematical functions, such as LOG10 and cosh. In the 2nd part, I'll explore functions designed specifically to manipulate floating-point numbers (as opposed to abstract real numbers).

The difference between an abstract real number (such as π or 0.2) and a Java double is obvious. First, the ideal state of a number is infinite precision, while the Java notation limits the number to a fixed number of digits. This is important when dealing with very large and very small numbers. For example, 2,000,000,001 (2,000,000,001) can be expressed precisely as an int, not a float. The nearest floating-point representation is 2.0e9-, or 200 million. It's better to use double numbers because they have more digits (which is one reason to always use double instead of float), but their precision is still limited.

The second limitation of computer algorithms (algorithms in the Java language and other languages) is that it is based on binary rather than decimal. Scores such as 1/5 and 7/50 can be expressed in decimal notation (0.2 and 0.14 respectively), but in binary notation there will be duplicate scores. As 1/3 in decimal notation, it becomes 0.3333333 ... With a base of 10, any fraction of a denominator that contains only prime factors 5 and 2 can be accurately represented. With a base of 2, only fractions with a denominator of 2 can be accurately represented: 1/2, 1/4, 1/8, 1/16, and so on.

This inaccuracy is one of the most important reasons for the urgent need for a math class. Of course, you can use only the standard + and * operators and a simple loop to define trigonometric functions and others that use the Taylor series expansion, as shown in Listing 1:

Listing 1. Using Taylor series to calculate sine

public class Sinetaylor {
public static void Main (string[] args) {
for (double angle = 0; angle <= 4*math.pi; angle + = MATH.PI/8) {

+ "T" + math.sin (angle));
}
}
public static double degrees (double radians) {
return 180 * RADIANS/MATH.PI;
}
public static double Taylorseriessine (double radians) {
Double sine = 0;
int sign = 1;
for (int i = 1; i < i+=2) {
Sine + + math.pow (radians, i) * sign/factorial (i);
Sign *=-1;
}
return sine;
}
private static double factorial (int i) {
double result = 1;
for (int j = 2; J <= i; j +) {
Result *= J;
}
return result;
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.