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

c++ ternary operator expected an expressionC/C++(29)

double recursively(int n) { (n==0)?(return 0.0):((n==1)?(return 1.0):(return 2*recursively(n-1)+recursively(n-2))); } When I use the ternary operator in a recursive function it is not showing "expected an expressionC/C++(29)" error. I am using…
-2
votes
1 answer

Problem with using conditionals inside template literals

How can I make ternary operator work if I have the following codes? '

{balance==0?Not available:(Stock: {balance}})

' Thank you! Sorry about that, maybe my question is not clear. Let me elaborate more here I'm…
user610983
  • 855
  • 2
  • 9
  • 14
-2
votes
2 answers

Beautify Mix of PHP and HTML statement

I want to write this line in one outer php tag. May be we can use echo statement with concatination? Or someone can use ternary statement to beautify it?

hammadshahir
  • 350
  • 4
  • 7
-2
votes
2 answers

Can we use command in ternary operator (Java)?

This is a working code: String a = "first"; String b = "second"; String object; System.out.println(object != null ? a : b); But it isn't: String a = "first"; String b = "second"; String object; object != null ? System.out.println(a) :…
hazazs
  • 39
  • 1
  • 8
-2
votes
1 answer

Java, convert Ternary operator to IF ELSE

I am sure this repeats something, but yet due to short time I have to ask you straight forward help. How to convert Ternary (: and ?) operator into IF-ELSE statement in Java? public Integer get(Integer index) { if (first == null) { …
apex_point
  • 21
  • 5
-2
votes
1 answer

How to simplify ternary expression

Is it possible to simplify this min + ternary expression by a one liner? BigDecimal min = x.getMinimum(); BigDecimal result = otherValue.compareTo(min) > 0 ? otherValue : min;
DerBenniAusA
  • 727
  • 1
  • 7
  • 13
-2
votes
2 answers

ternary with string in asp net mvc razor

I have

"@(System.Configuration.ConfigurationManager.AppSettings["OST"] == "true" ? "Out-of-State Travel" : "Out-Service Training/Out-of-State Travel")"

however it is rendering with the quotes it…
Bryan Dellinger
  • 4,724
  • 7
  • 33
  • 79
-2
votes
1 answer

Why is this ternary operator not working?

I would think this variable would be pretty straight forward but it's not working? It does echo the row as need if it's set but if it's "false" or not set it does not print "N/A"? Am i doing something wrong here? $term = isset($row['term']) ?…
gillweb
  • 91
  • 7
-2
votes
2 answers

Array.prototype.some not working with ternary operator

Why do the following two snippets not return the same value? [1,2,3,4].some((el) => { if (el === 4) { return true; } else { return false; } }); --> returns true [1,2,3,4].some((el) => { el === 4 ? true :…
Javiliano
  • 41
  • 4
-2
votes
1 answer

Ternary operator creating temporaries for returned references in C++

Why is the ternary operator creating a temporary variable for a& to reference in the case of: SeqNo &fb(int x); SeqNo &fc(int x); SeqNo &a = cond ? fb(y):fc(y); Note that the code updates a later i.e. a= value; fails to update the original…
Andrew Goedhart
  • 937
  • 9
  • 17
-2
votes
3 answers

Can we run for loops inside a conditional operator (? :)?

i have this code: function f(n){ var p = 0; if(n % 2 === 0 && n > 0) { for(let i = 1; i <= n; i++) { p += i; }; } else { return false; } return p; }; And was wondering if it was possible to convert to ternary operator to shorted…
tygar
  • 244
  • 1
  • 4
  • 15
-2
votes
3 answers

Reading and understanding ternary operators?

I'm trying to understand ternary operators and don't see an example with return statements. return (next == null) ? current : reversing(current,next); How would you write that without the ternary operator? Is it just: if (next == null) { } else…
chatslayer
  • 47
  • 3
-2
votes
4 answers

How to use ternary operator with new?

I have this code: if (providers.length > 0) this.providers = providers; else throw new IllegalArgumentException(); And i want to simplify it. I went for: (providers.length > 0) ? this.providers = providers : throw new…
user3756506
  • 431
  • 1
  • 3
  • 12
-2
votes
1 answer

adding more than 1 ternary operator inside echo

im trying to add more ternary operator inside an echo but i dont know how. below is my current codes echo "
adrian pascua
  • 3
  • 1
  • 1
  • 5
-2
votes
2 answers

How to edit this javascript ternary function to return nothing if first result is false?

function scrollTagsPanel(event) { reachedBottom() ? loadMoreTags() : console.log('keep scrolling'); function reachedBottom() { return tagsCol.scrollHeight - tagsCol.scrollTop === tagsCol.offsetHeight; } function…
Leon Gaban
  • 36,509
  • 115
  • 332
  • 529