Announcement

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

  • browse if does not work

    Hi everyone,

    I am using browse or count to investigate in my merged data, but when I add a value to the values of another variable by using -gen- I cannot find the values using the if option.

    Here is an example
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long grid_id float(year radiance_full radiance_full_add)
    1 2000 0 .01
    1 2001 0 .01
    1 2002 0 .01
    1 2003 0 .01
    1 2004 0 .01
    1 2005 0 .01
    1 2006 0 .01
    1 2007 0 .01
    1 2008 0 .01
    1 2009 0 .01
    1 2010 0 .01
    1 2011 0 .01
    1 2012 0 .01
    1 2013 0 .01
    1 2014 0 .01
    1 2015 0 .01
    1 2016 0 .01
    1 2017 0 .01
    2 2000 0 .01
    2 2001 0 .01
    2 2002 0 .01
    2 2003 0 .01
    2 2004 0 .01
    2 2005 0 .01
    2 2006 0 .01
    2 2007 0 .01
    2 2008 0 .01
    2 2009 0 .01
    2 2010 0 .01
    2 2011 0 .01
    2 2012 0 .01
    2 2013 0 .01
    2 2014 0 .01
    2 2015 0 .01
    2 2016 0 .01
    2 2017 0 .01
    3 2000 0 .01
    3 2001 0 .01
    3 2002 0 .01
    3 2003 0 .01
    3 2004 0 .01
    3 2005 0 .01
    3 2006 0 .01
    3 2007 0 .01
    3 2008 0 .01
    3 2009 0 .01
    3 2010 0 .01
    3 2011 0 .01
    3 2012 0 .01
    3 2013 0 .01
    3 2014 0 .01
    3 2015 0 .01
    3 2016 0 .01
    3 2017 0 .01
    4 2000 0 .01
    4 2001 0 .01
    4 2002 0 .01
    4 2003 0 .01
    4 2004 0 .01
    4 2005 0 .01
    4 2006 0 .01
    4 2007 0 .01
    4 2008 0 .01
    4 2009 0 .01
    4 2010 0 .01
    4 2011 0 .01
    4 2012 0 .01
    4 2013 0 .01
    4 2014 0 .01
    4 2015 0 .01
    4 2016 0 .01
    4 2017 0 .01
    5 2000 0 .01
    5 2001 0 .01
    5 2002 0 .01
    5 2003 0 .01
    5 2004 0 .01
    5 2005 0 .01
    5 2006 0 .01
    5 2007 0 .01
    5 2008 0 .01
    5 2009 0 .01
    5 2010 0 .01
    5 2011 0 .01
    5 2012 0 .01
    5 2013 0 .01
    5 2014 0 .01
    5 2015 0 .01
    5 2016 0 .01
    5 2017 0 .01
    6 2000 0 .01
    6 2001 0 .01
    6 2002 0 .01
    6 2003 0 .01
    6 2004 0 .01
    6 2005 0 .01
    6 2006 0 .01
    6 2007 0 .01
    6 2008 0 .01
    end
    When I do
    Code:
    br if radiance_full==0
    it perfectly works.

    But when I do
    Code:
    br if radiance_full_add==0.01
    or
    Code:
    br if radiance_full_add==.01
    The table that appears is completely empty.

    I use Stata now for quite a while, but this problem never occurred.

    Any help is appreciate.

  • #2
    I use Stata now for quite a while, but this problem never occurred.
    That surprises me, because precision problems like this crop up all the time and people get caught unawares by them.

    The problem arises because there is no exact finite precision binary representation of the number 0.01 (just as there is no exact finite precision representation in decimal of the number 1/3). When you write the logical expression radiance_full_add == .01, both radiance_full_add and .01 are first converted to double precision floating point numbers. But the variable radiance_full_add was created as a float, not a double. So it contains some approximation of .01 that fits within a float. To convert it to a double, Stata, not knowing that this number is, in your mind, an approximation to decimal .01, expands it to a double by packing 0's on the end. On the other side of ==, when Stata sees a literal constant .01 it converts it directly to a double. So this is analogous to comparing, in decimal, 0.33333 to 0.333333333. They are not equal, so Stata shows you nothing in the browser when you condition on equality.

    There are two solutions. One is to go back and create both radiance and radiance_full_add as doubles to start with. The other is to keep those variables as they are accept approximate equality at the float-precision level:
    Code:
    browse if float(radiance_full_add) == float(0.01)
    More generally, in Stata, or any other computer programming language, it is hazardous to ever condition on exact equality of non-integer numeric variables.
    Last edited by Clyde Schechter; 21 Mar 2022, 16:31.

    Comment


    • #3
      Clyde Schechter
      That surprises me, because precision problems like this crop up all the time and people get caught unawares by them.
      Seems like there is always a first time for something!

      Thank you very much! For the solution and extended explanation. I really appreciate it!

      Comment

      Working...
      X