Jump to content

Java BigDecimal


DavidCampbell

Recommended Posts

anyone know how to multiply multiple BigDecimals?

not like this example:

BigDecimal subTotal, taxRate, tax, total;
subTotal = new BigDecimal("32.50");
taxRate = new BigDecimal("0.05");
tax = subTotal.multiply(taxRate);
total = subTotal.add(tax);

I need to multiple more than two numbers and also do addition.
 

This is the line i'm trying to convert to BigDecimals: 

double aDeterminantMinuend = (a[0][0] * a[1][1] * a[2][2]) + (a[0][1] * a[1][2] * a[2][0]) + (a[0][2] * a[1][0] * a[2][1]);

as you can see those are doubles in an array that i'm doing my arithmetic to.

so I've prepped them like:

BigDecimal a00 = new BigDecimal(a[0][0]);
BigDecimal a01 = new BigDecimal(a[0][1]);
BigDecimal a02 = new BigDecimal(a[0][2]);

BigDecimal a10 = new BigDecimal(a[1][0]);
BigDecimal a11 = new BigDecimal(a[1][1]);
BigDecimal a12 = new BigDecimal(a[1][2]);

BigDecimal a20 = new BigDecimal(a[2][0]);
BigDecimal a21 = new BigDecimal(a[2][1]);
BigDecimal a22 = new BigDecimal(a[2][2]);

so now I need to do the BigDecimal arithmetic to them, but the only examples i can find are like the one posted above. Just two BigDecimals multiplied together.

i've tried embedding multiple calls the multiple() method, but that doesnt work.

I'm thinking the only way to do it is to make new BigDecimals for each step of the arithmetic, but surely theres a better, neater way?



 

Link to comment
Share on other sites

actually I think I've solved it:

 

		BigDecimal a00 = new BigDecimal(a[0][0]);
		BigDecimal a01 = new BigDecimal(a[0][1]);
		BigDecimal a02 = new BigDecimal(a[0][2]);

		BigDecimal a10 = new BigDecimal(a[1][0]);
		BigDecimal a11 = new BigDecimal(a[1][1]);
		BigDecimal a12 = new BigDecimal(a[1][2]);

		BigDecimal a20 = new BigDecimal(a[2][0]);
		BigDecimal a21 = new BigDecimal(a[2][1]);
		BigDecimal a22 = new BigDecimal(a[2][2]);

		BigDecimal a00a11a22 = new BigDecimal("0.0");
		BigDecimal a01a12a20 = new BigDecimal("0.0");
		BigDecimal a02a10a21 = new BigDecimal("0.0");

		BigDecimal a20a11a02 = new BigDecimal("0.0");
		BigDecimal a21a12a00 = new BigDecimal("0.0");
		BigDecimal a22a10a01 = new BigDecimal("0.0");		

		BigDecimal bigDecimalDeterminantMinuend = new BigDecimal("0.0");
		BigDecimal bigDecimalDeterminantSubtrahend = new BigDecimal("0.0");

		a00a11a22 = a00.multiply(a11).multiply(a22);
		a01a12a20 = a01.multiply(a12).multiply(a20);
		a02a10a21 = a02.multiply(a10).multiply(a21);

		bigDecimalDeterminantMinuend = a00a11a22.add(a01a12a20).add(a02a10a21);

		a20a11a02 = a20.multiply(a11).multiply(a02);
		a21a12a00 = a21.multiply(a12).multiply(a00);
		a22a10a01 = a22.multiply(a10).multiply(a01);

		bigDecimalDeterminantSubtrahend = a20a11a02.add(a21a12a00).add(a22a10a01);

 

Edited by DavidCampbell
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...