Exponentiation operator: Difference between revisions

Content added Content deleted
m (→‎{{header|OCaml}}: indentation)
Line 652: Line 652:
It is possible to create a generic exponential. For this, one must know the
It is possible to create a generic exponential. For this, one must know the
multiplication function, and the unit value. Here, the usual fast algorithm is
multiplication function, and the unit value. Here, the usual fast algorithm is
used.
used:


<lang ocaml>let pow one mul a n = let rec g p x = function 0 -> x | i ->
<lang ocaml>let pow one mul a n =
let rec g p x = function
g (mul p p) (if i mod 2 = 1 then mul p x else x) (i/2) in g a one n;;
| 0 -> x
| i ->
g (mul p p) (if i mod 2 = 1 then mul p x else x) (i/2)
in
g a one n
;;


pow 1 ( * ) 2 16;; (* 65536 *)
pow 1 ( * ) 2 16;; (* 65536 *)
Line 674: Line 680:
*)</lang>
*)</lang>


See also [[Matrix-exponentiation_operator#OCaml]] for a matrix usage.
See also [[Matrix-exponentiation operator#OCaml]] for a matrix usage.


=={{header|Perl}}==
=={{header|Perl}}==