Implementation of multiplication exceeding the number of significant digits
Author: ameyaExecutions: 569
※ This library was created by a member. This site is not responsible for its content.
This example demonstrates one method of storing the result of a multiplication in two variables to ensure the result has twice the number of digits. For illustrative purposes, it is set to 10 digits here, but the same approach can be applied even with 50 digits.
The source code looks like this:
I’ve omitted the `print` lines to make it easier to read.
/* `keta` is a reserved word representing the number of digits */
ketahalf = keta / 2; /* ← Half the number of digits, used for splitting */
mxall = 10 ^ keta; /* ← Use exponential notation when displaying the higher digits */
mxhalf = 10 ^ ketahalf; /* ← Dividing by these values to avoid the issue. */
/* The inputs are A and B, which are split into ah, a and bh, b, respectively */
ah = rounddown(A, -ketahalf);
bh = rounddown(B, -ketahalf);
a = A - ah;
b = B - bh;
/* Dividing by half ensures that the number of significant digits in the multiplication result fits within the specified number of digits */
/* Multiplication of the higher-order and lower-order digits */
cch = ah * bh;
cc = a * b;
/* Cross-multiplication */
m1 = ah * b;
m2 = bh * a;
/* Split the results of the cross-multiplication */
mh1 = rounddown(m1, -keta);
m1 = m1 - mh1;
mh2 = rounddown(m2, -keta);
m2 = m2 - mh2;
/* Add the results of the cross-multiplication */
th = mh1 + mh2;
/* Here, we use a technique because adding the lower-order digits may result in a carry */
tt = (m1 - 5E+9) + (m2 - 5E+9);
if (tt < 0) {
tt = tt + 1E+10;
} else {
th = th + 1E+10;
}
/* Add the results of the initial multiplication of the high-order and low-order bits */
cch = cch + th;
/* There is also a possibility of a carry to the low-order bits here */
cc = (cc - 5E+9) + (tt - 5E+9);
if (cc < 0) {
cc = cc + 1E+10;
} else {
cch = cch + 1E+10;
}
/* Display the answer. */
println(-11, cch / mxall, cc);
/* I realize this might be difficult to understand,
but if you keep in mind that all these steps are ultimately aimed at “preserving significant figures,”
things should start to make more sense. */
I’ve omitted the `print` lines to make it easier to read.
/* `keta` is a reserved word representing the number of digits */
ketahalf = keta / 2; /* ← Half the number of digits, used for splitting */
mxall = 10 ^ keta; /* ← Use exponential notation when displaying the higher digits */
mxhalf = 10 ^ ketahalf; /* ← Dividing by these values to avoid the issue. */
/* The inputs are A and B, which are split into ah, a and bh, b, respectively */
ah = rounddown(A, -ketahalf);
bh = rounddown(B, -ketahalf);
a = A - ah;
b = B - bh;
/* Dividing by half ensures that the number of significant digits in the multiplication result fits within the specified number of digits */
/* Multiplication of the higher-order and lower-order digits */
cch = ah * bh;
cc = a * b;
/* Cross-multiplication */
m1 = ah * b;
m2 = bh * a;
/* Split the results of the cross-multiplication */
mh1 = rounddown(m1, -keta);
m1 = m1 - mh1;
mh2 = rounddown(m2, -keta);
m2 = m2 - mh2;
/* Add the results of the cross-multiplication */
th = mh1 + mh2;
/* Here, we use a technique because adding the lower-order digits may result in a carry */
tt = (m1 - 5E+9) + (m2 - 5E+9);
if (tt < 0) {
tt = tt + 1E+10;
} else {
th = th + 1E+10;
}
/* Add the results of the initial multiplication of the high-order and low-order bits */
cch = cch + th;
/* There is also a possibility of a carry to the low-order bits here */
cc = (cc - 5E+9) + (tt - 5E+9);
if (cc < 0) {
cc = cc + 1E+10;
} else {
cch = cch + 1E+10;
}
/* Display the answer. */
println(-11, cch / mxall, cc);
/* I realize this might be difficult to understand,
but if you keep in mind that all these steps are ultimately aimed at “preserving significant figures,”
things should start to make more sense. */
No customer feedback for this calculation yet.
When dealing with integers, it’s a problem when numbers get rounded based on the number of significant digits, isn’t it?
Strictly speaking, to calculate the exact result of a multiplication, you need twice as many significant digits.