Announcement

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

  • Why does 0.1 come back differently? (Stata and Mata)

    Click image for larger version

Name:	decimal_binary_explained.png
Views:	1
Size:	110.8 KB
ID:	1787714


    The picture shows two trips from 0.1 to binary and back. Stata's eight-digit trip returns 0.09765625; Mata's 56-digit trip displays 0.100000000000000006. Match Mata's limit to eight digits and the answers match. No data or add-ons are needed. Paste the two blocks into one do-file.

    Stata, eight binary fractional digits:

    Code:
    version 16.1
    clear all
    scalar rem = 0.1
    local bits ""
    forvalues j = 1/8 {
        scalar rem = 2*rem
        local digit = floor(rem)
        scalar rem = rem - `digit'
        local bits "`bits'`digit'"
    }
    scalar answer = 0
    forvalues j = 1/8 {
        local digit = real(substr("`bits'", `j', 1))
        scalar answer = answer + `digit'/2^`j'
    }
    display "Stata, 8 digits:  0.`bits' -> " %21.18f scalar(answer)
    Mata, first 56 digits and then eight:

    Code:
    mata:
    x = 0.1
    e56 = .
    b56 = inbase(2, x, 56, e56)
    printf("Mata, 56 digits: %21.18f\n", frombase(2, b56))
    e8 = .
    b8 = inbase(2, x, 8, e8)
    printf("Mata, 8 digits:  %s -> %21.18f\n", b8, frombase(2, b8))
    end
    The eight-digit fraction is 0.00011001 (base 2) = 25/256. Its exact gap from 1/10 is 3/1280 = 0.00234375. The 56-digit conversion returns the stored double; it does not make mathematical 1/10 exact. Mata's default fractional-digit limit is eight, so what does the result look like if you omit the third argument of inbase()?

    Mata reference: https://www.stata.com/manuals/m-5inbase.pdf

  • #2
    Why isn't sin(pi) zero? A small Stata/Mata experiment

    After the earlier 0.1 experiment, I found myself wondering about pi. If sin(pi) = 0 on paper, would I get exactly zero in Stata and Mata? I tried this little experiment.

    Here is the code, in case anyone feels like trying it too. The two blocks can run together in a do-file, with no data or add-ons needed. They do not clear the current dataset.

    I started with Stata:

    Code:
    version 16.1
    display "double pi: " %21.17f _pi
    display "float pi:  " %21.17f float(_pi)
    display "sin(double pi): " %16.9e sin(_pi)
    display "sin(float pi):  " %16.9e sin(float(_pi))
    display "Looks like zero: " %9.6f sin(_pi)
    display "Exactly zero?    " (sin(_pi)==0)
    display "Within 1e-12?    " (abs(sin(_pi))<1e-12)
    Then I tried the same thing in Mata:

    Code:
    mata:
    p = pi()
    pf = floatround(p)
    printf("double pi: %21.17f\n", p)
    printf("float pi:  %21.17f\n", pf)
    printf("sin(double pi): %16.9e\n", sin(p))
    printf("sin(float pi):  %16.9e\n", sin(pf))
    printf("Looks like zero: %9.6f\n", sin(p))
    printf("Exactly zero?    %g\n", sin(p)==0)
    printf("Within 1e-12?    %g\n", abs(sin(p))<1e-12)
    end
    Both produced the following in Stata/MP 16.1 (last digits may vary by platform):

    Code:
    double pi:       3.14159265358979312
    float pi:        3.14159274101257324
    sin(double pi):  1.224646799e-16
    sin(float pi):  -8.742278000e-08
    Looks like zero: 0.000000
    Exactly zero?    0
    Within 1e-12?    1
    The part that caught my attention was seeing 0.000000 followed by "Exactly zero? 0". I had changed how the answer looked, without changing the answer itself.

    My way of making sense of it is that I never quite handed the computer mathematical pi, which begins 3.14159265358979323846... . Here _pi and pi() supply the same finite double approximation. float() and floatround() round that input further; the sine calculation still uses double precision.

    Writing the stored input as p = pi + delta helped me connect the signs: sin(p) = -sin(delta), approximately -delta for small delta. The double approximation is slightly below pi, while the float approximation is above it. That fits the small positive and larger negative residuals. Function evaluation also has finite precision; the pi values printed above are rounded displays, not their full exact expansions.

    For this example, I used abs(result)<1e-12 to mean "close enough to zero". The double result passes, but the float-rounded input's result does not. That threshold is a choice for this experiment, rather than a general rule. Starting with double preserves more precision; neither printing more digits nor promoting an already rounded float to double restores what was lost.

    So I seem to have three different meanings of zero here: zero on the screen, exact numerical equality to zero, and small enough for the purpose at hand. A small sequel to the 0.1 experiment, but one I found worth trying.

    I would be interested to hear if others have a favourite example of this, or a different way of looking at it.

    References:
    https://www.stata.com/support/faqs/d...oat-data-type/
    https://www.stata.com/manuals/m-5floatround.pdf

    Comment


    • #3
      Can a formula change when clicked? A small display test

      I wonder whether a formula here can show different views of the same numerical example without opening Stata. This first test uses MathJax's action extension. If it is supported here, clicking the expression should switch between two prewritten displays. It does not perform a calculation.

      \[
      \require{action}
      \toggle{\text{Click here: }0.000000}{1.224646799\times10^{-16}}\endtoggle
      \]

      The two views are a six-decimal display and a scientific-notation display of the sin(_pi) result recorded in Stata/MP 16.1. The scientific notation is itself rounded for display.

      If this stays as raw text, reports an unknown command, or renders without changing on click, that is useful to know too. I am testing whether this interaction is available in the forum, including after refreshing the posted page.

      MathJax documentation:
      https://docs.mathjax.org/en/latest/i...ns/action.html
      Last edited by Choonjoo Lee; 25 Oct 2026, 19:33.

      Comment

      Working...
      X