DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 1 of 1
  1. #1
    Join Date
    Apr 2006
    Posts
    20

    odd java rounding up problem...

    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++;
    		
    		
    	}
    	}
    		
    }
    Last edited by ftr; 10-19-2006 at 10:08 PM. Reason: issue changed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2006, 03:16 PM
  2. Java vs. .Net. A questionnaire
    By Basil in forum .NET
    Replies: 1
    Last Post: 05-13-2005, 06:46 AM
  3. learning c# very confusing.
    By Mike Tsakiris in forum .NET
    Replies: 11
    Last Post: 10-04-2002, 05:32 PM
  4. Re: VB vs. Visual Age for Java
    By JJ in forum Enterprise
    Replies: 1
    Last Post: 07-06-2000, 04:50 AM

Bookmarks

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


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links