Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# tan(90) and sin(90)/cos(90)

Rama Raju
Greenhorn
Posts: 12
Hi all,
In Mathematics, we know tan(90) and sin(90)/cos(90) are infinity.
But When I write same in java. I am getting
System.out.println(Math.sin(90)/Math.cos(90)); as -1.9952004122082418
System.out.println(Math.tan(90)) as
1.995200412208242.

Suma Narayan
Ranch Hand
Posts: 136
Hi Rama,
Yes in pure mathematics tan(90) is infinity and since sin(90) is 1.0000 and cos(90) is 0.0000, the tan(90) is infinity. Consider the following e.g
public class Matharg
{
public static void main(String[] args)
{

System.out.println("Value of tan(0) is " + Math.tan(Math.toRadians(0)));
System.out.println("Value of sin(90) is " + Math.sin(Math.toRadians(90)));
System.out.println("Value of cos(90) is " + Math.cos(Math.toRadians(90)));
}
}
All the above functions take the arguments of type double and return double.
If you run this, it gives the following result
Value of tan(0) is 0
Value of sin(90) is 1.0
Value of cos(90) is 6.123233995736766e-17
Value of tan(90) is 1.633123935319537e16
Note the toRadians is used to convert degree to radians as all sin,cos and tan take radian arguments.
The value of cos(90) is not 0.0000 and hence tan(90) is not infinity. In pure Mathematics, the value 6.123233995736766e-17 will be
.000000000000000006123.....If I remember correctly, we consider only 4 decimal places for all trignometric functions and also for Logarithm functions. But in Java since double provides such a wide range of values, cos(90) is not 0.0000 and hence we don't get the same result.
This is what I understand.
I will wait for others to answer this question. May be my understanding is not right.