![]() ![]() So i'm guessing that's what your algorithm calculates, and the error is caused by the fact that doubles loose precision,Įdit: it looks like you're using the leibniz formula which converges to pi/4 but is infinite, so the fact that you stopped at 1000 explains the errorĪlso Math. If you moved result to a member of the top level class, and both threads have been adding to it, it will have the summed up result already, no need to sum the threads Java Program to Find Area of circle Using Method Overloading 5. lp : while(true)//labelling the while loop. 2 : Find the factorial of the given number. Java Program for Program to find area of a circle 4. 1 : Calculate the volume of cylinder (hint: volume:Pi(rr)h). Find area of the larger circle when radius of the smaller circle and difference in the area is given 3. ![]() Please use a for loop to sum up all numbers. The calculatePi () method works by making repeated passes at calculating the area of the quarter circle, with each subsequent pass using a smaller slice width. Program to calculate area of inner circle which passes through center of outer circle and touches its circumference 2. Threads=new Thread(pc.new Pi(start,end)) parameters types and names, if any return value type, if any the implementation, the body of the method. ![]() If you are meaning to have both threads access your double value at the same time, you could move result out of Pi and put it in PiCalculatorĪnd then to run both threads at the same time, change your loop to int maxThreads = 2 join does is it tell the thread that calls it (in this case the main thread) to stop executing until finishes executing Int maxValuePerThread = 1000 / maxThreads įor(int i = 0 i. PI / 180.0 ) lookupValues i sinValue ( float ) i / 180.0f. The area of a circle is computed with the radius r: A \pi r2 A r2 If you use the unit circle with the radius r 1, you can ignore the radius. also, because you start then immediately join the threads, The code you have posted is equivalent to int maxThreads = 2 if ( curLoop > endLoop ) return null SinTableRange ret new SinTableRange ( ). Computation of Pi Before I present my Java program for the Monte Carlo simulation, I would like to explain some mathematical basics. Regarding question 2, volatile means the jvm shouldn't cache it see here, so it doesn't, but that doesn't matter in your code, because result is a member of the Pi class, so when you make two Pi class instances, and give one to each thread, the threads are using completely separate result variables. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |