Previous: , Up: Revised Arithmetics   [Contents][Index]


63.5.2 New Arithmetical Functions

Completing basic arithmetics, ENT provides functions for exponentiation. In general, it is highly advised to prefer the dedicated power function over successive multiplication. We give a timing example below.

Function: ^ number1 number2

Return the power number1 to the number2.

(^ 3 4)
     ⇒ 81
(^ 81 23)
     ⇒ 78551672112789411833022577315290546060373041

Note that exponentiation is not associative. That is why ^ merely takes two arguments at a time. Check the order of computation carefully. The following example demonstrates the generated error in case of wrong association.

(^ 2 (^ 3 4))
     ⇒ 2417851639229258349412352
(^ (^ 2 3) 4)
     ⇒ 4096

In order to get a notion of the time differences,

(let ((start (current-btime))
      (dummy (^ 2 (^ 3 4)))
      (stop (current-btime)))
  (- stop start))
     ⇒ 31

Now we perform the same with sequential multiplication:

(let* ((start (current-btime))
       (3^4 (* 3 3 3 3))
       (result 1)
       (2^3^4 (dotimes (i 3^4 result)
                 (setq result (* 2 result))))
       (stop (current-btime)))
  (- stop start))
     ⇒ 567

To see the superiority, we modify the actual problem.

(let ((start (current-btime))
      (dummy (^ 5 (^ 5 5)))
      (stop (current-btime)))
  (- stop start))
     ⇒ 77
(let* ((start (current-btime))
       (5^5 (* 5 5 5 5 5))
       (result 1)
       (5^5^5 (dotimes (i 5^5 result)
                 (setq result (* 5 result))))
       (stop (current-btime)))
  (- stop start))
     ⇒ 21963

Whenever computing two-powers or ten-powers, there are dedicated functions which make use of special algorithms.

Function: 2^ number &optional precision

Return the exponential of number to 2 power. If optional argument precision is non-nil, its value (an integer) is used as precision in float computations.

Function: 10^ number &optional precision

Return the exponential of number to 10 power. If optional argument precision is non-nil, its value (an integer) is used as precision in float computations.

There is another arithmetic function working on the prime decomposition of integer numbers. This is roughly equivalent to repeatedly check if a number is congruent modulo a factor and if so a division by that factor.

Function: remove-factor factor number

Remove all occurences of factor in number and return a cons cell with number divided by a maximal power of factor in the car and the exponent in the cdr.

(remove-factor 5 25)
     ⇒ (1 . 2)
(remove-factor 7 25)
     ⇒ (25 . 0)
(remove-factor 11 2198765678901)
     ⇒ (18171617181 . 2)

Previous: , Up: Revised Arithmetics   [Contents][Index]