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

From |
wgould@stata.com (William Gould) |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: string to number -- limitations |

Date |
Sun, 13 Oct 2002 10:19:31 -0500 |

From: baum <baum@bc.edu> Kompal Sinha <kompal.sinha@anu.edu.au> wrote, > Hi there, I have converted a 15 digit string into a numerical number by > using real() command. My problem is that now the data is read as floats > while I want it to be in doubles as the string happens to be the caseId of > my data. How do I proceed?? and Kit Baum <baum@bc.edu> replied, Very bad idea. There is no way of representing this many digits reliably as a floating point number, even using double precision; [...] ^^^^^^^^^^^^^^^^^^^^^^^^^^^ (emphasis added). Kit is right about the "very bad idea" with respect to -float-, but not with respect to -double-. The problem arises at 16 digits, not 15. Below I demonstrate that 15 digits work: . input double x x 1. 999999999999990 2. 999999999999991 3. 999999999999992 4. 999999999999993 5. 999999999999994 6. 999999999999995 7. 999999999999996 8. 999999999999997 9. 999999999999998 10. 999999999999999 11. end . format x %20.0g . list x 1. 999999999999990 2. 999999999999991 3. 999999999999992 4. 999999999999993 5. 999999999999994 6. 999999999999995 7. 999999999999996 8. 999999999999997 9. 999999999999998 10. 999999999999999 ----+----1----+ The above numbers have 15 digits. Here is what happens if we repeat the experiment with 16 digits: . input double x x 1. 9999999999999990 2. 9999999999999991 3. 9999999999999992 4. 9999999999999993 5. 9999999999999994 6. 9999999999999995 7. 9999999999999996 8. 9999999999999997 9. 9999999999999998 10. 9999999999999999 11. end . format x %20.0g . list x 1. 9999999999999990 2. 9999999999999992 3. 9999999999999992 4. 9999999999999992 5. 9999999999999994 6. 9999999999999996 7. 9999999999999996 8. 9999999999999996 9. 9999999999999998 10. 10000000000000000 Double-precision numbers use 51+1 = 52 bits for the significand (+1 because one bit is "implied"). Thus, the largest integer that can be stored is all 52 bits on, which is (2^53)-1 = (9.007e+15)-1. The largest 15 digit integer is 999999999999999 = 9.99999999999999e+14, and that is below the limit. My advice to Kompal is to count your digits carefully be be sure that you have 15 and not 16, and then to use -double-. -- Bill wgould@stata.com P.S. I bet I know why Kit made the error because I almost made it myself. A 15-digit number, I said to myself, that's like 9.999...e+15, yes? No, a 15-digit number is 9.999...e+14. <end> * * 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:
**Re: st: Re: stability tests** - Next by Date:
**st: Random time effects** - Previous by thread:
**st: string to number -- limitations** - Next by thread:
**st: Re: old nl prolem still there** - Index(es):

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