# Re: st: problem with blabla if baz == float(#)

 From Joseph Coveney To statalist@hsphsun2.harvard.edu Subject Re: st: problem with blabla if baz == float(#) Date Tue, 19 Nov 2002 09:55:05 +0900

```Guillaume Frechette had posted on a problem related to numerical precision in
expressions with arguments of certain noninteger numbers.  I posted an admittedly
awkward approach using tolerances.  Nick Cox posted an alternative, exploiting Stata's
consistency in internally representing precision to define categories using -egen,
group()- that contain like noninteger numbers.  Guillaume followed up with what seems
like a more elegant solution:

----------------------------begin excerpted posting-----------------------------------------------

Nick proposed one solution, here's another one which I just found:

. tab baz if baz==1-float(1/3)-float(1/3)

baz |      Freq.     Percent        Cum.
------------+----------------------------------
.3333333 |       1257      100.00      100.00
------------+----------------------------------
Total |       1257      100.00

---------------------------end excerpted posting--------------------------------------------------

Guillaume's solution works for single-precision (see
www.stata.com/support/faqs/data/float.html), but an attempt to use it with double-
precision variables doesn't work.  Omitting the -float()- function, however, does seems
to work with double-precision variables, in a manner exactly the opposite of that with
single-precsion variables (see log file below).  Stata's representation of precision
doesn't seem consistent here for some reason.

Joseph Coveney

--------------------------------------begin log--------------------------------------------------------

. set obs 850
obs was 0, now 850

. generate double foo=1.0/3.0

. generate double bar=1.0/3.0

. generate double baz=1.0-foo-bar

. * next line from Guillaume Frechette's posting
. tab baz if baz==1-float(1/3)-float(1/3)
no observations

. tabulate baz if baz==1-1/3-1/3

baz |      Freq.     Percent        Cum.
------------+-----------------------------------
.3333333 |        850      100.00      100.00
------------+-----------------------------------
Total |        850      100.00

. clear

. set obs 850
obs was 0, now 850

. generate float foo=1.0/3.0

. generate float bar=1.0/3.0

. generate float baz=1.0-foo-bar

. tab baz if baz==1-float(1/3)-float(1/3)

baz |      Freq.     Percent        Cum.
------------+-----------------------------------
.3333333 |        850      100.00      100.00
------------+-----------------------------------
Total |        850      100.00

. tabulate baz if baz==1-1/3-1/3
no observations

--------------------------------------begin log--------------------------------------------------------

*
*   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/
```