Statalist


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

Re: st: Precision issues with double storage type (??)


From   Keith Dear <keith.dear@anu.edu.au>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Precision issues with double storage type (??)
Date   Tue, 07 Aug 2007 09:31:38 +1000

Double precision is not infinite precision. Here are the two numbers you are comparing:

. di %24.18f .51
0.510000000000000010

. di %24.18f 69-68.49
0.510000000000005120

. di .51==(69-68.49)
0

As you can see, and as "==" is reporting, they are not quite the same. One answer, as Maarten says, is to count cents not dollars so that you can use integer variables.

. di %24.18f 51
51.000000000000000000

. di %24.18f 6900-6849
51.000000000000000000

. di 51==(6900-6849)
1

kd

At 08:36 AM 7/08/2007, you wrote:

<snip>


****Here is my second problem:

Then I type: gen FLAG_REFUND=1 if REFUND_C!=REFUND_B,

expecting Stata to produce two missing values. But Stata apparently thinks that for both observations the values of REFUND_B and REFUND_C are different (Please see the log file below).

I don't understand why this is happening because, after all, all the variables involved in this operation are stored in -double- and computations are conducted with -double- precision...

If anyone on this list could advise me on this matter, I would appreciate it. Thank you.

Hiroshi Maeda


My demonstration begins here =======================================

. clear;

. set obs 2;
obs was 0, now 2

. gen ID=_n;

. gen float DEPOSIT_A=.;
(2 missing values generated)

. gen float PAYMENT_A=.;
(2 missing values generated)

. gen float REFUND_A=.;
(2 missing values generated)

. replace DEPOSIT_A=61.42 if ID==1;
(1 real change made)

. replace PAYMENT_A=21.30 if ID==1;
(1 real change made)

. replace REFUND_A =40.12 if ID==1;
(1 real change made)

. replace DEPOSIT_A=69.00 if ID==2;
(1 real change made)

. replace PAYMENT_A=68.49 if ID==2;
(1 real change made)

. replace REFUND_A = .51 if ID==2;
(1 real change made)

. format DEPOSIT_A %9.2f;

. format PAYMENT_A %9.2f;

. format REFUND_A %9.2f;

. desc;

Contains data
obs: 2
vars: 4
size: 48 (99.9% of memory free)
-------------------------------------------------------------------------------
storage display value
variable name type format label variable label
-------------------------------------------------------------------------------
ID double %10.0g
DEPOSIT_A float %9.2f
PAYMENT_A float %9.2f
REFUND_A float %9.2f
-------------------------------------------------------------------------------
Sorted by:
Note: dataset has changed since last saved

. list, nodisplay noobs compress sepby(ID);

+----------------------------+
| ID DEP~A PAY~A REF~A |
|----------------------------|
| 1 61.42 21.30 40.12 |
|----------------------------|
| 2 69.00 68.49 0.51 |
+----------------------------+

. count if DEPOSIT_A==61.42;
0

. count if DEPOSIT_A==float(61.42)
> /*=> This shows that I have read [U] 13.10 Precision and Problems therein*/;
1

. count if PAYMENT_A==21.30;
0

. count if PAYMENT_A==float(21.30);
1

. count if REFUND_A==40.12;
0

. count if REFUND_A==float(40.12);
1

. count if DEPOSIT_A==69.00;
1

. count if DEPOSIT_A==float(69.00);
1

. count if PAYMENT_A==68.49;
0

. count if PAYMENT_A==float(68.49);
1

. count if REFUND_A==.51;
0

. count if REFUND_A==float(.51);
1

. gen CHECK_1=1 if DEPOSIT_A==PAYMENT_A+REFUND_A;
(1 missing value generated)

. gen CHECK_2=1 if float(DEPOSIT_A)==float(PAYMENT_A)+float(REFUND_A);
(1 missing value generated)

. list, nodisplay noobs compress sepby(ID)
> /*Problem 1: I don't understand why CHECK_2 is missing for observation 2*/;

+--------------------------------------------+
| ID DEP~A PAY~A REF~A CHE~1 CHE~2 |
|--------------------------------------------|
| 1 61.42 21.30 40.12 1 1 |
|--------------------------------------------|
| 2 69.00 68.49 0.51 . . |
+--------------------------------------------+

. gen DEPOSIT_S=string(DEPOSIT_A, "%9.2f");

. gen PAYMENT_S=string(PAYMENT_A, "%9.2f");

. gen REFUND_S =string(REFUND_A, "%9.2f");

. gen double DEPOSIT_B=real(DEPOSIT_S);

. gen double PAYMENT_B=real(PAYMENT_S);

. gen double REFUND_B =real(REFUND_S);

. format DEPOSIT_B %9.2f;

. format PAYMENT_B %9.2f;

. format REFUND_B %9.2f;

. drop *_S;

. desc;

Contains data
obs: 2
vars: 9
size: 128 (99.9% of memory free)
-------------------------------------------------------------------------------
storage display value
variable name type format label variable label
-------------------------------------------------------------------------------
ID double %10.0g
DEPOSIT_A float %9.2f
PAYMENT_A float %9.2f
REFUND_A float %9.2f
CHECK_1 double %10.0g
CHECK_2 double %10.0g
DEPOSIT_B double %9.2f
PAYMENT_B double %9.2f
REFUND_B double %9.2f
-------------------------------------------------------------------------------
Sorted by:
Note: dataset has changed since last saved

. gen CHECK_3=1 if DEPOSIT_B==PAYMENT_B+REFUND_B;

. list DEPOSIT_A-REFUND_A DEPOSIT_B-REFUND_B CHECK_3, nodisplay noobs compress sepby(ID);

+-------------------------------------------------------+
| DEP~A PAY~A REF~A DEP~B PAY~B REF~B CHE~3 |
|-------------------------------------------------------|
| 61.42 21.30 40.12 61.42 21.30 40.12 1 |
|-------------------------------------------------------|
| 69.00 68.49 0.51 69.00 68.49 0.51 1 |
+-------------------------------------------------------+

. gen REFUND_C=DEPOSIT_B-PAYMENT_B;

. format REFUND_C %9.2f;

. gen FLAG_REFUND=1 if REFUND_C!=REFUND_B;

. list DEPOSIT_A-REFUND_A DEPOSIT_B-REFUND_B FLAG_REFUND, nodisplay noobs compress sepby(ID);

+-------------------------------------------------------+
| DEP~A PAY~A REF~A DEP~B PAY~B REF~B FLA~D |
|-------------------------------------------------------|
| 61.42 21.30 40.12 61.42 21.30 40.12 1 |
|-------------------------------------------------------|
| 69.00 68.49 0.51 69.00 68.49 0.51 1 |
+-------------------------------------------------------+

. /*Problem 2: I don't understand why FLAG_REFUND has flagged observations 1 & 2*/;



--
Hiroshi Maeda
University of Illinois at Chicago
hmaeda1@uic.edu

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



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