[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

From |
"Nick Cox" <n.j.cox@durham.ac.uk> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
st: RE: -forvalues- and decimal numbers in the numlist |

Date |
Sat, 11 Sep 2004 20:01:01 +0100 |

This is a consequence of the fact that Stata works in binary, so that not all decimals can be held exactly. 0.1 is a case in point. The heart of the problem is shown by turning the magnifying glass on the first puzzling result: . forval i = 0(0.1)20 { 2. di %21.18f round(`i', 0.01) 3. } 0.000000000000000000 0.100000000000000010 0.200000000000000010 0.299999999999999990 0.400000000000000020 0.500000000000000000 0.599999999999999980 0.700000000000000070 0.800000000000000040 0.900000000000000020 . . . 19.400000000000002000 19.500000000000000000 19.600000000000001000 19.699999999999999000 19.800000000000001000 19.900000000000002000 You are asking Stata to add 0.1 each time, but Stata has do the equivalent in _binary_, and with even all the bits in the world it would usually have to approximate. In this case another 0.1 would take beyond 20, so it stops there. Incrementing by 1/8 is not problematic given that 1/8 = 1/2^3. Read [U] 16.10 every night after brushing your teeth... For another example, see http://www.stata.com/support/faqs/data/mod.html Nick n.j.cox@durham.ac.uk eva.poen@unisg.ch I am writing a program with a -forvalues- loop in it. I encountered a strange problem (Stata 8.2) which can be illustrated by this sample code: forvalues z = 15(1)30 { di "current maximum is: `z'" forvalues i = 0(0.1)`=`z'' { local a = round(`i',0.01) } di "a is: `a'" } Output is current maximum is: 15 a is: 15 current maximum is: 16 a is: 16 current maximum is: 17 a is: 17 current maximum is: 18 a is: 18 current maximum is: 19 a is: 19 current maximum is: 20 a is: 19.9 current maximum is: 21 a is: 20.9 current maximum is: 22 a is: 21.9 current maximum is: 23 a is: 22.9 current maximum is: 24 a is: 23.9 current maximum is: 25 a is: 24.9 current maximum is: 26 a is: 25.9 current maximum is: 27 a is: 26.9 current maximum is: 28 a is: 27.9 current maximum is: 29 a is: 28.9 current maximum is: 30 a is: 29.9 Stata stops looping at the second last value in the numlist from 20 onward. This behaviour continues until 76, from where on the correct value is returned again. This seems to happen only with decimal numbers, and only with some of them (compare to -forvalues i = 0(0.125)`=`z'' -). I can't really see a pattern. Maybe someone can give me a clue and tell me what to do about it. * * For searches and help try: * http://www.stata.com/support/faqs/res/findit.html * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

- Prev by Date:
**st: -forvalues- and decimal numbers in the numlist** - Next by Date:
**st: Re: RE: Re:Is there any way ?** - Previous by thread:
**st: -forvalues- and decimal numbers in the numlist** - Next by thread:
**st: blabel(group) for bar graphs** - Index(es):

© Copyright 1996–2017 StataCorp LLC | Terms of use | Privacy | Contact us | What's new | Site index |