Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

odd maths results

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    odd maths results

    Sorry to bother you with something that seems so silly.

    I am calculating the difference between to closing values the example being 15.78 and 15.64 the actual difference is 0.14, but when I run through with debug I get the value of .13999999999999879.

    actual code

    difference = Close[0] - Close[1];

    difference is set to 0 earlier to ensure a known value if this line is not executed.

    All the variables are of type double, since the close values in NinjaTrader are doubles.

    I know I must be doing something reaaaalllllly stupid, but any help would be appreciated. I know that the difference is not much, but there should not be any.

    TIA,

    Graham.

    #2
    Graham, you're welcome - what you see is a correctly calculated double value (16 digits), which the output window would not round - please see this link for more info on this topic - http://www.ninjatrader-support2.com/...ead.php?t=3929

    Comment


      #3
      Bertrand,

      Thanks for the reply. Now I know it wasn't me going completely mad.

      I have found a solution using rounding as follows:

      difference = decimal.Round(((decimal)Close[0] - (decimal)Close[1]), RoundingAccuracy);


      where difference is of type decimal and RoundingAccuracy is a constant that I have set to give the appropriate accuracy. The close prices are forced to be of type decimal using casting.

      This seems to work. It maybe of some use to others in the future.

      Graham.

      Comment


        #4
        Graham, thanks for the solution.

        Basically this is a limitation of all modern computer systems - computers are binary, so it is very difficult to represent an exact value with a bunch of 1s and 0s. If you're looking for some more reading, take a look at the section called Accuracy Problems from this wikipedia entry.
        AustinNinjaTrader Customer Service

        Comment


          #5
          Austin,

          thanks for that, I did have a quick look at the links that Bertrand put on his post and there were getting very deep.

          The solution that I have seems to work for me especially since the OHLC values are only to 2 decimal places in the first instance.

          Graham.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          567 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          330 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          101 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          548 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          548 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X