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

How to change order of axis ticks for a ternary diagram?

Is it possible to reverse scale, for example for AC from 0.1-1 to 1-0.1?
Ann
  • 73
  • 1
  • 6
3
votes
1 answer

TypeScript Generic to change all types of a particular type to another

I'm trying to come up with a typescript type that takes an object and sets all keys of type number to be string. The following doesn't work: export type ChangeTypeOfKeys = { [key in keyof T]: key extends number ? string :…
tic
  • 137
  • 12
3
votes
2 answers

Is there a better solution for this ternary condition?

Imagine the following ternary condition: foreground = self.foreground if self.foreground else c4d.COLOR_TRANS In this case, I need to call self.foreground twice just to check if it is True or not. Is there a way where I only need to call it once ?
Niklas R
  • 16,299
  • 28
  • 108
  • 203
3
votes
0 answers

Where can I find more information on the C/++ operators ?=

Possible Duplicate: What does the >?= operator mean? I was reading some C++ code and I saw the operators ?=. From their usage it appears that a ?= b is a = (b > a) ? b : a. Are these…
Goldy
  • 31
  • 1
3
votes
5 answers

How to create a ternary condition on a bit field in T-SQL

I have a SQLExpress table that includes a bit field for storing TRUE/FALSE state. Something like: +----+---------+ | ID | IsAlive | +----+---------+ | 1 | 1 | | 2 | 0 | | 3 | NULL | | 4 | 1 | +----+---------+ Using that…
Jed
  • 10,649
  • 19
  • 81
  • 125
3
votes
3 answers

return (n > 2) ? n = 5 : n = 4; doesn't work?

Why does this return an error return (n > 2) ? n = 5 : n = 4; but this does not return (n > 2) ? n = 5 : n + 4; Should it not be able to return n depending on either case?
3
votes
3 answers

?? operator in system.DBNull

Is there an operator or built in function to simplyfy this: myVal = object1.object2.something(a,b).dataColumn.toString()==""?object1.object2.something(a,b).dataColumn.toString():"-"; I know i can do something like: string str =…
tutetimas
  • 31
  • 3
3
votes
1 answer

Ternary Operators in C

#include int main() { int x = 1; short int i = 2; float f = 3; if (sizeof((x == 2) ? f : i) == sizeof(float)) printf("float\n"); else if (sizeof((x == 2) ? f : i) == sizeof(short int)) printf("short…
Stack
  • 111
  • 6
3
votes
2 answers

Ternary operator in map() react

I made an application in React that shows state holidays using state codes. Now I'm wondering how to handle error when a user types in a non-existent state code or more characters. Here's my try to do it. import React from "react"; const Holidays =…
branen890
  • 41
  • 1
  • 1
  • 5
3
votes
1 answer

JavaScript ternary operator and if/else statement

Can anyone explain to me what is the difference between these two statements and why the second one does not work and the first one does: if (finalWord.length > 140) return false; else return finalWord; (finalWord.length > 140) ? false :…
Dito
  • 915
  • 2
  • 10
  • 26
3
votes
3 answers

React - Using ternary to apply CSS class in functional component

I'm relatively new to React and working on a John Conway - Game of Life app. I have built a Gameboard.js functional component for the board itself (which is a child of App.js) and a Square.js functional component which represents an individual…
Nick Kinlen
  • 1,356
  • 4
  • 30
  • 58
3
votes
2 answers

JavaScript Ternary for function names?

I’ve come across the situation multiple times that I call a function based on a binary situation where ternary would work fine: buttonPressed ? files.audio.current.play() : files.audio.current.stop() But see all that extra wasted code? Is there a…
Kjell Connelly
  • 247
  • 3
  • 9
3
votes
4 answers

Concatenation of a string using ternary operator and recursion

I try to understand this one. It is an example from advanced javascript by John Resig. function yell(n) { return n > 0 ? yell(n-1) + "a" : "hiy"; } alert( yell(4) ); As a beginner I somehow would use a temporary variable to save the string and…
Fritzl
  • 43
  • 7
3
votes
5 answers

Incrementing different variables via the ternary operator in a for loop

I'm trying to add to a different variable based on a test, within my for loop: for i in range(alist.__len__()): if alist[i] is not blist[i]: ascore +=1 if alist[i] > blist[i] else bscore+=1 print(ascore,bscore) This code…
Eredea
  • 97
  • 1
  • 7
3
votes
3 answers

Reference error (left-hand asignment) in array assignment

I have the following code, where I'm trying to execute a function with the help of a parameter. The parameter tells Javascript to perform the operations on the arrays/strings that it indicates (e.g. ps array and properties string if the id parameter…