Questions tagged [ternary]

The base-3 positional numeral system which represents numbers using the digits 0, 1, and 2

Ternary is the base-3 positional numeral system which represents numbers using the digits 0, 1, and 2. In computing it is less common than , but there have been ternary computers, and it can be used to represent sequences of ternary (3-way) choices.

A unit of information in the ternary numeral system is sometimes called a trit (by analogy with for binary).

Number systems:

       Name         |   Base  |   numbers
------------------------------------------                 
ternary   - system  |     2   |    0... 2
decimal   - system  |    10   |    0... 9

ternary    |    decimal      
-----------------------
         0 |        0        
         1 |        1         
         2 |        2       
        10 |        3        
        11 |        4      
        12 |        5       
        20 |        6      
        21 |        7       
        22 |        8       
        30 |        9      
       101 |       10       
       121 |       16      
      1012 |       32       
      2101 |       64       
     11202 |      128     
    100110 |      255      

Decimal -> Ternary

Number: 42

42 / 3 = 14 Rest 0    ^
14 / 3 =  4 Rest 2    |
 4 / 3 =  1 Rest 1    |
 1 / 3 =  0 Rest 1    |  => 42 = 1120
                                 ----->

Ternary -> Decimal

Number: 1120

1  1  2  0   
|  |  |   -------- 0 x 3^0  ->   0
|  |   ----------- 2 x 3^1  ->   6
|   -------------- 1 x 3^2  ->   9
 ----------------- 1 x 3^3  ->   27
                                    = 42
643 questions
2
votes
5 answers

Ternary operators with more than one variable

I have been introduced to the concept of ternary operators, and it's pretty straightforward to understand the notation: desired_variable = true ? false ? "value1" : "value2"; I could not understand, however, the rational behind adding a second…
cgobbet
  • 309
  • 4
  • 12
2
votes
2 answers

How to implement a ternary into on-click handler

Been working on this all morning and just can't seem to figure out what's wrong with this ternary expression. No errors are being recorded, but the first part of the expression never evaluates. Any ideas?
Kevin Hebert
  • 63
  • 1
  • 7
2
votes
3 answers

why is this ternary throwing an error instead of evaluating to false?

I am trying to test the truthiness of an object property. Whether it exists and has a value use that value and if it does not then add a default value to another object. const initNetwork = ( setupObj ) => { let obj = {}; obj =…
shaun
  • 1,223
  • 1
  • 19
  • 44
2
votes
3 answers

Operator precedence between await and ternary

I tried this with Chrome console: const foo = async () => 1; const bar = async () => 2; (await 1) ? foo() : bar(); The result is 1. Can you explain this please? Isn't it suppose to return a Promise?
eee
  • 280
  • 4
  • 15
2
votes
2 answers

In Swift, Why do I have to force unwrap optionals declared with a ! before using them inside a ternary expression?

This code doesn't work. It causes a segmentation fault. The entire point of initializing a variable with ! instead of ? is so I can use it with it automatically unwrapping itself. Why is this the case? var isFoo: Bool! override func viewDidLoad()…
Sethmr
  • 3,046
  • 1
  • 24
  • 42
2
votes
4 answers

Ternary operator for return values and brackets

Whenever I see people using the ternary operator they often allow the implicit conversion to a boolean true or false check, for example: int a = 5; return a ? true : false; However I've heard it's good practice in many cases to be explicit, for…
Zebrafish
  • 11,682
  • 3
  • 43
  • 119
2
votes
2 answers

UML - Association between 3 classes

I have a piece of statement that I don't understand how to model it in a class diagram. It is about Formula 1 races. The piece is the following: We can only have one race per circuit in each season So there is a relationship between CIRCUIT,…
asehu
  • 127
  • 10
2
votes
1 answer

compilation error with ternary operator

I tried the following with ternary operator and I do not understand why it is not compiling. The issue seems so small but I do not understand and hence bothers me - Line 1 --> int a = false ? y+=1 : (x*=10); Line 2 --> int b = false ? y+=1 :…
Rads
  • 53
  • 1
  • 6
2
votes
4 answers

Assignment from ternary operator ignores remaining expression

Doing this const bool = true; const a = 2 + true ? 3 : 0; const b = 2 + false ? 3 : 0; gives me a = 3 and b = 0. Why is the 2 + being ignored?
ghirlekar
  • 1,071
  • 1
  • 9
  • 10
2
votes
1 answer

WebStorm inspection on comma expression in ternary JavaScript operator

I am using quite a few ternary operators instead of select case statements. These work fine to my best knowledge but I get now a lot inspection warnings from WebStorm telling me that comma expressions could be overly clever and may lead to subtle…
Dennis Bauszus
  • 1,624
  • 2
  • 19
  • 44
2
votes
1 answer

Declare variable with condition

var foo, bar; foo = 'hello'; // first bar = 'world'; // otherwise var baz = foo || bar; console.log(baz); wondering whats the PHP way of doing the above? I already know about the ternary operator: $foo = $bar = ''; $foo = 'hello'; // first $bar…
eozzy
  • 66,048
  • 104
  • 272
  • 428
2
votes
2 answers

Ternary in WHERE condition (MySQL)?

MySQL (5.7.x): Is there anyway to do a ternary argument in WHERE conditions? I'm trying to run a AND clause based on the value of the select: SELECT DISTINCT p.user_id, eml.template, eml.subject FROM order_lines ol, email_template eml …
guice
  • 976
  • 4
  • 11
  • 31
2
votes
2 answers

c++ ternary operator use

I am working in C++. I have two statements involving the ternary operator. std::stack s; int depth = 0; /*modify depth integer and stack size, based on height of BST*/ depth = s.size() > depth ? s.size() : depth; std::stack s; int…
lfitz
  • 35
  • 5
2
votes
1 answer

Is there a simple way to test if you match one of a set of enumerations?

Consider this code... switch(testValue) { case .ValueA, .ValueB, .ValueC: return 40 default: return 0 } Now if I was just checking for a single enumeration value, I could do this... return (testValue ==…
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
2
votes
3 answers

How to correctly do Nil Coalescing in Swift?

Supposed to do like this var inputField = UITextField() let defaultText = "PLACEHOLDER" let newText = inputField.text!.isEmpty ? defaultText : inputField.text! let newText2 = inputField.text ?? defaultText newText works, and outputs PLACEHOLDER.…