Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java: Maths in the default libraries

  • 18-11-2006 2:30pm
    #1
    Registered Users, Registered Users 2 Posts: 5,618 ✭✭✭


    Does anyone know if Sun are planning to build in some slightly more advanced maths functionality into their defualt VM set or is the Math class all we're going to get.

    I was looking for some of the functionality of my calculator (specificly factorial and nCr) for use in a java program I was writing today and was quite surprised not to find it in the API docs, and a quick google turned up no useful information.

    Maybe I should be writing this program in Fortran!


Comments

  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    Short answer: Nope. Maths package is what you get.

    A quick google appears to have code for nCr and factorials.


  • Registered Users, Registered Users 2 Posts: 5,618 ✭✭✭Civilian_Target


    Well - yeah, thats exactly what I did, factorial's just 3 lines, and one you have that nCr's only another 2 but it's not as handy as saying Math.fac...


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    public int Factorial(int number)
    {
         return abs(number)>0 ? number*Factorial(abs(number)-1) : 1;
    }
    
    public int NCR(int n, int r)
    {
        return Factorial(n) / Factorial(r);
    }
    

    Not the most efficient code, but bloody short :p


  • Registered Users, Registered Users 2 Posts: 5,618 ✭✭✭Civilian_Target


    Pfft - yeah wrote it myself in the end too, except I used small letters for the method names! Only needed r = 2, but also needed to calculate triangular numbers... Incidentally, if you were writing NCR, it's actually fac(n)/(fac(r)*fac(n-r))
    I have to do a heavier version next week, and it's gonna be in Fortran or I might take the time to learn Matlab.
    public int tn(int i){
    		int r = 0;
    		for(int j = 0; j < i; j++)
    			for(int k = j+1; k < i; k++)
    				r++;
    		return r;
    	}
    	public int fac(int i){
    		if(i <=1 ) return 1;
    		return i * fac(i - 1);
    	}
    	public int n2r(int i){
    		return fac(i) / fac (i - 2);
    	}
    


  • Closed Accounts Posts: 67 ✭✭verbatim


    Whilst lecturers love to teach recursion in Java, its not the best thing to use in the realworld. If you have too many levels of recursion in Java, your stack overflows, but since your code appears syntactically perfect it can take a while to figure out where the error is.

    The Maths support in Java has most of what you need to get by. What really annoys me is the BigInteger and BigDecimal classes, which only have a tiny fraction of the methods that the Maths class has.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 5,618 ✭✭✭Civilian_Target


    Nonsense. If used sensibly recursion works very well in the real world, and creates simple efficient code. If used badly it creates unnecessary n-p hard problems :(

    At the end of the day, most for and while loops behave exactly the same way as recursive statements in machine code so nothing is really lost by using recursion except a few ugly lines of for loop!

    Granted, my n2r method is not efficient because it calculates fac(i) and fac(i-2) whereas using a loop, fac(i-2) could be carried if you wrote it well. But I needed a quick fix, so that's what I did.

    I find the Java maths library to be woefully under-resourced, but I need the code portability. Otherwise I'd write it in Fortran.


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    verbatim wrote:
    Whilst lecturers love to teach recursion in Java, its not the best thing to use in the realworld. If you have too many levels of recursion in Java, your stack overflows, but since your code appears syntactically perfect it can take a while to figure out where the error is.
    I think a stack overflow exception would be pretty obvious to figure out :p (im sure java throws one of those type of exceptions when the stack overflows). There are places where recursion is *much* better than simple looping. Supposing i wanted you to do a search on your harddrive and find all the files that contain the word "test" in the filename. Recursion would be the easiest way to do that.
    FindAllFolders(string PATH, ArrayList results)
    {
         foreach(Folder newFolder in the current PATH)
              FindAllFolders(newFolder, results);
    
         foreach(File f in all the files in this PATH)
             if(file.Name.Contains("test"))
                  results.Add(f);
    }
    

    Now, how would you do that without recursion? (note that i used psuedo code, actual code would be ~4-6 lines longer)


  • Registered Users, Registered Users 2 Posts: 5,618 ✭✭✭Civilian_Target


    For those of you that do need more Maths functionality: I've started using Jama: http://math.nist.gov/javanumerics/jama/

    It's matrix features are particularly swish.


Advertisement