Chapter5
5.2
5.2.5 Input and Output Redirections
input redirection
In the preceding example, if you have a large number of data to enter, it would be cumbersome
to type from the keyboard. You can store the data separated by whitespaces in a text file, say
input.txt, and run the program using the following command:
java SentinelValue < input.txt
This command is called input redirection. The program takes the input from the file input.txt rather than having the user type the data from the keyboard at runtime.
output redirection
Similarly, there is output redirection, which sends the output to a file rather than displaying it on the console. The command for output redirection is:
java ClassName > output.txt
Input and output redirection can be used in the same command. For example, the following command gets input from input.txt and sends output to output.txt:
java SentinelValue output.txt
5.7 Minimizing Numeric Errors
Using floating-point numbers in the loop continuation condition may cause numeric errors.
Numeric errors involving floating-point numbers are inevitable, because floating-point numbers are represented in approximation in computers by nature.
not accurate
floating-point numbers 相加对精度有要求,float有时不准确,double有时也不准缺。
maybe no effect
Adding a very small number to a very big number can have no effect if the result requires more precision than the variable can store. For example, the inaccurate result of 100000000.0+ 0.000000001 is 100000000.0. To obtain more accurate results, carefully select the orderof computation. Adding smaller numbers before bigger numbers is one way to minimize errors.