pi in Objective C

  • The integer division probably needs to be coerced into a floating point one (cast one of the numbers to a double – or use the notation 23.0 to indicate that you want a floating point division).
  • Try printing out M_PI and see what it says (printf("M_PI = %16.9g\n", M_PI); in C).
  • Did you include the declaration for cos()? If not, it may be interpreted as a function returning an integer (#include <math.h> perhaps).

Example code (tested in C on Solaris 10 SPARC with GCC 4.3.3):

#include <math.h>
#include <stdio.h>

int main(void)
{
    float pNumber = 100*cos(2 * M_PI * (15746/23));
    printf("M_PI = %16.9g\n", M_PI);
    printf("pNum = %16.9g\n", pNumber);
    pNumber = 100*cos(2 * M_PI * (15746/23.0));
    printf("pNum = %16.9g\n", pNumber);
    return 0;
}

Example output:

M_PI =       3.14159265
pNum =              100
pNum =      -77.5711288

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)