class character1{
public static void main (String args[]){
  float a,b;
  a=Integer.parseInt (args[0]);
  System.out.println (b);

the above program isn't working when entering angle other than 1 , why ?

6 Years
Discussion Span
Last Post by tesuji

Check the arguments you passing to the acos(double) method.
If the absolute value is > 1 or NaN, it will return NaN.
See the API doc of Math class.


Add this to your code to see what you are passing to acos():

System.out.println("acos arg=" + a*(180/3.14)); // show the arg


adding the argument giving me following output when entering arccos = 2

acos arg=114.64968152866241

is there something wrong in a program ??


I was trying to show you that your arg to acos() was > 1 which will return a NaN value.
For example this code passes args to acos from .1 to 1.1:

for(double a = .1; a < 1.1; a+= .1) {
           double b = Math.acos(a);
          System.out.println ("a=" + a + ",  b=" + b);
            a=0.1,  b=1.4706289056333368
            a=0.2,  b=1.369438406004566
            a=0.30000000000000004,  b=1.266103672779499
            a=0.4,  b=1.1592794807274085
            a=0.5,  b=1.0471975511965979
            a=0.6,  b=0.9272952180016123
            a=0.7,  b=0.7953988301841436
            a=0.7999999999999999,  b=0.6435011087932845
            a=0.8999999999999999,  b=0.45102681179626264
            a=0.9999999999999999,  b=1.4901161193847656E-8
            a=1.0999999999999999,  b=NaN

Hey i don't understand what you are trying to say ??
why the value becomes Nan at 1.0999999 and why yu didn't multiply it by 3.14/180 ?



The cos-function y = cos(x) gives a value y which is from [-1 ... +1]. Its parameter x must be radians, that is for example -2*pi ... +2*pi, also values > 2*pi are allowed.

Therefore the parameter y of the restricted inverse function x = acos(y) must always be from [-1 ... +1]. x is then from restricted domain [pi .. 0] because cos is periodic.

As for your program:

>> a=Integer.parseInt (args[0]);
a can be a parameter for acos function if a is from [-1..+1]. If not, NaN would be the result.

>> b=(float)Math.acos(a*(180/3.14));
should be: b = Math.acos(a);
The result b is radians. You can convert it into degrees: b_degrees = b * 180 / pi or b_degrees = Math.acos(a) * 180 / pi, where pi = 3.14159265358979 or 4*atan(1).

-- tesu

Edited by tesuji: n/a

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.