• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Double difference strange results

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

  • ninjo
    started a topic Double difference strange results

    Double difference strange results

    Hello,

    I am trying to obtain the difference between two doubles and NT8 return stranges results:


    Code:
        
    double theDif =  0.709 - 0.7091;
    Print("theDif--> "+ theDif);

    And the Output result is:

    theDif--> -9,9999999999989E-05


    The result would be -0,0001



    What I am doing bad? Thank you.

    Last edited by ninjo; 03-11-2019, 10:44 AM.

  • koganam
    replied
    Originally posted by ninjo View Post
    I solved it with Math.Round function:

    Code:
    double theDif = Math.Round( 0.709 - 0.7091, 4);

    The problem is that I want to claculate the number of ticks between two chart prices.

    Code:
    Math.Round( 0.709 - 0.7091, 4) / TickSize
    Its works and return: -1


    In this case the price have 4 decimals, but how could I set for all markets??

    Thank you
    Why not just use the NS provided function, RoundToTickSize()?

    ref: https://ninjatrader.com/support/help...toticksize.htm

    Leave a comment:


  • NinjaTrader_Jim
    replied
    Hello ninjo,

    Thanks for your post.

    This mathematical behavior is expected with floating point arithmetic. We have some various tips on working with floating point arithmetic in our Tip page below.

    Floating point math - https://ninjatrader.com/support/help...arithmetic.htm

    Rounding the result to your expectation is viable. For being able to identify the number of digits needed for the rounding precision, you could follow an approach similar to those suggested in the publicly available resource below to get the number of significant digits in TickSize.

    Finding the number of places after the decimal point of a number - https://stackoverflow.com/questions/...nt-of-a-double

    Please let us know if we can be of further assistance.

    Leave a comment:


  • ninjo
    replied
    I solved it with Math.Round function:

    Code:
    double theDif =  Math.Round( 0.709 - 0.7091, 4);

    The problem is that I want to claculate the number of ticks between two chart prices.

    Code:
    Math.Round( 0.709 - 0.7091, 4) / TickSize
    Its works and return: -1


    In this case the price have 4 decimals, but how could I set for all markets??

    Thank you

    Leave a comment:

Latest Posts

Collapse

Topics Statistics Last Post
Started by timcjpfx, Today, 03:52 AM
0 responses
1 view
0 likes
Last Post timcjpfx  
Started by tonynt, Today, 01:42 AM
0 responses
7 views
0 likes
Last Post tonynt
by tonynt
 
Started by NT-Roland, Yesterday, 04:36 PM
0 responses
3 views
0 likes
Last Post NT-Roland  
Started by Newtrader101, Yesterday, 03:40 PM
2 responses
13 views
0 likes
Last Post Newtrader101  
Started by Branpo, Yesterday, 02:58 PM
2 responses
15 views
0 likes
Last Post Branpo
by Branpo
 
Working...
X