mirror of
				https://github.com/python/cpython.git
				synced 2025-11-03 19:34:08 +00:00 
			
		
		
		
	Improve the rounding and summing examples.
This commit is contained in:
		
							parent
							
								
									cc32a11976
								
							
						
					
					
						commit
						4af362905c
					
				
					 1 changed files with 17 additions and 7 deletions
				
			
		| 
						 | 
					@ -109,14 +109,24 @@ It's important to realize that this is, in a real sense, an illusion: you're
 | 
				
			||||||
simply rounding the *display* of the true machine value.
 | 
					simply rounding the *display* of the true machine value.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
One illusion may beget another.  For example, since 0.1 is not exactly 1/10,
 | 
					One illusion may beget another.  For example, since 0.1 is not exactly 1/10,
 | 
				
			||||||
summing ten values of 0.1 may not yield exactly 1.0, either::
 | 
					summing three values of 0.1 may not yield exactly 0.3, either::
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   >>> sum = 0.0
 | 
					   >>> .1 + .1 + .1 == .3
 | 
				
			||||||
   >>> for i in range(10):
 | 
					   False
 | 
				
			||||||
   ...     sum += 0.1
 | 
					
 | 
				
			||||||
   ...
 | 
					Also, since the 0.1 cannot get any closer to the exact value of 1/10 and
 | 
				
			||||||
   >>> sum
 | 
					0.3 cannot get any closer to the exact value of 3/10, then pre-rounding with
 | 
				
			||||||
   0.9999999999999999
 | 
					:func:`round` function cannot help::
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					   >>> round(.1, 1) + round(.1, 1) + round(.1, 1) == round(.3, 1)
 | 
				
			||||||
 | 
					   False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Though the numbers cannot be made closer to their intended exact values,
 | 
				
			||||||
 | 
					the :func:`round` function can be useful for post-rounding so that results
 | 
				
			||||||
 | 
					have inexact values that are comparable to one another::
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    >>> round(.1 + .1 + .1, 1) == round(.3, 1)
 | 
				
			||||||
 | 
					    True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Binary floating-point arithmetic holds many surprises like this.  The problem
 | 
					Binary floating-point arithmetic holds many surprises like this.  The problem
 | 
				
			||||||
with "0.1" is explained in precise detail below, in the "Representation Error"
 | 
					with "0.1" is explained in precise detail below, in the "Representation Error"
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue