Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pi in Objective C

I keep getting error in my iPhone programing when I try to use pi. I'm trying

    float pNumber = 100*cos(2 * pi * (days/23)); 

But i get errors that say:

_pi, referenced from

_pi$non_lazy_ptr

I saw somewhere on the internet to use M_PI and it compiles but I don't think it gives me the correct calculation.

When I try:

    float pNumber = 100*cos(2 * M_PI * (15746/23)); 

I get 0

Thanks

like image 307
Xcoder Avatar asked Jun 10 '09 21:06

Xcoder


People also ask

What does M_PI mean?

M_PI. Pi, the ratio of a circle's circumference to its diameter. M_PI_2. Pi divided by two.

Does Cmath have pi?

The PI constant is present in the cmath header file. The name of the constant is M_PI.

How do you find pi in Swift?

alt + p is the shortcut (on US-keyboards) that will create the π unicode character.


Video Answer


1 Answers

  • 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 
like image 82
Jonathan Leffler Avatar answered Sep 23 '22 21:09

Jonathan Leffler