2013年12月30日 星期一

[ In Action ] The Simple Groovy datatypes - Working with numbers

Preface: 
The available numeric types and their declarations in Groovy were introduced in section 3.1. You have seen that for decimal numbers, the default type isjava.math.BigDecimal . This is a feature to get around the most common misconceptions about floating-point arithmetic. We’re going to look at which type is used where and what extra abilities have been provided for numbers in the GDK. 

Coercion with numeric operators: 
It is always important to understand what happens when you use one of the numeric operators. Most of the rules for the addition, multiplication, and subtraction operators are the same as in Java, but there are some changes regarding floating-point behavior, and BigInteger and java.math.BigDecimal also need to be included. The rules are straightforward. The first rule to match the situation is used. 

For the operations + , - , and * : 
* If either operand is a Float or a Double , the result is a Double.
* Otherwise, if either operand is a java.math.BigDecimal , the result is a java.math.BigDecimal
* Otherwise, if either operand is a BigInteger , the result is a BigInteger.
* Otherwise, if either operand is a Long , the result is a Long.
* Otherwise, the result is an Integer.

Table 3.9 depicts the scheme for quick lookup. Types are abbreviated by uppercase letters. 
 

Rules can be daunting without examples, so this behavior is demonstrated in table 3.10. 
 

In Java, results like in the fourth row are often surprising—for example, (1/2) is always zero because when both operands of division are integers, only integer division is performed. To get 0.5 in Java, you need to write (1f/2)

GDK methods for numbers: 
The GDK defines all applicable methods from table 3.4 to implement overridable operators for numbers such as plus , minus , power , and so forth. They all work without surprises. In addition, the abstoInteger , and round methods do what you’d expect. 

More interestingly, the GDK also defines the methods times , uptodownto , and step. They all take a closure argument. Listing 3.9 shows these methods in action: timesis just for repetition, upto is for walking a sequence of increasing numbers, downto is for decreasing numbers, and step is the general version that walks until the end value by successively adding a step width. 
- Listing 3.9 GDK methods on numbers 
  1. def store = ''  
  2. 10.times{       
  3.     store += 'x'  
  4. }  
  5. assert store == 'xxxxxxxxxx'  
  6.   
  7. store = ''  
  8. 1.upto(5) { number ->     
  9.     store += number  
  10. }  
  11. assert store == '12345'  
  12.   
  13. store = ''  
  14. 2.downto(-2) { number ->  
  15.     store += number + ' '  
  16. }  
  17. assert store == '2 1 0 -1 -2 '  
  18.   
  19. store = ''  
  20. 0.step(0.50.1 ){ number ->  
  21.     store += number + ' '  
  22. }  
  23.   
  24. assert store == '0 0.1 0.2 0.3 0.4 '  
Calling methods on numbers can feel unfamiliar at first when you come from Java. Just remember that numbers are objects and you can treat them as such. You have seen that in Groovy, numbers work the natural way and even guard you against the most common errors with floating-point arithmetic. In most cases, there is no need to remember all details of coercion. When the need arises, this section may serve as a reference.

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...