0

Hi, I try to convert the c code into python it actually complie but no result came out

Code in C

#include <stdio.h>
# include <math.h>
double const pi=3.1415926535897932384626433;
double const twopi=2.0*pi;
double const halfpi=pi/2.0;

float cos_32(float x)
{
    int quad;
    x=fmod(x,twopi);
    if (x<0) 
    {
        x=-x;
    }
    quad=int(x/halfpi);
    switch (quad) 
    {
        case 0:
            return cos_32s(x);
            break;

        case 1:
            return cos_32s(pi-x);
            break;

        case 2:
            return cos_32s(x-pi);
            break;

        case 3:
            return cos_32s(twopi-x);
            break;
    }

}


float cos_32s( float x)
{
    float c1= 0.99940307;
    float c2= -0.49558072;
    float c3=0.03679168;
    float x2;

    x2= x * x;
    return (c1 + x2 *(c2 + c3 * x2));
}

Code in Python

pi=3.1415926535897932384626433
twopi=2.0*pi
halfpi=pi/2.0


def cos_32(x):
    x = math.fmod(x, twopi)
    if (x<0):
        x=-x
    quad = x/halfpi
    quad = int(x[ quad])
    if quad == 0:
     return cos_32s(x)
    elif n== 1:
     return cos_32s(pi-x)
    elif n == 2:
     return cos_32s(x-pi)
    elif  n== 3:
     return cos_32s(twopi-x)

def cos_32s(x):
    c1= 0.99940307
    c2= -0.49558072
    c3=0.03679168

    x2= x * x
    return (c1 + x2 *(c2 + c3 * x2))
3
Contributors
5
Replies
6
Views
5 Years
Discussion Span
Last Post by bumsfeld
0

n is undefined. I do not understand your int statement. I would index a tuple of the values instezd of
nested if.. Sorry but mobile posting is not working at the moment correctly.

Could you post the error you are getting and how you are using the function.

Edited by pyTony: index

0

quad = int(x[ quad])

I chnage to quad = int( quad]) Still there no output Di I need print statement basiclly

0

Your logic does not seem to function as I am getting error of almost 2 for
cos(3.14): -1 vs +1

Edited by pyTony: format

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.