Long multiplication: Difference between revisions
Content added Content deleted
m (Fixed lang tags.) |
|||
Line 10:
=={{header|Ada}}==
The following implementation uses representation of a long number by an array of 32-bit elements:
▲ type Long_Number is array (Natural range <>) of Unsigned_32;
end loop;▼
end loop;
end loop;
for Index in reverse Result'Range loop -- Normalization▼
for Index in
end
end "*";</lang>
The task requires conversion into decimal base. For this we also need division to short number with a remainder. Here it is:
<lang ada>procedure Div
( Dividend : in out Long_Number;
Divisor
)
Div : constant Unsigned_64 := Unsigned_64 (Divisor);
Size : Natural
▲ begin
Dividend
if Size = 0 and then Dividend (Index)
end
Accum := Accum
Remainder := Unsigned_32 (Accum);
▲ end loop;
end Div;</lang>▼
▲ end Div;
▲</lang>
With the above the test program:
<lang ada>with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;▼
▲with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO; use Ada.Text_IO;
with Interfaces; use Interfaces;
Line 87 ⟶ 82:
begin
Put (X);
end Long_Multiplication;</lang>
Sample output:
<pre>
Line 100 ⟶ 94:
[[ALGOL 68G]] allows any precision for '''long long int''' to be defined
when the program is run, e.g. 200 digits.
<lang
MODE INTEGER = LONG LONG INT;
Line 133 ⟶ 127:
{{works with|ALGOL 68G|Any - tested with release mk15-0.8b.fc9.i386}}
<!-- {{does not works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release 1.8.8d.fc9.i386 - translator throws and assert. I'm not sure why.}} -->
<lang
MODE INTEGER = FLEX[0]DIGIT; # an arbitary number of digits #
Line 226 ⟶ 220:
IF leading zeros = UPB out THEN "0" ELSE sign + out[leading zeros+1:] FI
);</lang>
<lang
# Finally Define the required INTEGER multiplication OPerator. #
################################################################
Line 249 ⟶ 243:
NORMALISE ab
);</lang>
<lang
OP - = (INTEGER a)INTEGER: raise integer not implemented error("monadic minus"),
ABS = (INTEGER a)INTEGER: raise integer not implemented error("ABS"),
|