Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# Re: st: Float Precision

 From Jamie Madden To statalist@hsphsun2.harvard.edu Subject Re: st: Float Precision Date Wed, 22 Aug 2012 14:12:43 +0100

```Many thanks for your informative reply.

Jamie

On Wed, Aug 22, 2012 at 10:57 AM, Nick Cox <njcoxstata@gmail.com> wrote:
> You say that you understand the reasoning, but when you write
>
> the value appears to be 3.58 when in reality when you click on it, it
> is 3.579999
>
> you miss, or mis-represent, the issue. If there is a reality here it
> is what Stata is holding your value as, namely, a binary approximation
> to 3.58. If it's being held in a -float-, then what is being held is
> _closer to_ the binary equivalent of 3.5799999 than it is to the
> binary equivalent of 3.58, but what is held is not, and cannot be,
> equal to 3.5799999 any more than it is, or can be, equal to 3.58.
>
> . set obs 1
> obs was 0, now 1
>
> . gen foo = 3.58
>
> . di %21x foo[1]
> +1.ca3d700000000X+001
>
> . di %21x 3.58
> +1.ca3d70a3d70a4X+001
>
> . di %21x 3.5799999
> +1.ca3d6fcd17756X+001
>
> (Note to the picky: When I try Jamie's example, I see 99999 not 9999.
> Presumably the difference is a typo.)
>
>
> That said, if you find Bill's blog explanation unclear, then you need
> to raise specific questions about it. Bill's blog entry was written
> because many postings like this have failed to convince some or
> sufficiently to clarify what is at stake, and another such exchange is
> unlikely to help much.
>
> The tone of your posting is that if this is all true, then it is a
> confounded nuisance and perhaps even smacks of bad language design.
> (I'm imputing here.) My personal experience in 21 years of very
> intensive usage of Stata is that I am bitten by precision issues
> occasionally and need to spell out -float()- probably less than once a
> year. In practice, things like
>
> gen newvariable=1 if gear_ratio>=float(3.58)
>
> don't arise at all frequently in my experience. As Maarten also
> suggests, when you want exact arithmetic, often it just means that you
> need to recast the problem to integers. Naturally, that is, as said,
> just my experience.
>
> The answer to your last question is that you can -set type double-,
> but I'd recommend against it. You end up using vastly more memory, all
> for the sake of not being inconvenienced by a syntax quirk that bites
> only very occasionally.
>
> Nick
>
> On Wed, Aug 22, 2012 at 10:16 AM, Jamie Madden <jamiem1234@gmail.com> wrote:
>
>> I am having difficultly understanding that I need to write float in
>> front of numbers when I am creating new variables in order to
>> compensate for precision issues. I have read "the penultimate guide to
>> precision" by William Gould but it does not seem to answer my problem.
>>
>>
>> For example if we look at (using Stata 12)
>>
>>   sysuse auto, clear
>>
>> at the first row and column "gear_ratio", the value appears to be 3.58
>> when in reality when you click on it, it is 3.579999. I understand the
>> reasoning behind this and understand if I want to create a variable
>> for example that is greater than or equal to 3.58 I should type
>>
>> gen newvariable=1 if gear_ratio>=float(3.58)
>>
>> Surely there is no need to type float for every variable that you
>> create in a dataset? Is there a way to set this up for your whole
>> dataset? Or change the default setting?
>>
> *
> *   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/
```