Skip to main content

Questions tagged [operators]

Regarding programming languages, operators are constructs which behave generally like functions, but which differ syntactically or semantically from usual functions. From Wikipedia: http://en.wikipedia.org/wiki/Operator_%28programming%29

Filter by
Sorted by
Tagged with
0 votes
1 answer
581 views

In C/C++ and Java, why does dividing an integer by an integer return an integer, while in other languages, it returns a decimal value? [closed]

I have found another similar question, but the answer there talks mainly about C/C++, and does not mention about the division operator. So, in C/C++ and Java, dividing an integer by an integer returns ...
Arunabh's user avatar
  • 143
1 vote
3 answers
415 views

What is the problem with a range operator?

Virtually all (imperative) languages offer the operators < <= >= > for lower, lower or equal, greater or equal and greater. But why no ternary range operator like (10 < x < 100) to ...
theking2's user avatar
  • 139
2 votes
3 answers
436 views

C# Duplicated usage of an if/else condition and a ternary operator. A good practice?

I had a debate with a work mate regarding the following code as to which one of it would be the better practice: My code (in pseudo): var A = <precondition either TRUE or FALSE>; var B; if(A) { ...
Chams's user avatar
  • 29
0 votes
1 answer
257 views

Excessively verbose and cryptic comparisons in Java

I don't know if this is the right place to ask more of a "philosophical" question. The more I code in Java, the more I have to bear with Comparable<T>. And the more I bear with this ...
Thomas Herondale's user avatar
0 votes
4 answers
260 views

Language design : use equals symbol = both for affectation and comparison, like in MySQL

I'm currently designing a database query language and I came to wonder what should be the best syntax for the comparison operator. Most modern languages use ==, but amongst the database languages ...
ibi0tux's user avatar
  • 241
18 votes
3 answers
2k views

Should wrappers compare as equal using the == operator when they wrap the same object?

I'm writing a wrapper for XML elements that allows a developer to easily parse attributes from the XML. The wrapper has no state other than the object being wrapped. I am considering the following ...
John Wu's user avatar
  • 26.9k
3 votes
1 answer
202 views

Should equality operators be approximate when defining numeric types that rely on floating-point numbers?

For example, say you have a class Point which has floating point components. It's tempting to overload the equality operator so you can do something like a = Point(1.1, 2.2) b = Point(3.3, 6.6) b /= ...
Josie Thompson's user avatar
4 votes
7 answers
3k views

Why don't languages use the words "and" and "or" instead of "&&" and "||"?

When I was a beginner it took a while to learn the language syntax and the idea that languages couldn't improve after they were invented. But now we're seeing new language features added every year ...
1.21 gigawatts's user avatar
2 votes
1 answer
257 views

Good way to do 3D vector math in language without operator overloading

I would like to make a simple web application (a static website where all computation happens on the client) that generates a mesh and displays it. I have a working prototype in Unity and now I'm ...
Toast's user avatar
  • 128
1 vote
2 answers
1k views

Solving issues in using post and pre increment operators as part of expressions

I recently had a discussion with a friend about code maintainability with regards to modifying an iterator inside of the body of a loop (C# syntax): List<int> test = new List<int>(); for (...
Taco's user avatar
  • 1,165
0 votes
2 answers
878 views

Why was the caret used for exponentiation in BASIC?

As far as I have been able to find, the first language to use ^ for exponentiation was BASIC, in 1964. Earlier languages, such as Fortran, used other symbols such as ** for exponentiation (although in ...
CoffeeTableEspresso's user avatar
3 votes
1 answer
110 views

Should I replace a constant with static methods, if that constant usually 'cooperate' with a specific operator?

For example, to convert between g and kg, I have a constant 1000: public static final float G_TO_KG=1000; . . . this.result = someResult*1000; I found G_TO_KG always bind to operator '*'. So my ...
ocomfd's user avatar
  • 5,722
2 votes
4 answers
4k views

Why does C provide both the comma operator and the semicolon to separate statements?

Both the comma operator and the semicolon can be used to separate statements. Let's consider this simple code: #include <stdio.h> void do_something(int*i) {(*i)++;} int main() { int i; ...
gaazkam's user avatar
  • 4,509
58 votes
8 answers
19k views

Is there a keyword or operator for "nor"?

Is there an operator equivalent of nor? For example, my favorite color is neither green nor blue. And the code would be equivalent to: // example one if (color!="green" && color!="blue") { ...
1.21 gigawatts's user avatar
62 votes
3 answers
11k views

&& and || are not logical but conditional operators?

I am a bit confused by the MSDN C# documentation which states that & and | are logical operators and that && and || are conditional operators. I keep calling &&, || and ! logical ...
John V's user avatar
  • 4,936
2 votes
2 answers
518 views

Is implementing operator overloading with variable arity possible?

Implementing operator overloading for e.g. + (plus) isn't terribly difficult if you know it's a binary operator. One can just parse expression + expression But what if the programmer can choose ...
Mark's user avatar
  • 682
2 votes
0 answers
410 views

What programming language first used the 'Safe navigation operator' (?.)?

What programming language first used / invented the 'Safe navigation operator' (?.) ? Unfortunately, the Wikipedia page on this subject does not have information on it and googling yielded no readily ...
Daniel's user avatar
  • 136
1 vote
2 answers
2k views

What are the most idiomatic abbreviations for comparison operators [closed]

I am wondering what the most idiomatic text abbreviations for comparison operators are. I need to build a custom enum with them, so I cannot use their symbols. To me personally, these seem idiomatic: ...
Felk's user avatar
  • 194
0 votes
4 answers
2k views

Is there a way to see how many operations a function, block of code or a statement costs?

Let's say I have a phone that can process 1 million operations per second and a micro controller that can perform 1000. Is there a way to tell how many operations a performed by a function or block ...
1.21 gigawatts's user avatar
3 votes
1 answer
6k views

Can I overload operator[][]?

I'm writing a Matrix type, and I would like the following code to define a 2,2 integer matrix, put its first element at 3, and throw an exception upon reaching the third line: Matrix<int> a(2,2)...
qreon's user avatar
  • 339
3 votes
5 answers
2k views

Do increment and decrement operators decrease readability? [closed]

I understand what increment and decrements operators are (++ and --) and the difference between post and pre (i++ vs ++i) but should they be avoided as they do increase the difficulty of reading the ...
Celeritas's user avatar
  • 595
4 votes
3 answers
188 views

Could one swap the direction of >> and << overloads to define them as class members?

Some operators cannot be overloaded as class members. One such example are the bitwise shift operators used for streams see here for example. The reason for that (as far as I understand it) seems to ...
null's user avatar
  • 3,646
0 votes
2 answers
261 views

Operators precedence

I have a code snippet in Java: int y = ++x * 5 / x-- + --x; So my confusion was since x--(postfix) has higher precedence than ++x(prefix) operator so x-- should be executed first then ++x.But a ...
user1369975's user avatar
  • 1,299
9 votes
6 answers
2k views

In C, is * an operator, or part of a type in a declaration?

In C, * is called the indirection operator or the dereference operator. I understand how it works when it is used in a statement. It makes sense to write *p or * p, considering that it is a unary ...
Torm's user avatar
  • 151
2 votes
2 answers
31k views

IF statement with OR logical operator

Just a basic question on IF statements in programming languages, specifically C++. Consider the following basic code example: int i = 2; if(i == 2 || i == 4) { //do something } Because the ...
Bob's user avatar
  • 21
0 votes
2 answers
455 views

Could Java XYZ implement (limited) Operator Overloading without breaking backwards compatibility?

Recently, I've been learning a bit more of C++ and the dangers and uses of operator overloading, and the readability boost it provides to arithmetic types (like Complex numbers). A while ago, I was ...
Alxe's user avatar
  • 113
1 vote
3 answers
870 views

Which programming languages support operators as first class citizens? [closed]

Which programming languages support operators as first class citizens? eg: Return an operator (+, -, =, ==, etc.) from a function, or store within a variable.
Jordan Mack's user avatar
43 votes
10 answers
9k views

Why do most mainstream languages not support "x < y < z" syntax for 3-way Boolean comparisons?

If I want to compare two numbers (or other well-ordered entities), I would do so with x < y. If I want to compare three of them, the high-school algebra student will suggest trying x < y < z....
JesseTG's user avatar
  • 657
44 votes
4 answers
9k views

Why do so few languages with a variable-type 'operator' exist?

I mean it in this way: <?php $number1 = 5; // (Type 'Int') $operator1 = +; // (Type non-existent 'Operator') $number2 = 5; // (Type 'Int') $operator2 = *; // (Type non-existent ...
kgongonowdoe's user avatar
-1 votes
1 answer
166 views

What is the difference between these two php operators? [closed]

What is the difference between these 2 PHP operators ? The first one is = and the second one is .=.
Red Baron's user avatar
62 votes
6 answers
13k views

Why is the minus sign, '-', generally not overloaded in the same way as the plus sign?

The plus sign + is used for addition and for string concatenation, but its companion: the minus sign, -, is generally not seen for trimming of strings or some other case other than subtraction. What ...
Digvijay Yadav's user avatar
1 vote
2 answers
2k views

Incrementing Strings in PHP (and Perl)

PHP allows you to increment strings. Why? Let's jump ahead a bit. Take the following code: $string = '9ZzZ'; echo ++$string; // 10AaA From a purist point of view this may seem like nonsense, ...
DanielM's user avatar
  • 253
-1 votes
2 answers
1k views

How does the post increment operator work in GNU C? [closed]

While the run the below program in Turbo C compiler, I am getting the expected output, however, when I run the same program it using "gcc" compiler in linux, it is giving an unexpected output! int ...
crown679's user avatar
13 votes
1 answer
1k views

Why it is not possible to overload compound assignment operator in C#?

The title is be misleading, so please read entire question :-). By "compound assignment operator" I have in mind a construct like this op=, for example +=. Pure assignment operator (=) does ...
greenoldman's user avatar
  • 1,543
3 votes
1 answer
1k views

What is the etymology of the "dot" operator for string concatenation?

Some languages have a . operator for string concatenation. The oldest language I could find that supports it is Perl. Was Perl the first to use it? Why was it chosen?
Tyler Holien's user avatar
93 votes
7 answers
17k views

Short circuit evaluation, is it bad practice?

Something that I've known for a while but never considered is that in most languages it is possible to give priority to operators in an if statement based on their order. I often use this as a way to ...
innova's user avatar
  • 1,041
14 votes
2 answers
9k views

When should I use C#'s implicit type conversion operator?

In C#, we can overload the implicit conversion operator like this (example from MSDN): struct Digit { /* ... */ public static implicit operator byte(Digit d) // implicit digit to byte ...
Mints97's user avatar
  • 799
1 vote
1 answer
2k views

What is the "->" symbol called?

I have seen the -> operator/symbol in Java 8 predicates recently and wondered what its name is. I know that it is used in lambda expressions, but I know that the symbol for lambda is λ, so that's ...
SamTebbs33's user avatar
3 votes
1 answer
820 views

Well defined mathematical operations for bearing (angle) class

I have written a class to represent bearings (angles with a nautical theme, and a specific normalisation range). In the program, it is necessary to perform some mathematical operations on them, so I'...
wakjah's user avatar
  • 133
0 votes
2 answers
262 views

Missing `$` like operators in Haskell

Below I've produced what I believe to be a summary of the standard $ like operators for various classes in Haskell. There's some gaps however. Following the applicative pattern, you would think those ...
Clinton's user avatar
  • 1,083
4 votes
2 answers
792 views

Implicit Conversion Operators for Value Types in C#

I’ve come across this blog article: Implicit Conversion Operators are Bad. The article discourages the use of implicit conversion with reference types. The article describes problems caused by ...
Nick Alexeev's user avatar
  • 2,532
3 votes
1 answer
956 views

How do you set and check a single bit in a programming language that does not support bitwise operations?

I'm using a programming language (more a scripting language) that does not support any bitwise operators like AND, OR, XOR, NOT (and shift as well). Common arithmetic and logical operations like + - *...
tigrou's user avatar
  • 279
0 votes
1 answer
237 views

Is it correct to call the assignment symbol an "operator" when it is actually a statement?

In some languages (C++, Java, Ruby, etc.) an assignment returns a value and can be used in an expression: x = (y = z); // ok, 'x' gets the value of 'z' In other languages (Ada, VHDL), an ...
rick's user avatar
  • 1,995
5 votes
4 answers
2k views

Lexing: One token per operator, or one universal operator token?

When lexing, what would be the best way to tokenize operators? Would one just create a BinaryOperator token, or a separate token for every single binary operator? Examples: PlusOperator, MinusOperator,...
Jeroen's user avatar
  • 613
14 votes
3 answers
42k views

Speeds of << >> multiplication and division

You can use << to multiply and >> to divide numbers in python when I time them I find using the binary shift way of doing it is 10x faster than dividing or multiplying the regular way. ...
Crizly's user avatar
  • 327
31 votes
3 answers
8k views

Why does the boolean type in C++ support ++ but not --?

Why does the operator -- not exist for bool whereas it does for operator ++? I tried in C++, and I do not know if my question apply to another language. I will be glad to know also. I know, I can ...
aloisdg's user avatar
  • 809
27 votes
9 answers
14k views

What is the benefit of having the assignment operator return a value?

I'm developing a language which I intend to replace both Javascript and PHP. (I can't see any problem with this. It's not like either of these languages have a large install base.) One of the things ...
billpg's user avatar
  • 611
0 votes
5 answers
1k views

Why is it bad to use redundancy with logical operators? [duplicate]

I'm moving over to work on a library that a fellow developer has been writing. It's full of == true and == false, which I find crazy frustrating to read. I've tried asking him to quit doing it, but ...
RodeoClown's user avatar
3 votes
3 answers
1k views

Specifics of Switch and If statements

Why do we need the switch statement if there is the if statement? Why can't we use several ifs like if(a==1) do this1; if(a==2) do this2; ... instead of switch (a) { case(1): {do ...
user114292's user avatar
5 votes
2 answers
2k views

Why isn't exponentiation hardware-implemented?

Why is there no exponentiation operation in hardware, even though many languages have builtin operators for it? Is it because even hardware implementations would need to use the same algorithm as ...
user1358's user avatar
  • 341