Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: float function question


From   Mike Wazowski <[email protected]>
To   [email protected]
Subject   Re: st: float function question
Date   Tue, 16 Jun 2009 20:19:14 -0700 (PDT)

as you can see,

assert float(.01)==.01

returns false so your 

replace x_str = "0.01" if float(x) == 0.01

evaluates to false and there is no replacement

it may seem puzzling but try

. di %21x .01
+1.47ae147ae147bX-007

. di %21x float(.01)
+1.47ae140000000X-007

as you can see, the underlying hexadecimal representation of ..01 and float(.01) differs, see nick cox' stata tip 33 for details:

http://www.stata-journal.com/article.html?article=dm0022

and i also think bill gould has written about precision previously on stata list.

mike



--- On Wed, 6/17/09, David Airey <[email protected]> wrote:

> From: David Airey <[email protected]>
> Subject: st: float function question
> To: "Statalist" <[email protected]>
> Date: Wednesday, June 17, 2009, 2:55 AM
> .
> 
> Why does the float function not work like this? I feel
> silly.
> 
> clear
> input float x
> 0.00
> 0.01
> 0.03
> 0.10
> 0.30
> 1.00
> end
> generate str4 x_str = ""
> replace x_str = "0.00" if float(x) == 0.0
> replace x_str = "0.01" if float(x) == 0.01
> replace x_str = "0.03" if float(x) == 0.03
> replace x_str = "0.10" if float(x) == 0.1
> replace x_str = "0.30" if float(x) == 0.3
> replace x_str = "1.00" if float(x) == 1.0
> list, clean
> 
> which gave
> 
> . list, clean
> 
>      
>    x   x_str
>   1.     0    0.00
>   2.   .01
>   3.   .03
>   4.    .1
>   5.    .3
>   6.     1    1.00
> 
> 
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
> 


      


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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