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;
    if (x<0) 
    switch (quad) 
        case 0:
            return cos_32s(x);

        case 1:
            return cos_32s(pi-x);

        case 2:
            return cos_32s(x-pi);

        case 3:
            return cos_32s(twopi-x);


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


def cos_32(x):
    x = math.fmod(x, twopi)
    if (x<0):
    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

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

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 4 Years Ago by pyTony: index

quad = int(x[ quad])

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

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

Edited 4 Years Ago by pyTony: format

This article has been dead for over six months. Start a new discussion instead.