calum.org is hosted on a linuxvps.org Linux Virtual Private Server. Why not get your own?
Because J2ME is annoying, it doesn't have a function that rounds a number, say 1234.56789 to a certain number of decimal places. So here is mine.
PS. I am learning Java, so if it doesn't adhere to the One True Java Way (tm), please feel free to write me a long, vitriolic diatribe criticising me and delete it before sending. Otherwise, leave a comment telling me where I went wrong.
public static double calumround(double arg, int places) {
double tmp = (double) arg * (pow(10,places));
int tmp1 = (int)Math.floor( tmp + 0.5 );
double tmp2 = (double) tmp1 / (pow(10,places));
return tmp2;
}
Because J2ME is still annoying, it also doesn't have a "pow" function. So here is mine.
public static int pow(int arg, int times){
int ret = 1;
for ( int i = 1 ; i <= times ; i++ ) {
ret = ret * arg;
}
return ret;
}
These functions work well enough for me and what I want from them. If you're planning to use them for landing a multi-million dollar probe on a remote planet, you might want to check them out first. Or not - it's your call.
J2ME doesn't have a lot of stuff that I consider necessary. Which is annoying. However, when I saw my colleague trying to learn Symbian, I decided that Java was probably the easier evil of the two.
Comments
J2me is rather sparse
You double to int casting is nice. Ive wondered for a while how to cast in j2me , since it doesnt provide Double.toInt(). J2me is really daft sometimes. I just dont get y they dont implement some of these really really basic things. For example, arctan is not implemented (its not difficult to do, but come on.)
Mathematic functions can easily be implemented by using some applied maths. Stuff like numerical methods which can be used to approximate all of this can be found in this book. Nice reference if you like maths.
http://convertit.com/Go/ConvertIt/Reference/AMS55.ASP?Res=150&Page=-5
How about not using a
How about not using a counter? It saves a variable :)
int pow(int base, int power)
{
int ret=1;
while (power > 0)
{
ret = ret*base;
power--;
}
return ret;
}
Please note that both implementations are naive in the sense that if you pass a negative power as parameter... well, you can figure that out.