C++
conditional statements
sign equivalence
programming
number comparison

Three-way conditional in c to determine sign equivalance of two numbers

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

To determine whether two numbers have equivalent signs, you first need to define what “equivalent” means for zero. Once that rule is clear, the code is usually much simpler than a deeply nested conditional expression. In C or C++, the best solution is often a direct boolean comparison, not an elaborate ternary chain.

Define the Zero Rule First

There are at least two reasonable interpretations:

  • zero matches only zero,
  • zero is considered non-negative and therefore matches positive numbers.

If you do not define that rule first, every implementation discussion becomes muddy.

For many numeric tasks, the strict rule is better:

  • positive matches positive,
  • negative matches negative,
  • zero matches zero only.

Clear Boolean Solution

A simple and readable implementation:

cpp
1#include <iostream>
2
3bool same_sign(int a, int b) {
4    if (a == 0 || b == 0) {
5        return a == 0 && b == 0;
6    }
7
8    return (a < 0) == (b < 0);
9}
10
11int main() {
12    std::cout << same_sign(5, 10) << '\n';
13    std::cout << same_sign(-3, -8) << '\n';
14    std::cout << same_sign(0, 0) << '\n';
15    std::cout << same_sign(0, 7) << '\n';
16}

This version is usually preferable to a nested ternary because the intent is obvious.

If You Really Want a Ternary Expression

A ternary form is possible, but it is not automatically better.

cpp
1bool same_sign(int a, int b) {
2    return (a == 0 || b == 0)
3        ? (a == 0 && b == 0)
4        : ((a < 0) == (b < 0));
5}

This is concise, but if the logic becomes any more complicated, readability drops quickly. The ternary operator works best when both branches are very simple.

Alternative: Explicit Sign Function

For reusable numeric code, a sign function can make the comparison clearer.

cpp
1int sign_of(int x) {
2    return (x > 0) - (x < 0);
3}
4
5bool same_sign(int a, int b) {
6    return sign_of(a) == sign_of(b);
7}

Here:

  • positive maps to 1,
  • zero maps to 0,
  • negative maps to -1.

This is compact and general. It also avoids special-case code in the comparison itself.

Avoid Multiplication-Based Tricks

Some people write:

cpp
bool same_sign = a * b > 0;

This is risky because:

  • it fails for zero depending on your intended rule,
  • multiplication can overflow for large integers,
  • the intent is less explicit.

Even if overflow is not a problem for your current inputs, it is still a weaker expression of the logic than comparing sign categories directly.

Choosing the Best Style

Use plain boolean logic if the team values readability.

Use a sign_of helper if the codebase performs many sign-based comparisons.

Use a ternary only when the expression stays short and obviously readable.

The shortest code is not always the clearest code.

Generic Version

If you want a reusable C++ template for ordered numeric types:

cpp
1template <typename T>
2int sign_of(T x) {
3    return (T(0) < x) - (x < T(0));
4}
5
6template <typename T>
7bool same_sign(T a, T b) {
8    return sign_of(a) == sign_of(b);
9}

This works well for integers and many other ordered numeric types, as long as your zero rule matches the sign_of semantics.

Common Pitfalls

  • Writing the condition before deciding how zero should be treated.
  • Using multiplication-based checks that can overflow or hide the real intent.
  • Nesting ternary operators so deeply that the sign logic becomes harder to audit than an if statement.
  • Assuming “same sign” is universally defined when different domains treat zero differently.
  • Optimizing for fewer characters instead of for readable numeric logic.

Summary

  • The key design choice is whether zero should match only zero or also non-negative values.
  • For most code, direct boolean comparison is clearer than a complex ternary expression.
  • A sign_of helper is a clean reusable approach.
  • Avoid multiplication tricks because they are less explicit and can overflow.
  • Choose the simplest expression that matches the domain’s sign semantics clearly.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.