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
1 answer

Strange behaviour of ternary plot in shiny app

I want to include a ternary plot in a shiny app. I'm use package ggtern. When I run the following code: dd <- data.frame(x=c(3,1,5), y=c(45,29,10), z=c(10,45,94), ss=c(58,75,109)) ggtern(data=dd, aes(x=x,y=y,z=z)) + …
marxco
  • 121
  • 1
  • 3
2
votes
3 answers

Conditionally instantiate a new Perl array

Initially, my code looked like this: my @departments = @{$opts->{'d'}} if $opts->{'d'}; I wanted to refactor the in-line if statement according to Perl Best Practices, so now I've got the following code: my @departments; if( $opts->{'d'} ) { …
s_dolan
  • 1,196
  • 1
  • 9
  • 21
2
votes
1 answer

how to implement symmetric modulo operation?

I require symmetric modulo operator for balanced ternary number system; It can be used to calculate least significant trit of trits sum (ternary addition function); My implementation seems inefficient, it uses 2 modulo(%) operators, and several…
xakepp35
  • 2,878
  • 7
  • 26
  • 54
2
votes
4 answers

Ruby ternary operator if else

How would you make this ternary? if @user.skill.present? @skill = @user.skill else @skill = Skill.new end
pipinha
  • 195
  • 1
  • 10
2
votes
3 answers

Perl Ternary statement with regex as one of its results

I currently have the following code: my $class = $rs->{'CLS_ID'}; $class =~ s/^C\S{1,3}\s+// if ($transform); This works fine, but I was wondering if those 2 statements could be combined into a single ternary expression?
Timmah
  • 2,001
  • 19
  • 18
2
votes
2 answers

Bash ternary operation gives same output on either Boolean condition

So in my bash script, I output status report to terminal as well as write it to the log file. I wanted to use a bash ternary operator that will output to terminal as well as write a log file if variable LOG_TO_TERMINAL is true, and if that is set to…
THIS USER NEEDS HELP
  • 3,136
  • 4
  • 30
  • 55
2
votes
1 answer

Polymer: ternary operator vs tokenlist?

Polymer expressions allow you to write ternary operators. There is also a tokenList filter that can be used specifically on the class attribute. It seems to me that the following two render the same thing. ternary operator:
2
votes
6 answers

How to write fibonacci-sequence using recrusion in Ruby?

I have tried to create a method in Ruby which returns true if number is included fibonacci-sequence or false if it's not. I think I got a problem when I return true or false. Could anybody tell me why the first code doesn't work please? def…
user4069050
2
votes
1 answer

Other ternary operations than conditional expression

I read on Wikipedia that there are often only one ternary operation type possible in C-like languages, which is the conditional expression. I am trying to find out what other ternary operation exists and what language would make use of them.
B7th
  • 644
  • 1
  • 6
  • 17
2
votes
2 answers

What in the world is this embedded ternary executing?

I have inherited some absolutely god-awful code from a vendor and between being a relative rookie in Javascript and the atrocious way this is written, I have managed to stump the entirety of my office with what this code is supposed to mean. Can…
2
votes
1 answer

Is there a `?.` operator in Python?

Groovy has a very handy operator ?.. This checks if the object is not null and, if it is not, accesses a method or a property. Can I do the same thing in Python? The closest I have found is the ternary conditional operator. Right now I am doing l =…
Sergey Orshanskiy
  • 6,794
  • 1
  • 46
  • 50
2
votes
3 answers

Ternary Operator Simplification : Remove Repitition

Curiously, is there a shorter way to write this in one line without having to reference the node twice? I find myself doing a lot of these in parsing. lidID.idCountry = (passportnode.Descendants("COUNTRY").First().Value != String.Empty) ? …
Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
2
votes
1 answer

PHP shorthand (Ternary) function

In the javascript world we can run a function through a Ternary comparison, I wanted to see if this applies to PHP, it seems it does only to an extent. This is not for production use nor will it be actually used. This is merely a topic to see PHP's…
Michael Mikhjian
  • 2,760
  • 4
  • 36
  • 51
2
votes
2 answers

Which ternary operator in C# is most popular and mostly used?

Which ternary operator in C# is most popular and mostly used?
Gupta Ji
2
votes
4 answers

Ternary operator evaluation order

class Foo { public: explicit Foo(double item) : x(item) {} operator double() {return x*2.0;} private: double x; } double TernaryTest(Foo& item) { return some_condition ? item : 0; } Foo abc(3.05); double test = TernaryTest(abc); In…
Chris Bednarski
  • 3,364
  • 25
  • 33