Long multiplication: Difference between revisions
Content added Content deleted
(Kotlin variant enhanced) |
(→{{header|JavaScript}}: Minor edits to allow compilation in current JavaScript engines Added output, and comments thereon) |
||
Line 2,349: | Line 2,349: | ||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
With integer inputs at this scale, JavaScript still gives a slightly lossy result, despite the subsequent digit by digit string concatenation approach. |
|||
<lang javascript>function mult(num1,num2){ |
<lang javascript>function mult(num1,num2){ |
||
⚫ | |||
var a1 = num1.toString().split("").reverse(); |
|||
⚫ | |||
var aResult = new Array; |
|||
⚫ | |||
for ( var iterNum1 = 0; iterNum1 < a1.length; iterNum1++ ) { |
|||
⚫ | |||
idxIter = iterNum1 + iterNum2; // Get the current array position. |
|||
var idxIter = iterNum1 + iterNum2; // Get the current array position. |
|||
aResult[idxIter] = a1[iterNum1] * a2[iterNum2] + ( idxIter >= aResult.length ? 0 : aResult[idxIter] ); |
|||
⚫ | |||
if ( aResult[idxIter] > 9 ) { // Carrying |
|||
if ( aResult[idxIter] > 9 ) { // Carrying |
|||
aResult[idxIter + 1] = Math.floor( aResult[idxIter] / 10 ) + ( idxIter + 1 >= aResult.length ? 0 : aResult[idxIter + 1] ); |
|||
aResult[idxIter] -= Math.floor( aResult[idxIter] / 10 ) * 10; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
⚫ | |||
} |
|||
return aResult.reverse().join(""); |
|||
}</lang> |
|||
} |
|||
mult(Math.pow(2,64), Math.pow(2,64))</lang> |
|||
{{Out}} |
|||
<pre>340282366920938477630474056040704000000</pre> |
|||
Compare this with the full precision result: |
|||
<pre>340282366920938463463374607431768211456</pre> |
|||
=={{header|jq}}== |
=={{header|jq}}== |
||
{{Works with|jq|1.4}} |
{{Works with|jq|1.4}} |