Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6590947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:21:48+00:00 2026-05-25T17:21:48+00:00

We know that sin(x)=x-x^3/3!+x^5/5!-x^7/7!+x^9/9! and so on. I have written this code: #include <iostream>

  • 0

We know that
sin(x)=x-x^3/3!+x^5/5!-x^7/7!+x^9/9! and so on. I have written this code:

#include <iostream>
#include <math.h>
using namespace std;

const int m=19;

int factorial(int n) {

    if (n==0){ return 1;}
    return n*factorial(n-1);
}

int main() {
   float x;
   cin >> x;

   float sum=0;
   int k=1;
   for (int i=1;i<=m;i+=2) {
      sum+=(k*(powf(x,i)/factorial(i)));
      k=k*(-1);
   }

   cout<<"series sum is equal :"<<sum<<endl;
   return 0;
}

One problem is that when I enter x=3 it gives me -10.9136, but I know that values range of sin(x) is [-1, 1] what is problem? Please help me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:21:49+00:00Added an answer on May 25, 2026 at 5:21 pm

    The problem is that you’re running out of precision due to destructive cancellation.

    You have an alternating series where some of the terms get very large. But those terms cancel each other out to a small result. Since float has limited precision, your round off error is larger than your final value.

    You can “reduce” the problem by using double-precision. But it won’t go away. Standard implementations of sin/cos involve taking the modulo of the argument by 2 pi to make it small.

    EDIT :

    I found the other problem. You have an integer overflow in your factorial function when i = 19.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that this line of code will make the cell text-wrap: $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setWrapText(true); 'D1'
I have the following code doing Sin/Cos function using a pre-calculated memory table. in
I know that this sort of question has been asked here before, but still
I know that Java have its own garbage collection, but sometimes I want to
I know that immutable objects always have the same state, the state in which
I know that in order to download a cookie associated with an account using
I have below a code that will plot a sphere, it's proportions are defined
I have function source stored in a string eg.: var source=(function(){return Math.sin(x);}); I want
I have a C++ program that is doing a lot of math (mostly calls
We know that modern processors execute instructions such as cosine and sin directly on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.