[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Mathematical precision question

From   "Lektzian, David" <[email protected]>
To   <[email protected]>
Subject   st: Mathematical precision question
Date   Tue, 29 Jan 2008 15:20:14 -0600

Dear Statalist,

I have a simple problem that I am hoping somebody can help me with.  I
am trying to generate a variable that multiplies three other variables
by quantities and then adds them.  Specifically, here is what I am
trying to do:
Var1 = 1972
Var2 = 2
Var3 = 20

I want to generate the variable ddy as follows:
.gen ddy = Var1*1000000 + Var2*1000 + Var3

The correct answer, both on my calculator and in excel is: 1972002020

So, what does Stata come up with?  Formatting the answer into the 10
digits necessary to view the answer:
.Format ddy %10.0f

Reveals that  Stata's answer is:

I'm sure this must be something to do with precision, but I don't know
how to fix the problem.  When I run this repeatedly, Stata somewhat
INconsistently gets this operation wrong every time.  In the first
example Stata was over by 28.  In this next example Stata is under by
Var1   Var2   Var3
1973   002     020 = 1973001984 according to Stata when in reality it
equals 1973002020

Here Stata is closer only going over by 7.
Var1   Var2   Var3
2002   002     055 = 2002002048 according to Stata when in reality it
equals 2002002055

I'm sure this problem must have something to do with precision, but I
need these answers to be exact.  Can somebody help explain to me how to
get Stata to perform these mathematical operations correctly?

*As a note, I will mention that I am using these variables as ID
variables which is why I want them exact.  There might be a better way
to create ID variables, but I'm a bit of a novice with Stata syntax.

Thank you

*   For searches and help try:

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