# Lexical elements: Imaginary literals

### January 20, 2023

I understand in principle what imaginary numbers are. I used them during high school and university mathematics classes to get good grades.

I’ve never had a practical use for them.

I’ve certainly never used them in programming, in Go or otherwise.

All that said, I guess it’s cool that Go has first-class support for imaginary numbers, rather than it being an add-on library of some kind.

If you’re like me, and have never had the need to write software that understands complex numbers, you might skip today’s email, or better yet, jump over to 3Blue1Brown’s video that explains what complex and imaginary numbers are, and why they’re useful.

## Imaginary literals

An imaginary literal represents the imaginary part of a complex constant.

What’s important to understand from this opening sentence is that imaginary literals are not meant to be used alone. They’re meant to be used in creating complex values or constants. So while you could pass around an imaginary value in your code willy nilly, it won’t generally do you any good without a more complete understanding of complex number theory, and how Go interacts with complex numbers, the latter of course, which we’ll get to in time.

It consists of an integer or floating-point literal followed by the lowercase letter `i`. The value of an imaginary literal is the value of the respective integer or floating-point literal multiplied by the imaginary unit `i`.

``````imaginary_lit = (decimal_digits | int_lit | float_lit) "i" .
``````

For backward compatibility, an imaginary literal’s integer part consisting entirely of decimal digits (and possibly underscores) is considered a decimal integer, even if it starts with a leading `0`.

This paragraph is really the only “gotcha”. As discussed earlier this week normally `0123` would be interpreted as the Octal value `0o123` (or `83` in decimal), a historical exception is made for imaginary literals, such that a leading 0 is still treated as a decimal value.

``````0i
0123i         // == 123i for backward-compatibility
0o123i        // == 0o123 * 1i == 83i
0xabci        // == 0xabc * 1i == 2748i
0.i
2.71828i
1.e+0i
6.67428e-11i
1E6i
.25i
.12345E+5i
0x1p-2i       // == 0x1p-2 * 1i == 0.25i
``````

Quotes from The Go Programming Language Specification, Version of June 29, 2022