Results 1 to 18 of 18
Thread: square rooting a BigDecimal
 05202011, 06:27 PM #1
 05202011, 07:58 PM #2Java Code:
package bigroot; import java.math.BigDecimal; import java.math.BigInteger; //sqrtfunction copied of BigFunctionsClass //From The Java Programmers Guide To numerical Computing (Ronald Mak, 2003) public class BigRoot { /** * Compute the square root of x to a given scale, x >= 0. * Use Newton's algorithm. * @param x the value of x * @param scale the desired scale of the result * @return the result value */ public static BigDecimal sqrt(BigDecimal x, int scale) { // Check that x >= 0. if (x.signum() < 0) { throw new IllegalArgumentException("x < 0"); } // n = x*(10^(2*scale)) BigInteger n = x.movePointRight(scale << 1).toBigInteger(); // The first approximation is the upper half of n. int bits = (n.bitLength() + 1) >> 1; BigInteger ix = n.shiftRight(bits); BigInteger ixPrev; // Loop until the approximations converge // (two successive approximations are equal after rounding). do { ixPrev = ix; // x = (x + n/x)/2 ix = ix.add(n.divide(ix)).shiftRight(1); Thread.yield(); } while (ix.compareTo(ixPrev) != 0); return new BigDecimal(ix, scale); } }
Java Code:package bigroot; import java.math.BigDecimal; /** * Test the BigFunctions by comparing results with class java.lang.Math. */ public class TestBigRoot { private static final int SCALE = 40; private void run() { System.out.println("sqrt 2 = " + Math.sqrt(2)); System.out.println(" = " + BigRoot.sqrt(BigDecimal.valueOf(2), SCALE)); System.out.println("sqrt 26 = " + Math.sqrt(26)); System.out.println(" = " + BigRoot.sqrt(BigDecimal.valueOf(26), SCALE)); } public static void main(String args[]) { TestBigRoot test = new TestBigRoot(); try { test.run(); } catch(Exception ex) { System.out.println("ERROR: " + ex.getMessage()); } } }
 05202011, 08:05 PM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,541
 Blog Entries
 7
 Rep Power
 20
 05202011, 09:28 PM #4
Thanks. I am not going to try to understand it now, but it does a great job of what I want it to.
 05212011, 03:28 PM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,541
 Blog Entries
 7
 Rep Power
 20
cenosillicaphobia: the fear for an empty beer glass

 05212011, 03:42 PM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,541
 Blog Entries
 7
 Rep Power
 20
 05212011, 04:32 PM #8My reply #3 (accidentally) described how the method in reply #2 works.No bug ever had to calculate its fitnessfunction.
 05212011, 04:42 PM #9
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,541
 Blog Entries
 7
 Rep Power
 20
 05212011, 09:57 PM #10
I am just making a basic calculator. I read a forum post a while back that said if you are adding money or anything like that you should never use double. You should use BigDecimal.
Were you suggesting I convert the BigDecimal to an int or just use double? I haven't made very much java yet. I just do it in my spare time.
 05212011, 10:16 PM #11
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,541
 Blog Entries
 7
 Rep Power
 20
Are you making a general purpose (basic) calculator, or are you building a financial calculator? If it's the former, ordinary doubles would do fine (unless you want unlimited precision). If it is a financial calculator it depends on the type of calculations you want to perform. So, it's your turn to elaborate on this ;)
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
 05212011, 10:36 PM #12
 05212011, 10:56 PM #13
Just a warning: you'll need a book then like mentioned in the listing, and Jos/Fubarable are right, working with doubles is almost allways more then needed (and a good, (notPolishnotation) calculator is difficult enough). But it would be a challenge. Tell here when you're done!
No bug ever had to calculate its fitnessfunction.
 05222011, 02:33 AM #14
 05222011, 02:53 AM #15Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,565
 Rep Power
 12
No in polish notation your example would be entered as 5,6,5,*,+,9,1,+, (I think!)
The nice thing is that the order of operations is unambiguously specified without parentheses. The downside is that only Poles can understand it.
The notation is described at Polish notation  Wikipedia, the free encyclopedia  the example I gave seems to be Reverse Polish notation  Wikipedia, the free encyclopedia.
 05222011, 10:03 AM #16
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,541
 Blog Entries
 7
 Rep Power
 20
Yep, also, if the arity of the operators/functions is known and fixed you don't need those irritating parentheses in Polish notation either, e.g. 3*(4+5) is * 3 + 4 5 in Polish notation and is unambiguous, as is the reverse Polish notation 3 4 5 + * It's the variadic functions/operators that mess things up, e.g. (+ 1 2 3) where the + operator takes three arguments. There also is the functional notation (sometimes called 'outer Lisp') where the operators are used as functions, so 3*(4+5) is written as *(3, +(4, 5)). If the comma isn't a function/operator itself it can be left out.
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
 05222011, 12:13 PM #17
I just can't leave for a moment! The explanations of Jos and PBrockway are perfect, and google also knows something about it. I just mentioned it because it is supposedly easier to program: you don't need the parsing. But indeed, only Polish people seem to understand or like it.
(In effect what you do on a small calculator with just an inbetween result showing is a bit like reverse Polish notation (but cheating by using a memory): just enter arguments followed by an operator all the time.)Last edited by Jodokus; 05222011 at 01:23 PM. Reason: forgot "reverse"
No bug ever had to calculate its fitnessfunction.
 05222011, 12:59 PM #18
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,541
 Blog Entries
 7
 Rep Power
 20
That's not entirely true, all three forms need a bit of parsing, e.g. + *  doesn't make sense for a postfix expression (reverse Polish) if there are no operands available on the data stack; equivalently * 3  doesn't make sense as an infix expression, nor does it as a prefix expression (Polish notation). On the other hand those parsers are minimal compared to the lexical analyzers needed in all three of the expression forms.
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
Similar Threads

Negating a BigDecimal
By ryanmk54 in forum New To JavaReplies: 5Last Post: 05162011, 06:23 PM 
Divide bigdecimal again
By ellhar in forum New To JavaReplies: 3Last Post: 03232011, 10:19 AM 
BigDecimal and BigInteger
By jon80 in forum New To JavaReplies: 8Last Post: 11282010, 11:15 AM 
Using BigDecimal
By doymand in forum New To JavaReplies: 2Last Post: 10302010, 09:25 AM 
BigDecimal question
By orchid in forum New To JavaReplies: 2Last Post: 08122008, 01:44 PM
Bookmarks