# Wait, but what's e^N?

Objective:

The objective is to calculate e^N for some real or imaginary N (e.g. 2, -3i, 0.7, 2.5i, but not 3+2i). This is code golf, so, shortest code (in bytes) wins.

So, for example:

1. N = 3, e^N = 20.0855392
2. N = -2i, e^N = -0.416146837 - 0.9092974268i

The letter i shouldn't be capitalized (since in math it is lowercase)

If your language supports raising numbers to complex numbers, you can't use that. You may use built-in real exponentiation.

For any answer where b (in a + bi) is negative, it is OK to have both the plus and minus sign (e.g. 2 + -3i), since that is understood to be (2 - 3i)

Bonus (-15 bytes): Solve this for N is a any complex number (e.g. 5 + 7i). If you are going for the bonus, you can assume that both components will always be given (even for the standard objective)

• Can we print a double struck i, provided we don't actually use complex arithmetic in the code? – Martin Ender Nov 25 '14 at 14:08
• @mmk See the image in belisarius's comment on my Mathematica answer. May the i in the output look like that, or does it have to be ASCII code 0x69? – Martin Ender Nov 26 '14 at 2:19
• @MartinBüttner, as long as it isn't capitalized, it is OK. – mmk Nov 26 '14 at 2:26
• what are acceptable in/out formats? A list of 2 numbers? a+bi? r theta? native representation (APL represent complex numbers as aJb)? – TwiNight Nov 26 '14 at 10:39
• And J uses ajb. – Adám Aug 4 '16 at 19:38

# Mathematica, 23 20 bytes - 15 = 5

E^#(Cos@#2+Sin@#2I)&


This is an anonymous function taking two arguments (real and imaginary component). You should supply floats unless you want exact result (with square roots and exponentials). So you can use this like

E^#(Cos@#2+Sin@#2I)&[3.0,-2.0]


Note that I is Mathematica's imaginary unit, but I'm not actually using any complex arithmetic. Also note, that Mathematica will actually print the result using a (double-struck) lowercase i as required. See the screenshot in belisarius's comment.

• Perhaps there is some subtlety I don't see, but E^# (Cos@#2 + Sin@#2 I) & seems to work – Dr. belisarius Nov 25 '14 at 3:53
• @belisarius, I don't want to use I because the question asks for lowercase i, and at least Mma 10 doesn't distribute that multiplication. If you can confirm that Mma 9 does, I'll change it. – Martin Ender Nov 25 '14 at 9:37
• i.stack.imgur.com/YnPhC.png – Dr. belisarius Nov 25 '14 at 11:51
• And no, with the letter "i", it doesn't distribute – Dr. belisarius Nov 25 '14 at 11:53
• You lucky!, I cannot reduce any chars using a function in CJam :/ – Optimizer Nov 27 '14 at 11:03

# CJam, 22 - 15 = 7

l~_mc\ms@me:X*\X*'+@'i


Try it online here

Takes input in the form of a b where a is the real component and b is the complex.

For ex. 2i is

0 2


gives

-0.4161468365471424+0.9092974268256817i


# 80386 Assembly (41 bytes - 15 = 26)

8B 4C 24 04 8B 54 24 08 D9 EA DC 09 D9 C0 D9 FC DC E9 D9 C9 D9 F0 D9 E8 DE C1 D9 FD DD 02 D9 FB D8 CA DD 19 D8 C9 DD 1A C3


Floating point math in assembly is a pain..

This is a function (cdecl calling convention) that calculates e^(a+bi) via the relation e^(a+bi)=e^a cos b + e^a sin b * i. (It accepts two double * parameters, a and b in that order, and outputs the real part into the a pointer, and the imaginary part into the b pointer).

The function prototype in C would be:

void __attribute__((cdecl)) exp_complex(double *a, double *b);


About half (20, to be exact) of the 41 bytes were spent calculating e^a; another 8 were spent getting the function arguments into registers.

The above bytecode was written by hand, from the following assembly (NASM-style, commented):

; get pointers into ecx, edx
mov ecx, [esp+4] ; 8B 4C 24 04
mov edx, [esp+8] ; 8B 54 24 08

; log(2,e)
fldl2e           ; D9 EA
; a log(2,e)
fmul qword [ecx] ; DC 09
; duplicate
fld st0          ; D9 C0
; get integer part
frndint          ; D9 FC
; get fractional part
fsub st1, st0    ; DC E9
; put fractional part on top
fxch st1         ; D9 C9
; 2^(fract(a log(2,e))) - 1
f2xm1            ; D9 F0
; 2^(fract(a log(2,e)))
fld1             ; D9 E8
; 2^(fract(a log(2,e))) * 2^(int(a log(2,e)))
; = e^a
fscale           ; D9 FD

; push b
fld qword [edx]  ; DD 02
; sin and cos
fsincos          ; D9 FB
; e^a cos b
fmul st2         ; D8 CA
; output as real part
fstp qword [ecx] ; DD 19
; e^a sin b
fmul st1         ; D8 C9
; output as imaginary part
fstp qword [edx] ; DD 1A

; exit
ret              ; C3


To try this in C (gcc, linux, intel processor):

#include <stdio.h>
#include <string.h>
#include <sys/mman.h>

int main(){
// bytecode from earlier
char code[] = {
0x8B, 0x4C, 0x24, 0x04, 0x8B, 0x54, 0x24, 0x08,
0xD9, 0xEA, 0xDC, 0x09, 0xD9, 0xC0, 0xD9, 0xFC,
0xDC, 0xE9, 0xD9, 0xC9, 0xD9, 0xF0, 0xD9, 0xE8,
0xDE, 0xC1, 0xD9, 0xFD, 0xDD, 0x02, 0xD9, 0xFB,
0xD8, 0xCA, 0xDD, 0x19, 0xD8, 0xC9, 0xDD, 0x1A,
0xC3,
};
// allocate executable memory to a function pointer called 'exp_complex'
void __attribute__( (__cdecl__) ) (*exp_complex)(double *,double *) = mmap(0,sizeof code,PROT_WRITE|PROT_EXEC,MAP_ANON|MAP_PRIVATE,-1,0);
memcpy(exp_complex, code, sizeof code);

// test inputs
double a = 42.24, b = -2.7182818;

printf("%.07g + %.07g i\n", a, b);

// call bytecode as a c function
exp_complex(&a, &b);

printf("%.07g + %.07g i\n", a, b);

// release allocated memory
munmap(exp_complex, sizeof code);

return 0;
}


## ES6, 6356 55 bytes - 15 = 40

(a,b)=>(m=Math,e=m.exp(a),e*m.cos(b)++${e*m.sin(b)}i)  This is a function that accepts a number in the form a + bi with a and b as the function arguments. It uses Euler's formula e^ix=cosx+isinx. • You can use parentheses and the comma operator to drop return: (a,b)=>(m=Math,e=m.exp(a),e*m.cos(b)+"+"+e*m.sin(b)+"i") – Ry- Nov 23 '14 at 16:55 • You can save 1 char using template strings: (a,b)=>(m=Math,e=m.exp(a),e*m.cos(b)++${e*m.sin(b)}i) – Oriol Nov 23 '14 at 21:58
• Sorry for the almost-3-year lag, but you can save 2 char by combining variable assignment with the evaluations: (a,b)=>(e=(m=Math).exp(a))*m.cos(b)++${e*m.sin(b)}i  – Shieru Asakoto Oct 11 '17 at 8:24 • Use with will same one more byte: with(Math)f=(a,b)=>(e=exp(a))*cos(b)++${e*sin(b)}i – tsh Jun 9 '20 at 10:25

## TI-BASIC (NSpire) 13 (28 bytes-15)

f(r,j):=^r*(sin(j)i+cos(j


The  is e. This computes e^z for z=r+i*j. i is an undefined variable. This defines a function f which accepts two parameters: the real part and the imaginary part.

• i isn't an undefined variable on 83/84 calculators; I'm not sure about the Nspire. Could you check what i^2 gives on the calculator? I don't have one. – tomsmeding Nov 23 '14 at 16:37
• How are r and j provided ? We have to write either a function that takes input from arguments or a complete program taking input from ARGV or STDIN. I can very well write a CJam program of 0 score if I follow your approach. – Optimizer Nov 23 '14 at 16:43
• i is different depending on how it's entered. Entered as a character on the keyboard it refers to the variable, while entered through a special way it refers to sqrt(-1). – Octavia Togami Nov 23 '14 at 17:53
• @tomsmeding this works on models that actually have symbolic variables (89, 92, nspire, and the like), where a variable that isn't given a value is just a symbol. i is i and i^2 is i^2. – hobbs Nov 23 '14 at 18:35
• The added cost should be 14 for Define f(r,j)= – hobbs Nov 23 '14 at 18:37

# Haskell 30 raw, 15 with bonus:

e a b=map(*exp a)[cos b,sin b]


load it up into the GHCi, then try out the function:

Ok, modules loaded: Main.
*Main> e 3 0
[20.085536923187668,0.0]
*Main> e 0 (-2)
[-0.4161468365471424,-0.9092974268256817]


Also, humans can understand why this works :)

I hope the GHCi is a code golf legal way of bringing Haskell in. Also note that a human must infer that the second field is the complex one. [edit1]

# CJam, 33 bytes

l_"i"&{);d_mc\ms_0<!'+*\'i}{dme}?


Test it here.

This supports only purely real or purely imaginary input.

# BBC BASIC - 44 bytes (with tokenisation) - 15 = 29

Put this after the END statement. Arguments are the same as

a+bi = (a,b)


### Code

DEFFNf(a,b)e=EXPa:=STR$(e*COSb)+"+"+STR$(e*SINb)+"i"


### Usage

imaginary$= FNf(1,1) PRINT imaginary$
END
DEFFNf(a,b)e=EXPa:=STR$(e*COSb)+"+"+STR$(e*SINb)+"i"


Where the output is:

1.46869394+2.28735529i


# Python, (94 - 15) = 79 bytes

from math import *
import cmath
n=input()
print e**(n[0])*(cos(n[1])+1j*sin(n[1]))[1:-2]+'i'


Pretty straightforward implementation. Since python uses "j" instead of "i" for imaginary numbers, replacing the characters in the final string eats up quite a few (12) bytes. This can definitely still be golfed.

Takes input in the form of [a,b] for a + bi.

• I think you can remove the space in 'print ' and why do you need cmath? – TuxCrafting Aug 18 '16 at 18:12

# APL (Dyalog Extended), 20 16 - 15 = 1 byte (SBCS)

'+i',⍨¨*⍤⊣×2 1○⊢


'+i',⍨¨ append "+" and "i" to the ends of
*e to the power of
⊣ the left argument (the real part)
× times
2 1○ the cosine and sine of
⊢ the right argument (the imaginary part)

TryAPL online!

# C (gcc), 51 47 bytes - 15 = 32

#define f(x)(cos((x)*1i)-1i*sin(1i*(x)))*exp(x)
`

Try it online!