开发者

Help with the correct parameters to Math.tan

开发者 https://www.devze.com 2023-01-09 15:02 出处:网络
import java.util.Scanner; public class a { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub
import java.util.Scanner;

public class a {

    /**
    * @param args
    */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner in = new Scanner(System.in);
        System.out.println("enter #");
        long a = in.nextLong();

        long b = (long) Math.toRadians(a);
        long c = (long) Math.tan(b);

        System.out.println(c);

    }
}

Above is my attempt to make Math.tan spit out the correct values of an angle in degrees. I know the method math.tan accepts only angles in radians as a parameter. I can't get correct value for ang开发者_运维问答les of 90, 270... Using a double doesn't yield correct answers.


You will need to use doubles for b and c.

However, note that tan of 90 and 270 degrees is undefined, for example see the graph here:

http://www.intmath.com/Trigonometric-graphs/4_Graphs-tangent-cotangent-secant-cosecant.php


Of course you cannot get a nice value for 90 degrees, because tan is not defined for that angle. This is clear from the definition

tan(x) = sin(x) / cos(x).

If x = 90°, then cos(x) = 0, so you get a division by zero. The same thing applies to 270°, and in fact all odd multiples of 90°. (Now, computers are numerical beasts, and pi/2 radians cannot be represented exactly (in any base), in addition to cos being computed numerically, so instead of NaN or INF you will likely get a huge number, because the denominator is very close to zero, but not necessarily equal to zero, because of numerical approximation.)

Also, long is an integer type. Integer precision might be good enought for your needs when it comes to angles expressed in degrees (0, 1, 2, ..., 358, 359). But in radians, one full lap around a circle corresponds to the angles 0 radians to 2π, where 2π ~ 6.28. Hence b cannot be an integer, and neither can c.


This works just fine:

public class TanDemo
{
    public static void main(String[] args)
    {
        for (int degrees = -89; degrees < 90; ++degrees)
        {
            double radians = Math.toRadians(degrees);
            System.out.println("deg: " + degrees + " rad: " + radians + " tan: " + Math.tan(radians));
        }
    }
}


Use double as a starting point for the input and output of methods like Math.toRadians(double)=>double and Math.tan(double)=>double:

import java.util.Scanner;

public class a {

    /**
    * @param args
    */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner in = new Scanner(System.in);
        System.out.println("enter #");
        double a = in.nextDouble();

        double b = Math.toRadians(a);
        double c = Math.tan(b);

        System.out.println(c);

    }
}

On a domain note – as Saxon Druce pointed out – the tangent of n*90° (n*π/2 rads) is undefined. Correct answers at exactly n*π/2 for n is an odd whole number are not possible. More specifically tan(x) approaches as x increases towards n*π/2. It is also true that tan(X) approaches -∞ as x decreases towards n*π/2.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号