Announcement

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

  • Why round function results different

    . di ((87.8-86.8)*95+(90-87.8)*85)/(90-86.8)
    88.125

    . di round(88.125,0.01)
    88.13

    . di round(((87.8-86.8)*95+(90-87.8)*85)/(90-86.8),0.01)
    88.12

  • #2
    The first paragraph running

    Code:
    help precision
    is

    Description

    Stata works in binary. Stata stores data in float precision by default. Stata performs all calculations in double precision. Sometimes the combination results in
    surprises until you think more carefully about what happened.


    Below, note that in binary, the representation of the two values is not the same.

    Code:
    di %16H 88.125
    di %16H `=((87.8-86.8)*95+(90-87.8)*85)/(90-86.8)'
    Res.:

    Code:
    . di %16H 88.125
    4056080000000000
    
    .
    . di %16H `=((87.8-86.8)*95+(90-87.8)*85)/(90-86.8)'
    405607ffffffffff
    Last edited by Andrew Musau; 02 Jul 2024, 02:32.

    Comment


    • #3
      Display to a specified number of decimal places is a problem for a display format, not for a numeric function.

      Andrew Musau makes the main point concisely.

      One of several longer explanations is in my paper on rounding and binning in the Stata Journal. Type say

      Code:
      search binning, sj
      to get a link.

      Comment


      • #4
        Here's the link I had in mind.

        https://www.stata-journal.com/articl...article=dm0095

        Comment

        Working...
        X