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 7093289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:25:14+00:00 2026-05-28T08:25:14+00:00

I created this function CalculateCos : int Factorial (long int n) { long int

  • 0

I created this function CalculateCos:

int Factorial (long int n)
{
    long int r = 1;
    for (int i = 2; i<=n; i++)
    {
        r = r*i;    
    }   

    return r;
}

float CalculateVariable(int CVnumber, int CVloopCounter)
{
    float CVresult = 0;
    CVresult = pow(CVnumber, (CVloopCounter*2)) / (long int)Factorial(CVnumber*2);

    return CVresult;
}

float CalculateCos(int number)
{
    float result = 1;
    int loopCounter = 1;
    int minusOrPlus = 1;
    while(loopCounter <= precision && loopCounter <= 8)
    {
        if(!minusOrPlus)
        {
            result = result - CalculateVariable(number, loopCounter);
            printf("%f\n", result);
            minusOrPlus = 1;
        }
        else
        {
            result = result + CalculateVariable(number, loopCounter);
            printf("%f\n", result);
            minusOrPlus = 0;
        }
        loopCounter++;
      }
      return result;
}

The reason why I printf after the subtraction or adding, is because it gives me strange output, like:

Enter a number, for the cos function
6
1.000000
0.999997
1.000095
0.996588
1.122822
-3.421593
160.177368
-5729.385254
Result is: -5729.3852539
Official function result is:  0.9601703

Can you help me to get correct results on this?

UPDATE:

Now my solution is:

float CalculateCos(float number)
{
    float result = 0;
    float step = 1;
    int loopCounter = 1;

    while(loopCounter <= 5)
    {
        step = step * (-number) * number / (((2*loopCounter)-1)*((2*loopCounter)-2));
        result += step;
        loopCounter++;
    }

    return result;
}
  • 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-28T08:25:15+00:00Added an answer on May 28, 2026 at 8:25 am

    Current problem:

    since your Factorial function returns int and you casts it to long int, its result is going to overflow even before the input goes to 16 in your case (14! > max_int).

    You’re calculating cos using Taylor series:

    cos(x) = 1 – x2/2! + x4/4! – x6/6!
    + …

    I’m not going to write code. But there are some things wrong in your program, which can be fixed easily:

    1. The input is in radian, so number should be a float.
    2. Calculating each step of Taylor series using exponentiation and factorial separately leads to overflow very soon. The correct way is maintaining a float variable: step = 1 at first and in kth loop iteration step = step * (- x) * x / ((2*k-1)*(2*k)). In this way, you simply add step to result in the loop and don’t need minusOrPlus anymore.
    3. The number of loop iterations is bounded by 8 which is too small, so the result could be not precise enough.
    4. I don’t see you use precision variable anywhere. It could be used to check precision of the result. For example, when abs(step) < precision, we’re going to terminate the loop.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created this function: nDone<- function (under,strike,ttoe,vol,rf,dy) { return(pnorm(((log(under/strike) + (rf-dy+(vol^2)/2)*ttoe)/(vol*(ttoe^0.5))))) } nDone(90,100,3,0.17,0.05,0) [1]
i have created a module with this among others this function in it: <?php
I have dialog created like this $('#add_error').click(function(e) { $('<div>') .load('/someaction/format/html/') .dialog({ title: 'Some title',
I've got a javascript function I created. Somewhere in that function, this line is
I have a loop created with each, check this example: $('.foo').each(function(i){ //do stuff });
languages used: html, javascript/jquery, and php 5.2 So I created this function that onclick
I am using JQueryUI Dialog and created this function below: <script> $(document).ready(function() { $('#dialog').dialog({
I created this function to remove vbCrLf from the values of properties in a
I am rather new at PL/SQL (esp. using Dynamic SQL), I created this function,
I created this function and it works on small strings, but for longer strings

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.