Weird java rounding issue...


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Weird java rounding issue...

  1. #1
    Join Date
    Apr 2006
    Posts
    20

    Weird java rounding issue...

    I'm not going to go into length about what the program does, as thats not the fix i want. The fix I want is that it only ever outputs is 3.0... it seems to round everything up and down, despite me never telling it to.

    Any ideas?

    Code:
    import java.util.*;
    import java.math.*;
    
    public class pi{
    	public static void main (String[]args){
    	
    	double pi=0;
    	double fraction=0;
    	String mathfunction;
    	int funccounter=0;
    	int denominator=0;
    	int loopcounter=0;
    	
    	pi=4;
    	denominator=1;
    	while (pi!=3.1415926)
    	{
    		if (loopcounter==0)
    		{
    			fraction=1-1/3;
    		}
    		else
    		{
    			fraction=1/denominator;
    		}
    		denominator=denominator+2;
    		if (funccounter % 2==0)
    		{
    			pi=pi-fraction;
    		}
    		else
    		{
    			pi=pi+fraction;
    		
    		}
    		System.out.println(pi);
    		if ((pi==3.14||pi==3.14159||pi==3.1415926))
    		{
    			System.out.println("Pi was equal to "+pi+" on the "+ loopcounter+ " st/nd/th loop");
    		}
    		loopcounter++;	
    		funccounter++;
    		
    		
    	}
    	}
    		
    }

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    have you looked at any of the output formatting classes for how to set the output to display all of your "decimal" values?

  3. #3
    Join Date
    Oct 2006
    Posts
    5
    Try this: -
    ~~~~~~~~~~~~~
    import java.util.*;
    import java.math.*;

    public class pi{
    public static void main (String[]args){

    double pi=0;
    double fraction=0;
    String mathfunction;
    int funccounter=0;
    int denominator=0;
    int loopcounter=0;

    pi=4;
    denominator=1;
    while (pi!=3.1415926)
    {
    if (loopcounter==0)
    {
    fraction=1.0-1.0/3.0;
    }
    else
    {
    fraction=1.0/denominator;
    }
    denominator=denominator+2;
    if (funccounter % 2==0)
    {
    pi=pi-fraction;
    }
    else
    {
    pi=pi+fraction;

    }
    System.out.println(pi);
    if ((pi==3.14||pi==3.14159||pi==3.1415926))
    {
    System.out.println("Pi was equal to "+pi+" on the "+ loopcounter+ " st/nd/th loop");
    }
    loopcounter++;
    funccounter++;


    }
    }

    }

Similar Threads

  1. Re: C++ vs Java
    By Michael in forum Java
    Replies: 2
    Last Post: 12-31-2002, 05:55 PM
  2. learning c# very confusing.
    By Mike Tsakiris in forum .NET
    Replies: 11
    Last Post: 10-04-2002, 06:32 PM
  3. JAVADOTNET !!!
    By Dharmesh in forum .NET
    Replies: 4
    Last Post: 10-01-2001, 04:47 PM
  4. Java...how does it all start?
    By Mike Skvarenina in forum Java
    Replies: 1
    Last Post: 02-09-2001, 01:04 AM
  5. Java Developers and Java Architect for exciting projects in Chicago area
    By Keith Franklin, MCSD in forum java.announcements
    Replies: 0
    Last Post: 08-18-2000, 07:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center