Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • why the values are different?

    The values of b are calculated, it should be exactly the same with a. How to exucute some function to make b equal a? By the way, the function of round doesn't work.
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double(a b)
    89.9 89.89999866000001
    70.1 70.10000038
    76.1 76.10000038
    85.3 85.30000000000001
    83.3 83.30000000000001
    
    end
    gen c = a == b

  • #2
    Fred:
    see -help precision-.
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment


    • #3
      Originally posted by Carlo Lazzaro View Post
      Fred:
      see -help precision-.
      Thanks Carlo, I know it's the precision problem. However, how to make the value b equal to a, since b is already double type?

      Comment


      • #4
        Fred:
        Code:
        . clonevar c=a
        
        . drop b
        
        . rename c b
        
        . list
        
             +-------------+
             |    a      b |
             |-------------|
          1. | 89.9   89.9 |
          2. | 70.1   70.1 |
          3. | 76.1   76.1 |
          4. | 85.3   85.3 |
          5. | 83.3   83.3 |
             +-------------+
        
        .
        Kind regards,
        Carlo
        (StataNow 18.5)

        Comment


        • #5
          No, that is not what I want. Since the b is calculated by other functions, I want to compare a with b, therefore, I shouldn't clone a for b.

          Comment


          • #6
            Fred:
            Code:
            . format b %12.1f
            
            . list
            
                 +-------------+
                 |    a      b |
                 |-------------|
              1. | 89.9   89.9 |
              2. | 70.1   70.1 |
              3. | 76.1   76.1 |
              4. | 85.3   85.3 |
              5. | 83.3   83.3 |
                 +-------------+
            
            .
            See -help format- for further details.
            Kind regards,
            Carlo
            (StataNow 18.5)

            Comment


            • #7
              Well, this is not format issue, either. I need to compare whether a equals b, therefore,
              Code:
              gen c= a==b
              is necessary for my do files.

              a equals to b, this is the fact that I know; however, for Stata, when I compare a with b using "a==b", this is a negative answer.

              Comment


              • #8
                I can't follow this.

                If the values should be the same, then why are they different?

                Perhaps you should be calculating differences and then deciding whether they matter.

                The matter is confused because if you are comparing a float with a double then you can expect small differences for that reason alone and if you have different display formats then you can expect to see small differences for that reason alone.

                The report that round() doesn't work is unclear, but can be answered. Applying round(foo, 0.1) or round(foo, 0.01) is usually expecting Stata to do what is impossible, force all decimals to be held exactly in binary.

                Your concurrent thread raises related issues, If you open two or more threads at about the same time, you need a really good reason to do that, which would be that the questions are quite different. Otherwise a cross-reference to the other thread is essential.

                https://www.statalist.org/forums/for...her-than-float

                Comment


                • #9
                  Hi Nick, here is the explanation why a and b should be the same.
                  The value of "a" is the sum of some variables (a1 a2 a3), stored in an excel file. After importing the data from excel, I calulated "b" using "rowtotal (a1 a2 a3)", therefore, "a" from excel and "b" from stata should be the same. However, due to the precision issue, "a" and "b" are different right now.

                  How to make "b" equal to "a" after I import from excel? Thanks!
                  By the way, in another thread, the title of that thread is not clear, I want delete that thread, however, I didn't find how to deleteit.

                  Comment


                  • #10
                    Fred:
                    Code:
                    . input double(a b)
                    
                                  a           b
                      1.
                    . 89.9 89.89999866000001
                      2.
                    . 70.1 70.10000038
                      3.
                    . 76.1 76.10000038
                      4.
                    . 85.3 85.30000000000001
                      5.
                    . 83.3 83.30000000000001
                      6.
                    .
                    .
                    . end
                    
                    . sum
                    
                        Variable |        Obs        Mean    Std. dev.       Min        Max
                    -------------+---------------------------------------------------------
                               a |          5       80.94    7.838877       70.1       89.9
                               b |          5       80.94    7.838877       70.1       89.9
                    
                    . sum, d
                    
                                                  a
                    -------------------------------------------------------------
                          Percentiles      Smallest
                     1%         70.1           70.1
                     5%         70.1           76.1
                    10%         70.1           83.3       Obs                   5
                    25%         76.1           85.3       Sum of wgt.           5
                    
                    50%         83.3                      Mean              80.94
                                            Largest       Std. dev.      7.838877
                    75%         85.3           76.1
                    90%         89.9           83.3       Variance         61.448
                    95%         89.9           85.3       Skewness      -.3317959
                    99%         89.9           89.9       Kurtosis       1.754059
                    
                                                  b
                    -------------------------------------------------------------
                          Percentiles      Smallest
                     1%         70.1           70.1
                     5%         70.1           76.1
                    10%         70.1           83.3       Obs                   5
                    25%         76.1           85.3       Sum of wgt.           5
                    
                    50%         83.3                      Mean              80.94
                                            Largest       Std. dev.      7.838877
                    75%         85.3           76.1
                    90%         89.9           83.3       Variance       61.44799
                    95%         89.9           85.3       Skewness       -.331796
                    99%         89.9           89.9       Kurtosis       1.754059
                    
                    .
                    The only differences that I'm able to spot between -a- and -b- here are negligible matters of rounded decimal digits for variance (with no apparent bearing on std. dev., though) and skewness.
                    Kind regards,
                    Carlo
                    (StataNow 18.5)

                    Comment


                    • #11
                      Deleting threads. After hundreds of posts here you should be familiar with the FAQ Advice. https://www.statalist.org/forums/help#closure explains that you cannot delete posts. You don't own threads just because you start them.

                      Otherwise, sorry, but I have other things to do now, but someone else may address your question given the new context.

                      Comment


                      • #12
                        Carlo Lazzaro Thanks, Carlo, again! The differences of a and b are negligible, however, I just want to fix "b" equal to "a" because in my do files, I need to compare a with b, as I explained.
                        For your information, I will generate c if a==b, the value of c I want is 1. However, in my case, all "c" equals 0.

                        The "c" here is just an emample, in the actual senario, I generare more meaningful variables.

                        Comment


                        • #13
                          Fred:
                          something similar happened to me years ago when, just out of curiosity, I copied from a spreadsheet some numeric variables and pasted them into a Stata .pdf file.
                          When compared with the same variables created in Stata, differences were immaterial, but I was not able to find a way to match those values exactly.
                          Kind regards,
                          Carlo
                          (StataNow 18.5)

                          Comment


                          • #14
                            I see, thaks all the way, Carlo!

                            Comment


                            • #15
                              I think Carlo's advice is quite correct: you will not be able to force a == b. What I can add is that, as Carlo has already pointed out, you are demanding exact equality on floating-point calculation results, which is simply impossible. But you can verify close to equality. Instead of -gen c = a == b-, how about -gen c = float(a) == float(b)-? This will tell you if the variables agree to within floating point precision, which is going to be good enough for nearly all practical purposes anyway.

                              Comment

                              Working...
                              X