Meaning of a comma in a number [duplicate] - c++

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C++ Comma Operator
that probably a trivial question, but I don't know the answer. And this has been troubling me this afternoon.
I was just writing a function to convert RVB to YUV. Nothing really special, but mistakenly used the comma (,) instead of a dot in my numbers.
It compiles but the result was not what I expected, for example "-3713796" instead of a 0-255 range number.
(0,615*(double) 61) - (0,51498*(double) 61) - (0,10001*(double) 61)
So what does it mean ?
If it's not a compilation error, it's probably usefull for something but what?
Ps: I was using C++ with Qt.

You've accidentally used the comma operator. It evaluates its first operand and discards the result, and then evaluates the second operand and returns its value and type.
In my experience, it's most commonly used in loop statements. For other uses, see Uses of C comma operator

It's the comma operator which evaluates each operand and returns the rightmost. In this case it evaluated 0, then 615*(double) 61 and resulted in a value of that product.

Related

Why write `!!x`, when `x` will do? [duplicate]

This question already has answers here:
Double Negation in C++
(14 answers)
What is the usage of "!!" (negating twice)? [duplicate]
(3 answers)
Confused by use of double logical not (!!) operator [duplicate]
(5 answers)
Closed 2 years ago.
I often see experienced programmers write !!x, even though the expected expression is a Boolean (i.e., zero or not zero) and not an integer.
For example, a line from boost:
BOOST_ASSERT(!!p); // where `p` is a pointer
What's the point of !!p when just p will do?
What I understand by a Boolean parameter is an expression converted to a value of integral type, and the value is compared against zero, explicitly or implicitly (with if or its ternary operator equivalent).
Thus, anything that takes a Boolean and expects only 0 or 1 is wrongly implemented, if my understanding of Boolean is correct.
For clarification: it's obvious that ! converts to bool; the question is explicitly asking for why.
By default, BOOST_ASSERT(expr) expands to assert(expr). That C++ assert function takes a scalar argument, not bool. Thus, your statement, "the expected expression is a boolean," is not correct.
Classes in C++ can implement operator bool(); the basic_stream classes are examples of such: assert(std::cin) will not work. The !! operator forces the use of std::cin::operator bool(), and bool after !! will be evaluated to int 0 or 1. This is shorter than assert(bool(std::cin)).
If I were a boost author, I would expand BOOST_ASSERT(expr) to assert(!!(expr)) - as is done for ( BOOST_LIKELY(!!(expr)) ? ((void)0) ).
Closely related to the question
Warnings.
Some compilers issue warnings.
Overflow of the integer storing the Boolean.
I'll just quote the comment mentioning the behaviour
The compiler would treat the values the same either way if it's only used as an operand to an if or &&, but using !! may help on some compilers if if (!!(number & mask)) gets replaced with bit triggered = !!(number & mask); if (triggered); on some embedded compilers with bit types, assigning e.g. 256 to a bit type will yield zero. Without the !!, the apparently-safe transformation (copying the if condition to a variable and then branching) won't be safe.
Related
Some consider defining operator bool() to be "unsafe". And to have an idiomatical way to convert to bool, they define bool operator !().
So writing !!obj is to support such users. And it doesn't even hurt to do so!

In C++. whats the difference between (*a).b and a->b? [duplicate]

This question already has answers here:
What is the difference between the dot (.) operator and -> in C++? [duplicate]
(14 answers)
Closed 3 years ago.
I am learning C++ in a advanced programming class from my work since I have only worked in Web and .NET languages so far.
In a midway test the instructor has marked all of my uses of (*a).b as wrong and deducted points for it, which could negatively affect my final score and I need a near perfect score to transision in work from web stack to application stack, so could some of you help me resolve this dispute?
If a is a pointer, there's no difference in functionality at all, and in fact one is expressed in terms of the other [expr.ref§2]:
The expression E1->E2 is converted to the equivalent form (*(E1)).E2; the remainder of [expr.ref] will address only the first option (dot).
If a is an instance of a class with overloaded operators * and ->, there could be a difference. But such a discrepancy would be surprising, and I'd consider the class to have a bug because of this.
In the end, it's all about convention and readability then. The -> operator exists as a shorthand for the */. pair, as it is shorter and has better precedence rules (no need for parentheses). Thus, one would indeed use it rather than */..

What does ~(tilde) means before scanf function in c++? [duplicate]

This question already has answers here:
The tilde operator in C
(6 answers)
Closed 3 years ago.
I found a code that takes input string and print them out.
But I don't know what does the tilde means in front of the scanf.
I found tilde can be used for either destructor or binary negation but it doesn't look like both. And the code doesn't work without tilde.
int main() {
char arr;
while (~scanf("%c", &arr)){
putchar(arr);
}
}
I found tilde can be used for either destructor or binary negation but it doesn't look like both.
It's the bitwise NOT operator applied to the return value of scanf() as you mentioned latter.
And the code doesn't work without tilde.
As #Mukul Gupta explained in their comment:
scanf returns the number of values it scanned successfully or EOF
if it reaches the end of file. EOF is a macro that represents a
negative value. On most platforms, the value of EOF is (int) -1.
In this case, taking 1's complement of -1, will make the value as 0
and is used to break from the loop.

Odd variable assignment [duplicate]

This question already has answers here:
Uses of C comma operator [duplicate]
(20 answers)
Closed 9 years ago.
Good day, everyone.
I've come across a peculiar piece of code today, which I don't quite understand.
I don't even know how to search for this particular problem.
In this code, which works, a variable assignment is done like this:
if(condition) {
Var1 = false, Var2 = false;
}
Now, I was under the impression, that ALL commands need to be terminated by a semicolon instead of a comma. I am familiar with the syntax
Var1 = Var2 = false;
but not with the one posted above. The compiler (g++) doesn't even throw me a warning or anything...am I missing something from the specification here?
Or is the compiler generous with me and just replaces the , with a ; internally? If so, shouldn't he at least throw a warning?
Thank you for your time.
am I missing something from the specification here?
Yes, it's the "comma operator", specified by C++11 5.18. It evaluates the sub-expression to the left, then the one to the right, and the overall result is that of the right-hand one.
In this case, it's equivalent to two expression statements separated by ;
It's useful in places like if/while/for where you're only allowed one expression, but might want to do more than one thing:
while (++i, --j != 0)
and also if you like to jam multiple statements together to make life difficult for whoever has to read your code.
In the C and C++ programming languages, the comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type). (read more)
As Alexandru Barbarosie pointed out, there's quite a thorough explanation on what's happening at https://stackoverflow.com/questions/1613230/uses-of-c-comma-operator
To quickly summarize it for whomever stumbles across this post: When used outside of for loops and stuff, the , actually has the same effect as the ;.
For more information, please visit the link.

What does double not (!!) used on a non-boolean variable do? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Double Negation in C++ code.
I am working with production code where I have run across statements like this a few times:
Class.func(!!notABool);
The first couple of times I dismissed it as a programmer quirk (maybe to emphasize that it is a conditional statement rather than a number being passed into func?) but I have run across several statements that use the above and now I am wondering whether it actually makes a difference or not. In most cases notABool is a number(int, float, double... I have seen all 3) My initial guess was that it is akin to typing:
Class.func((bool)notABool);
but I am not entirely sure?
Yes, functionally it is exactly the same as doing (bool) notABool.
By definition, in C++ language the operand of ! is implicitly converted to bool type, so !!notABool is really the same as !! (bool) notABool, i.e. the same as just (bool) notABool.
In C language the !! was a popular trick to "normalize" a non-1/0 value to 1/0 form. In C++ you can just do (bool) notABool. Or you can still use !!notABool if you so desire.
For primitive types, yes, it's essentially equivalent to:
!(notABool != 0)
which in turn is equivalent to:
(bool)notABool
For non-primitive types, it will be a compiler error, unless you've overloaded operator!, in which case, it might do anything.
It's a legacy idiom from C, where it meant "normalize to 0 or 1". I don't think there's a reason to use it in C++ other than habit.
It's converting BOOL (define for int) to c++ bool. BOOL is a define that in some cases for true can contain different integer values. So for example BOOL a = (BOOL)1; and BOOL b =(BOOL)2; both pass check for true. But if you'll try to compare you'll find that a not equals b. But after conversion !!a equals !!b.
(bool)notABoo - is not akin, because you'll convert type of variable byte still'll have different values. !! converts not only type but in some cases values too.