How is the rules of calculating a value named in C++ - c++

Ok, this is a hard (for me) to explain what exactly I'm asking for, but I'll try it anyway...
I'm trying to explain to a person, who is learning C++, how an expression is calculated.
More specifically, why this:
5 / 2
gives 2 and why that:
5.0 / 2.0
gives an expected 2.5.
My explanation says it is because Integer value / Integer value = Integer value. And this is the clu of my question: how is that rule called? I always thought it is "Type Algebra", but putting that on Google shows this term is rather Haskell related.
So, is the rule describing how operations and type of expressions in C++ depends on the types of values/variables somehow called? And an extra question: is it related only to C++ (I mean: this term is used only in C++ related material)?

You are looking for topics like:
Promotion rules
Implicit conversions
Arithmetic conversions
Other stuff like operator precedence may also apply depending on the expression.

Related

Why isn't an order type used for comparisons in OCaml?

I have yet to find a suitable answer for an OCaml design decision, and hope one of those here with in-depth knowledge of OCaml's implementation can shed some light.
In SML, if we evaluate Int.compare(6, 9) we get LESS which is a constructor for the order type (the others being GREATER and EQUAL). In OCaml, if we evaluate compare 6 9 we get -1.
Was there a specific reason to favor having comparison functions return zero, a negative number, or a positive number in OCaml's standard library, rather than an algebraic data type as in SML?

syntax command syntax issue

I've spent hours reading Stata help file (17SE) but I'm unable to understand why the syntax in this syntax command is wrong:
syntax, n(integer) interact(real) infage(integer min=45 max=75) supage(integer min=45 max=75)
For sure the part until interact(real) works, but what's wrong with the following part?
I suspect that you want something more like
syntax, n(integer) interact(real) infage(numlist integer >=45 <=75) ///
supage(numlist integer >=45 <=75)
There are two points here.
The min and max arguments are about how many elements are specified, not about what their values might be. I doubt that you want to insist that the user types in at least 45 integers to each option.
Expecting that a specification integer() would support specification of the allowed range seems reasonable enough, but nothing in the documentation supports that. It's numlist() that allows more checking.

What is the decimal point ('.') in C++ and can I make one?

I am in a C++ class right now so this question will concern itself primarily with that language, though I haven't been able to find any information for any other language either and I have a feeling whatever the answer is it's probably largely cross language.
In C++ unmarked numbers are assumed to be of integral type ('4', for example, is an integer)
various bounding marks allow for the number to be interpreted differently (''4'', for example, is a character, '"4"' a string).
As far as I know, there is only one kind of unary mark: the decimal point. ('4.' is a double).
I would like to create a new unary mark that designates a constant number in the code to be interpreted as a member of a created datatype. More fundamentally, I would like to know what the '.' and ',' and '"', and ''' are (they aren't operators, keywords, or statements, so what are they?) and how the compiler deals with/interprets them.
More information, if you feel it is necessary:
I am trying to make a complex number header that I can include in any project to do complex math. I am aware of the library but it is, IMHO, ugly and if used extensively slows down coding time. Also I'm mostly trying to code this to improve my programming skills. My goal is to be able to declare a complex variable by doing something of the form cmplx num1= 3 + 4i; where '3' and '4' are arbitrary and 'i' is a mark similar to the decimal point which indicates '4' as imaginary.
I would like to create a new unary mark that designates a constant number in the code to be interpreted as a member of a created datatype.
You can use user defined literals that were introduced in C++11. As an example, assuming you have a class type Type and you want to use the num_y syntax, where num is a NumericType, you can do:
Type operator"" _y(NumericType i) {
return Type(i);
}
Live demo
Things like 4, "4" and 4. are all single tokens,
indivisible. There's no way you can add new tokens to the
language. In C++11, it is possible to define user defined
literals, but they still consist of several tokens; for complex,
a much more natural solution would be to support a constant i,
to allow writing things like 4 + 3*i. (But you'd still need
the C++11 support for constexpr for it to be a compile time
constant.)

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.

Why "**" does not bind more tightly than negation in OCaml?

after this question, I don't know what to think.
In OCaml, if you do something like -1.0**2.0 (because of the typing you need to have float), you obtain 1.00. According to the standard order of operations, the result should be -1 (as in python).
I wasn't able to find the reason or a clear definition of the operator precedence in OCaml...
Is this because of the type system ? or the fact that there's a binding underneath with pow ?
As the very page you quote says, "The order in which the unary operator − (usually read "minus") acts is often problematical." -- it quotes Excel and bc as having the same priority for it as O'CAML, but also says "In written or printed mathematics" it works as in Python. So, essentially, there's no universal consensus on this specific issue.
Operator precedence is syntax-directed in OCaml, which means that the first character of the function identifier (and whether it's unary or binary) determines the operator precedence according to a fixed sequence. Contrast this with languages like Haskell, where the operator precedence can be specified at function definition regardless of which characters are used to form the function identifier.