CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 23

Threaded View

  1. #14
    Join Date
    Feb 2017
    Posts
    674

    Re: Write a C++ program to compute Sin(x)

    Quote Originally Posted by 2kaud View Post
    For a 'better' implementation consider
    Original loop:

    Code:
    	for (unsigned int nn = 1; nn < n; ++nn) {
    		const unsigned int i =  nn * 2 + 1;
    
    		dem *= i * (i - 1);
    		num *= rad * rad;
    
    		if (nn % 2)
    			res -= num / dem;
    		else
    			res += num / dem;
    	}

    If speed is important the above loop can be changed like this,

    Code:
    	for (unsigned int nn = 3; nn < 2*n; nn += 2) {
    		dem *= nn * (nn - 1);
    		num *= -rad*rad;
    		res += num / dem;
    	}
    There are fewer operations and it's now branch-free.
    Last edited by wolle; February 17th, 2018 at 05:12 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured