What Is Ternary? Math, Music, Chemistry, and More

Ternary means “composed of three parts.” It comes from the Latin ternarius, and you’ll encounter it across math, computer science, chemistry, music, and data visualization. The core idea never changes: where binary deals in twos, ternary deals in threes. What that looks like in practice depends entirely on the field.

Ternary in Math: Base-3 Numbers

The most foundational meaning of ternary is the base-3 number system. Just as the decimal system uses ten digits (0 through 9) and binary uses two (0 and 1), the ternary system uses only three digits: 0, 1, and 2. Each position in a ternary number represents a power of 3 rather than a power of 10.

To convert a ternary number to decimal, you multiply each digit by its corresponding power of 3 and add the results. For example, the ternary number 1021 translates to:

  • 1 × 3³ (27) + 0 × 3² (0) + 2 × 3¹ (6) + 1 × 3⁰ (1) = 34 in decimal

Ternary has a neat mathematical property: a number’s remainder when divided by 2 equals the count of 1s in its base-3 representation. This is the base-3 equivalent of “casting out nines” in decimal arithmetic, except here you “cast out twos.”

The Ternary Operator in Programming

If you searched “what is ternary” as a programmer, this is probably what you’re after. The ternary operator (also called the conditional operator) is a shorthand way to write an if/else statement in a single line. It’s the only operator in most languages that takes three operands, which is where the name comes from.

The syntax looks like this:

condition ? valueIfTrue : valueIfFalse

First, the condition is evaluated. If it’s true (or “truthy”), the expression returns the value after the question mark. If it’s false (or “falsy”), it returns the value after the colon. So instead of writing a four-line if/else block to assign a variable, you can write something like age >= 18 ? "adult" : "minor".

This syntax works identically in JavaScript, C, C++, Java, PHP, and many other languages. Python uses a slightly different format: value_if_true if condition else value_if_false, but the logic is the same.

You can also chain ternary operators together to mimic an if/else if/else structure. The operator is right-associative, meaning nested ternaries evaluate from right to left. In practice, though, chaining more than one or two gets hard to read, and most style guides recommend switching to a regular if/else block at that point. Values like null, 0, empty strings, undefined, and NaN all count as falsy and will trigger the “false” branch.

Ternary Compounds in Chemistry

In chemistry, a ternary compound contains exactly three different chemical elements. Water (H₂O), for instance, is not ternary because it only has two elements. Sodium sulfate (Na₂SO₄), with sodium, sulfur, and oxygen, is.

Ternary compounds can form from elements in three different columns of the periodic table, like mercury indium telluride, or from just two columns, like aluminum gallium arsenide. That second example is especially important in electronics, where ternary semiconductor compounds are used to build LEDs, laser diodes, and solar cells. The rare-earth oxides alone form tens of thousands of ternary and higher-order compounds with other metal oxides, making this a vast category of materials.

Ternary Form in Music

In music theory, ternary form refers to a three-part structure commonly labeled ABA. The idea is simple: a musical theme is stated (A), a contrasting section follows (B), and then the original theme returns (A). It’s often described as “statement, digression, restatement.”

This structure is everywhere in Western classical music. Many Romantic-era character pieces, including nocturnes by Chopin, intermezzos by Brahms, and “Songs Without Words” by Mendelssohn, use a larger ternary form where each section may span dozens of measures. Rachmaninov’s famous Prelude in C-sharp minor is another well-known example. The ABA pattern gives listeners a sense of departure and return, which is one reason it has remained so popular for centuries.

Ternary Plots for Three-Variable Data

A ternary plot (also called a triangular diagram) is a way to visualize data that has three components adding up to 100%. The plot is shaped like an equilateral triangle, with each corner representing 100% of one component and the opposite side representing 0% of that component.

A point at one corner means the sample is entirely one component. A point along an edge means it contains only two of the three components. A point inside the triangle contains some proportion of all three, and its position tells you exactly how much of each. Gridlines inside the triangle run parallel to each side, letting you read percentages just like you’d read coordinates on a standard graph.

Geologists use ternary diagrams constantly to classify rocks, soils, sediment types, and mineral compositions based on relative proportions of their constituents. They also show up in metallurgy, genetics, and any field where three variables compete for a fixed total. The regions inside the triangle are often divided into labeled “fields,” so once you plot a data point, you can immediately classify it based on which field it falls in.

Ternary Computing

Binary computers store information in bits, each holding a value of 0 or 1. A ternary computer would use “trits” instead, with each unit holding three possible values: typically -1, 0, and +1. This isn’t purely theoretical. In 1958, engineer Nikolai Brusentsov and his team at Moscow State University built the Setun, the world’s first (and still only) ternary computer.

The Setun used a symmetric ternary system with values of -1, 0, and +1. It had 81 words of ferrite core memory, about 2,000 words of drum storage, and a set of just 27 instructions. Addition took 180 microseconds, multiplication about 335 microseconds. Input and output ran through five-hole punched paper tape in blocks of 27 words. The hardware relied on pairs of ferrite cores for each trit, exploiting the fact that the cores’ magnetic properties naturally supported three distinguishable states rather than just two.

There’s a theoretical reason ternary is interesting for computing. The mathematical sweet spot for information density turns out to be the number e (roughly 2.718). Since you can’t build hardware with 2.718 states, the two closest whole-number options are 2 and 3. Three is closer to e, which means ternary systems can theoretically represent the same range of numbers using fewer total digits and less “cost” per number than binary. In practice, however, decades of engineering optimization around binary transistors have made binary far cheaper and more reliable, which is why no ternary computers followed the Setun into production.