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

Multiple if else conditions in Thymeleaf

I'm trying to append a css class depending on multiple if cases with the ternary operator. Is my ternary wrong? I get the below error: Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression error. If I look…
2
votes
1 answer

Ternary plot from plotly

enter image description here I want to add lines (horizontal, vertical) into ternary diagramm to highlight some parts. Could help me please? The ternary diagram The code is bellow: fig = px.scatter_ternary(df,a=df.a, b =df.b, c=df.c,…
Ann
  • 73
  • 1
  • 6
2
votes
4 answers

Fast sum of digits in a ternary representation (Python)

I have defined a function def enumerateSpin(n): s = [] for a in range(0,3**n): ternary_rep = np.base_repr(a,3) k = len(ternary_rep) r = (n-k)*'0'+ternary_rep if sum(map(int,r)) == n: s.append(r) …
wcc
  • 196
  • 8
2
votes
1 answer

What makes a dot plot on top of another on geom_point? And how to change it?

What makes a value-dot plot overlap and plot on top of another on the ggtern plot? And how can I change it? I would like to have my selected coloured observations plotted on top of the black ones. (On my data I have 1400 observations and even with…
Ecg
  • 908
  • 1
  • 10
  • 28
2
votes
1 answer

Condition evaluating incorrectly (Lua 4)

I am trying to use this function in Lua 4: function ternary(cond, T, F) if cond then return T else return F end end In this context: loadHW1 = false print(ternary(loadHW1 == true, "this should not appear", nil)) However, the text always gets…
posfan12
  • 2,541
  • 8
  • 35
  • 57
2
votes
4 answers

Javascript. Why this condition s[i+1] don't give an error "out of range"?

var romanToInt = function(s) { value = 0; for (let i = 0; i < s.length; i += 1) { symbols[s[i]] < symbols[s[i + 1]] ? value -= symbols[s[i]] : value += symbols[s[i]] } return value }; This is an leetcode exmple, I am confused with this…
Blacktea
  • 83
  • 6
2
votes
4 answers

using += and ternary operator in java clarification

I was running through some practice questions for an upcoming exam and came across a question that me nor my class mates can seem to understand. It is as follows: where all variables are int or int array. score += (rounds[i]) ? i + START : 0 How…
James_B
  • 137
  • 7
2
votes
4 answers

Java - Use ternary operator on char input to get boolean

Looking to do something like in C#: bool walkable = t.Type == TileType.Green ? true : false; but in Java Boolean international_F = (in.next() == 'Y') ? true : false; The above is what I've tried so far. Wondering if it's even possible. EDIT: I…
Edward Suzuki
  • 103
  • 2
  • 9
2
votes
2 answers

How to pass a prop with a default value in case the prop is empty without using the prop's "default" property

I'm using a third party multi-language package where translation values can only be obtained/used from component's template, not from component's logic (at least I can't find how to the latter). I need to pass such translation value in some…
drake035
  • 3,955
  • 41
  • 119
  • 229
2
votes
0 answers

How to modify plot axis ranges of a Ternary plot in R

I am trying to create a ternary plot in R using the Ternary package. The range of points to be plotted is relatively restrained as you can see from the picture below. I would like to rescale the triangle so that the a-axis (Al2O3) range is between…
2
votes
3 answers

Simply if statement to a Tenerary in Ruby

I'm new to Rails and was wondering about the following: Is there a way to simplify this further? animal = Animal.find_by(name:name, type:type) if !animal animal = Animal.create(name:name, type:type) I was thinking of using a ternary…
lost9123193
  • 10,460
  • 26
  • 73
  • 113
2
votes
2 answers

Ternary expression with a javascript object

Let's say I have the following javascript object: var __error__ = {0: 'ok'} I want to return an empty string if the key is not in the obj, and an error message otherwise. For example: var value = __error__[col_num] ? "The value cannot be converted"…
user12282936
  • 167
  • 2
  • 7
2
votes
1 answer

C Fast base convert from decimal to ternary

is there any method of changing decimal number to ternary ? I mean i don't want to use modulo and divide method, i have very big decimal number, something like 128123832812381835828638486384863486.............1237127317237 and so on. Also don't…
Krzysztof Lewko
  • 982
  • 7
  • 24
2
votes
3 answers

How many ternary operator used in computer language?

I was curious that are there any ternary operator being used in programming language except ?: operator. And could found only 2 from wikipedia Is it only operator we have been used? Are there any more than these?
Thaina Yu
  • 1,372
  • 2
  • 16
  • 27
2
votes
4 answers

Is it ok to use count + 1 in ternary operator?

What I want to do is check if there is element in array, if it is, I want to increment counter. Compiler reports error when I do count++ but not when I do count + 1, and it increments correctly. Is it because count++ is operation and not an…