Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Manually entry of 0.128 in the Data Editor

    I'm using Stata/SE 17.0

    I'm having trouble getting the exact value of 0.128 entered into a variable using either the Data Editor manually or by command.

    Here's the code I can enter to recreate the issue:
    Code:
    gen float x = .
    set obs 4
    replace x = .127 in 1
    replace x = .128 in 2
    replace x = .129 in 3
    replace x = .130 in 4
    Code:
    tab x
    provides a table with the same numbers as entered, except .130 is now .13

    The values in the data editor display the same in the variable column, however in the toolbar showing the active cell:

    .127 displays as .127
    .128 displays as .12800001
    .129 displays as .12899999
    .13 displays as .13

    Code:
    graph bar x if x==.13
    returns 'no observations' r(2000);

    I feel as though I've seen this topic before in the forum but I can't locate the thread.

  • #2
    If I use
    Code:
    gen double y = .
    replace y =.128 in 1
    graph bar y if y==.128
    I get the expected bar graph

    Comment


    • #3
      This is a precision issue.

      Code:
      search precision 
      for resources. There is overwhelming choice, but I recommend the blog posts of William Gould.

      In essence .128 as an exact decimal has no exact binary equivalent. It's in excellent company. Of decimals to 3 d.p. (999 of them from 0.001 to 0.999) only 0.125, 0.250, 0.375, 0.500, 0.625, 0.750, 0,875 -- so 7 out of 999 -- have exact binary equivalents by virtue of being multiples of 1/8 or 1/2^3.

      When you compare a float variable with the constant 0.128, Stata translates the latter into the best binary approximation it can find, but the former is truncated compared with that. Use a double and it works. Otherwise check out the function float().

      Comment


      • #4
        Good info, thank you Nick

        Comment


        • #5
          Check out this blog post if interested: https://blog.stata.com/2011/06/17/pr...-again-part-i/

          Comment

          Working...
          X