Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Working with M_PI

Hello friends,

I've been working with some mathematical symbols and something thats occur to me is that M_PI seems to be just a constant with a set precision.

The problem I am having is as follows:

sin(M_PI * 2)   // returns -2.44929359829471E-16  = ~0  SHOULD RETURN 0
sin(M_PI * 3 / 2)   // returns -1
sin(M_PI)   // returns 1.22464679914735E-16 = ~ 0  SHOULD RETURN 0
sin(M_PI/2)   // returns 1

Is there any way to make this more accurate?

You mean like this:

let π = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223085334468503526193118817101000313783875288658753320838142061717766914730359825349042875546873115562863882353787593751957781857780532171226806613001927876611195909216420198

So of the people over on stack overflow say to import Darwin have you tried that?

Hey Aaron,

Thanks for the response! I've tried using Darwin, and it gives me the same results. According to some other posts on stack overflow, I shouldn't need the import Darwin since Foundation already includes Darwin (c preprocessors, etc.). I've tried another method which involved conversion into degrees and that was also a deadend.

Hmm, ok. I'll keep digging and see what I can find :)