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
2 answers

Ternary and print combination giving odd results

I was trying to use a ternary to print out a string if something was true and something else if it wasn't (simple enough, right?). It gives no errors and doesn't seem like it would cause any trouble. However, when…
Vreality
  • 305
  • 5
  • 16
2
votes
3 answers

Unusual use of the Ternary Operator

I'm a newbie to C, I understand why ternary operators can be useful, less code than if/else blocks. I have been given some C code to maintain, and one thing I've noticed is the previous programmer used ternary operators like this myInt = (!myInt) ?…
Doug Molineux
  • 12,283
  • 25
  • 92
  • 144
2
votes
2 answers

Seemingly redundant ternary operators in javascript

The following is common code floating around online that checks if cookies are enabled in a particular browser: var cookieEnabled = (window.navigator.cookieEnabled) ? true : false; if (typeof navigator.cookieEnabled == "undefined" &&…
rjkaplan
  • 3,138
  • 5
  • 27
  • 33
2
votes
4 answers

Best way to do ternary conditionals in Python < 2.5

I have to bear with a Python version < 2.5 (it is 2.4.3 for specifics) It seems that ternary operators were introduces in Python starting at 2.5. For those who are not familiar, ternary operators in Python >= 2.5 look like this: def…
Charles Menguy
  • 40,830
  • 17
  • 95
  • 117
2
votes
2 answers

Operator precedence and ternary operator

I have a problem in C. #include int main() { int a = 10, b = 0, c = 7; if (a ? b : c == 0) printf("1"); else if (c = c || a && b) printf("2"); return 0; } This code prints 2 but I think a?b:c returns b=0 and…
user1094138
  • 59
  • 2
  • 6
1
vote
2 answers

Unexpected Result, Ternary Operator in Gnu C

So the operator precedence of the ternary operator in C seems truly bizarre to me. Case in point: #include int main () { int i=5; int j=6; int k=7; printf("A: %d\n", i+j+(k!=7)?1:11); //prints 1 printf("B: %d\n",…
1
vote
4 answers

Something we found when using comma in condition ternary operator?

Well, I had a question about comma in ternary operator. Cut the crap, the code is below: void test_comma_in_condition(void) { int ia, ib, ic; ia = ib = ic = 0; bool condition=true; cout<<"Original:"<
Joseph
  • 51
  • 1
  • 3
1
vote
1 answer

Color tiles (triangles) of a categorical ternary/simplex plot according to numeric values

I have a data set with combinations of 3 categorical variables (each with 6 classes) and a continuous value (ranging from 0 to 1) that belongs to these class combinations. In R, this data looks like this (example data): grid <-…
Anti
  • 365
  • 1
  • 14
1
vote
2 answers

The ternary operator overhead of ITC for all of its operands

I cannot help myself but struggle to understand why is the ternary conditional's 3rd operand (the false-condition expression) subject to implicit type conversion even when the control condition evaluates to true and vice-versa? I can probably only…
Edenia
  • 2,312
  • 1
  • 16
  • 33
1
vote
0 answers

Is there a Rubocop cop for question mark method with ternary operator?

I regard it to be really confusing to use predicate methods (by convention ending with a question mark) in combination with ternary operator. In my opinion, using thet ternary operator here is bad formatting due to the double question marks it leads…
lovet
  • 11
  • 2
1
vote
0 answers

Ternary contours with plotly in R

Ternary contour plots w/ interpolation etc. are quite easily to create in plotly with Python. Though, the main functionality of ternary plots is already available for plotly in R, I am missing functionalities for interpolation, etc. to create plots…
moremo
  • 315
  • 2
  • 11
1
vote
1 answer

Why is my JS Ternary ignoring the conditional argument where the affirmative should execute a map.set(k,v) function?

The goal is to map data- attributes from one element to another, but while ignoring certain attributes such as class, id, etc. Here is a block: let ignoreList = ['class', 'id', 'name', 'value', 'type', 'src']; …
brian-welch
  • 441
  • 1
  • 7
  • 19
1
vote
2 answers

Filter Method Behavior

Why filter method can't work with ternary conditional as it works with if conditionals ? In case it works with if..else let numbers = [-1, -2, -3, 1, 2, 3]; let negatives = []; let positives = numbers.filter(num => { if(num > 0) { return…
1
vote
2 answers

Using empty char in C

I want to use the ternary operator and an empty char '' to spell correctly the word "neightbor" (with or without a "s"). I want to do the following : printf("There is %d neightbor%c\n", nbNeighbors, (nbNeighbors>1)?'s':''); obviously, I get an…
1
vote
1 answer

I'm trying to update a className in a React component, using itenerary statement but for some reason the className is not updated. How come?

This is a simple todo list app. I'm using a ternary condition in order to update a child component (third line). once the user clicks on the second button (last button), a function in the parent component will be called (passed as a prop property)…