Question

In: Computer Science

in java Math.sin(Math.PI / 6) not being equal to 0.5 (but rather equals to 0.49999999999999994. Why...

in java Math.sin(Math.PI / 6) not being equal to 0.5 (but rather equals to 0.49999999999999994. Why is that?

Furthermore, if you try to print the following, you will notice that System.out.println(Math.sin(Math.PI/2)) gives you 1, but System.out.println(Math.cos(Math.PI/2)) does not give you 0, why are these two results not consistent?

Solutions

Expert Solution

Explantion:

This is a result of floating-point precision.

You can get certain number of significant digits. if anything that can't be represented exactly or if you get a approximated value you have to be rounded it to exact value to get a exact value of the result like sin or cos

For example, pi is not a rational number, and so it's impossible to get an exact representation. Since you can't get an exact value of pi, and you aren't going to get exact sin and cos of numbers including pi.

always use exact arithmetic numbers instead of approximate arithmetic value

Math.round(x) returns the value of x rounded to its nearest number

you can run the following java program to get System.out.println(Math.sin(Math.PI/2)) value 1 and System.out.println(Math.cos(Math.PI/2)) value 0

in the below program check the results of the values

// importing lang.Math functions
import java.lang.*; 
class math 
{
        public static void main(String[] args) 
        {

//              System.out.println("Round--Math.PI/6="+(Math.round(Math.PI/6)));
        //      System.out.println("Math.sin(Math.PI/6= "+(Math.sin(Math.round(Math.PI/6))));
                // you have to take the mid value of  0.49999999999999994 to 0.8414709848078965 i.e 0.5
// here exact value is 0.5

                System.out.println(" [after using Math.round]  Math.sin(Math.PI/2= "+(Math.round(Math.sin(Math.round(Math.PI/2)))));
                System.out.println("[after using Math.round] Math.cos(Math.PI/6= "+(Math.round((Math.cos(Math.round(Math.PI/2))))));
        }
}

output Screen


Related Solutions

6. If ?(?) = 0.5 and ?(?|?) = 0.4, are events ? and ? independent? Why...
6. If ?(?) = 0.5 and ?(?|?) = 0.4, are events ? and ? independent? Why or why not? 7. A report on addiction notes that 16% of Americans have the disease of addiction, and 90% of them receive no form of treatment. Determine the probability that a randomly selected American has the disease of addiction and receives no form of treatment.
Suppose that the natural rate of unemployment equals 6%, and the public expect inflation to equal...
Suppose that the natural rate of unemployment equals 6%, and the public expect inflation to equal 4 %, and the coefficient a in PC equation = 0.4. What is the unemployment rate when the actual inflation equals 2%? What if the actual inflation rate equals 10%? During the 1990s, we observed the co-existence of low inflation and low unemployment. One of the arguments given by macroeconomists is that the expected inflation was lower than usual during the 1990s. Suppose now...
compute and print factorial if it is equal to 6, not equal to 6, and square...
compute and print factorial if it is equal to 6, not equal to 6, and square of the factorial, if the input is equal to 6 ..... C programming
6. Why do misfolded proteins tend to aggregate with one another (0.5 pt)?
6. Why do misfolded proteins tend to aggregate with one another (0.5 pt)?
Why is it “harder” to find a significant outcome (all other things being equal) when the...
Why is it “harder” to find a significant outcome (all other things being equal) when the research hypothesis is being tested at the .01 rather than the .05 level of significance?
u equals (5,-11,-6) v equals (-4,8,16+k) w equals (-4,9,5) linearly dependent if and only if k...
u equals (5,-11,-6) v equals (-4,8,16+k) w equals (-4,9,5) linearly dependent if and only if k equals
All else being equal, would you prefer to work in a tall or flat organization? Why?...
All else being equal, would you prefer to work in a tall or flat organization? Why? Which one do you think is best: a narrow span of control, or a wide span of control? How does the concept of span of control relate to tall/flat organizations? Justify your reasoning.
All else being equal, would you prefer to work in a tall or flat organization? Why?...
All else being equal, would you prefer to work in a tall or flat organization? Why? Which one do you think is best: a narrow span of control, or a wide span of control? How does the concept of span of control relate to tall/flat organizations? Justify your reasoning. 300 words
A biased coin (P(head) = p , p is not equal to 0.5) is tossed repeatedly....
A biased coin (P(head) = p , p is not equal to 0.5) is tossed repeatedly. Find the probability that there is a run of r heads in a row before there is a run of s tails, where r and s are positive integers. Express the answer in terms of p, r and s.
Are these rides equal in terms of monetary revenue? Test at α = 0.5 significance level....
Are these rides equal in terms of monetary revenue? Test at α = 0.5 significance level. Happy Cat:                 Average daily revenue = $705.00;   standard deviation = 80;    n = 12 Vicious Mouse:          Average daily revenue = $663.00;   standard deviation = 90;    n = 17 SHOW ALL WORK
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT