Waterloo Cup-decimal operation, keep decimal point __ Waterloo Cup

Source: Internet
Author: User

The default decimal declaration in Java is double;

such as: float x = 1.0, error, need to write: float x = 1.0f;

* Float means that the single-precision floating-point number occupies 4 bytes in the machine and is described with a 32-bit binary
* Double indicates that the double-precision floating-point number occupies 8 bytes in the machine, and is described in 64-bit binary binary

* For programmers, the difference between double and float is that the double is high, the number is 16 digits, and the float precision is 7 digits. But double consumes twice times the amount of float, double is much slower than float, the name of the mathematical function in the Java language double and float is different, do not write wrong, can use single precision do not use double precision (in order to save memory, speed up the operation speed).

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------

Keep decimal point:

Method One:

Double d = 3.1415926;
string result = string. Format ("%.2f", D);
%.2f%. Represents a floating-point type with a result of an arbitrary number of digits before the decimal point of 2 representing a two-bit decimal format.


Topic One:

There are many formulas for calculating pi pai in history, in which Gregory and Leibniz found the following formula:
Pai = 4* (1-1/3+1/5-1/7 ...)
The formula is simple and graceful, but in a bad way, it converges too slowly.
If we rounded to keep its two decimal digits, then:
Cumulative 1 Items are: 4.00
Cumulative 2 items are: 2.67
Cumulative 3 items are: 3.47
。。。
Please write out what it accumulates 100 items (rounded to two digits after the decimal).
Note: Only fill in the decimal itself, do not fill out any redundant instructions or explanations.


public class Test
{public
	static void Main (string[] args)
	{
		float sum = 0;
		for (Float i = 1; I <= i++) {
			if (i%2!=0) {
				sum = 1.0f/(i*2-1);
			} else {
				Sum-= 1.0f/(i*2-1);
			}
			String result = String.Format ("%.2f", sum*4);
			SYSTEM.OUT.PRINTLN (result);}}


--------------------------------------------------------------------------------------------------------------- --------------------------------------------------
Keep Decimal Method Two:

  public static String FormatDouble4 (double D) {
        DecimalFormat df = new DecimalFormat ("#.00");
        Return Df.rmat (d);//string type
    } public
class Main {public
	static void Main (string[] args) {
		String astring = "2.34";
		float a = float.parsefloat (astring);//string type and float conversion
		System.out.println (a);
	}
--------------------------------Topic Two:
Infinite Fractions

An infinite fraction, sometimes tending to a fixed number.
Please calculate the infinite fraction shown in figure 1.jpg, asking for rounding, accurate to 5 digits after the decimal point, and 0 with a small digit.

Please fill in the floating point number, do not fill in any superfluous content.

/* Recursive and decimal operations */Public
class test
{public
	static void Main (string[] args)
	{
		float a= Digui (1.0f);
		String result = String.Format ("%.5f", a);
		SYSTEM.OUT.PRINTLN (result);
	}

	private static float Digui (float f) {
		//TODO auto-generated method Stub
		if (f==100.0f) {return
			F;
		} Return
		f/(F+digui (f+1));
	}

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------


Some of the high precision must be in double type;


1/1 + 1/2 + 1/3 + 1/4 + ... In mathematics, it is called a harmonic series.

It is divergent, that is to say, if you add enough items, you can get any large numbers.

However, it is diverging very slowly:

Top 1 Items and reach 1.0
Top 4 and only over 2.0
The top 83 and only over 5.0

public class Test {public

	static void Main (string[] args) {
		double sum = 0;
		Double i = 1.0d;
		int count = 0;
		while (sum<15.0) {
			sum+=1.0d/i;
			i++;
			count++;
		}
		System.out.println (count);
	}

}



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.