Gree Gree -4 years ago 76
Objective-C Question

Not getting Tan (90) Value as undefined in objective-c

While running below statement it prints the wrong value in the console. I got stuck as I am a beginner.

NSLog(@"tan(90)=%f",tan(90*M_PI/180.0);


Output displayed as : tan(90)=16331239353195370.000000

Answer Source

The result is correct. M_PI / 2.0 is a double that is quite close to π/2, but is not precisely π/2 (π cannot be precisely represented by a double). Therefore its tangent is very large, but not infinite.

schmitdt9's link to the tan docs are useful, but the important note is this one:

The function has mathematical poles at π(1/2 + n); however no common floating-point representation is able to represent π/2 exactly, thus there is no value of the argument for which a pole error occurs.

"Pole" means "input for which the function is infinite."

To your question "how i should print tan(90) as undefined in console," the answer is you'll need to special-case it. Normalize whatever you've been passed to 0-360, and check if it's 90 or 270. If so, print infinite, otherwise call tan.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download