Thread: Why tan(1.5708) in radian returns 16331239353195370.00?

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    12

    Question Why tan(1.5708) in radian returns 16331239353195370.00?

    Hello to all,

    I was doing a simple trig problem in c where i ran across this problem. I was expecting NaN since tan(90 in deg or 1.5708 in rad) is 1/0 . But to my surprise i got 16331239353195370.00 . I admit its been 3 years since i code in c .

    What should i do?

  2. #2
    Registered User
    Join Date
    May 2015
    Posts
    56
    Hello,

    math - tan() in Java returning a strange value - Stack Overflow
    I know this is Java but the reason given holds for 'C'.

    Quote:-
    Well, tan(pi/2) in radians is essentially infinite, isn't it? So you'd expect to get a very large number, wouldn't you? (It's not infinity because pi/2 can't be exactly represented as a double.

    Google search on 16331239353195370 throws up lots of results.

    Regards

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    12
    Quote Originally Posted by mad-hatter View Post
    Hello,

    math - tan() in Java returning a strange value - Stack Overflow
    I know this is Java but the reason given holds for 'C'.

    Quote:-
    Well, tan(pi/2) in radians is essentially infinite, isn't it? So you'd expect to get a very large number, wouldn't you? (It's not infinity because pi/2 can't be exactly represented as a double.

    Google search on 16331239353195370 throws up lots of results.

    Regards
    Thank you for the reply. The given thread does explain the reason.
    However, how should i implement it in c so i get nan or 1/0 or something expected?

  4. #4
    Registered User
    Join Date
    May 2015
    Posts
    56
    Hello,

    The GNU C Library: Infinity and NaN

    or maybe something like:-
    Code:
    if (myNumber == 16331239353195370)
    {
        //do something)
    }
    Regards
    Last edited by mad-hatter; 01-07-2017 at 07:25 AM.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    π/2 radians cannot be expressed as an exact real number, so you're passing an approximation to tan(). This results in a value on the curve somewhere approaching tan(π/2).

    What Every Computer Scientist Should Know About Floating-Point Arithmetic

    One solution would be to check the value, and if it's within close proximity to π/2, refrain from performing the calculation - just as it's good practice to ensure a denominator is not zero before performing division.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by mad-hatter View Post
    or maybe something like:-
    Code:
    if (myNumber == 16331239353195370)
    {
        //do something)
    }
    I would avoid this approach.

    1. Comparing real types with == does not usually work out well.
    2. The actual result in this situation can vary depending on many different factors, so it's not a portable solution.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DialogBox returns -1. GetLastError returns error 1813.
    By Benji Wiebe in forum Windows Programming
    Replies: 14
    Last Post: 09-26-2011, 10:21 AM
  2. I think I almost have this degree to radian conversion.
    By caysonmars in forum C Programming
    Replies: 2
    Last Post: 06-15-2011, 08:54 AM
  3. radian to degree value problem?
    By maifs in forum C Programming
    Replies: 7
    Last Post: 04-02-2009, 04:44 AM
  4. Radian calculator
    By StoneCold in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2007, 09:51 PM
  5. radian to degrees
    By Mr.Pink in forum C++ Programming
    Replies: 8
    Last Post: 02-17-2005, 11:03 PM

Tags for this Thread