-1

I am going through some C++ code to determine how it operates ( I don't know the language very well yet myself) and I ran into some lines that look like this:

  bw = 2.*ti/3.;           rorq = ((bw/to)**(1./q));

Is there any particular reason that the code would be using .*ti instead of just *ti? Does it have something to do with the variables?

Skye
  • 3
  • 6
  • 1
    I would suggest not going through c++ *code* to learn the language. Especially code with variable names like `bw`, and `ti`. Try a good [c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – cigien Apr 16 '20 at 17:07
  • While there's at least one language that I can't remember off the top of my head that uses `.*` for elementwise multiplication, C++ doesn't have anything like that. – chris Apr 16 '20 at 17:09
  • 2
    There is no operator `**` in C++. One `*` would be for multiplication, and the other would be for dereferencing. However, the expression `(1./q)` is not a pointer, so this expression is not valid. – Thomas Matthews Apr 16 '20 at 17:12
  • I have to go through the code, I do not have the time to learn c++ properly unfortunately. It hasn't been too bad, I have been using cplusplus.com for most of my questions and that has been working fine, but I don't know the reason for the ./ vs. / I also have the variable meanings figured out so that is not an issue – Skye Apr 16 '20 at 17:14
  • Let's add some spaces to clarify the expression: `bw = 2.0 * ti/3.0;` The "." is parsed with the number 2 to indicate floating point. This all has to do with *operator precedence* when parsing an expression. Likewise, the other expression should be `(1.0 / q)`. Also look up the difference between integer division and floating point division. – Thomas Matthews Apr 16 '20 at 17:14
  • Okay that makes sense, thanks @Thomas Matthews – Skye Apr 16 '20 at 17:16
  • It doesn't look like it converted completely. This can be seen from the usage of `**`. The C++ language doesn't have exponentiation operators, it has the `pow()` function instead. – Thomas Matthews Apr 16 '20 at 17:16

2 Answers2

3

First, this is not valid C++ code:

rorq = ((bw/to)**(1./q));  

The C++ language does not have a ** operator.

In parsing the expressions, there are rules of precedence. The '.' attaches itself to the number. Adding proper spaces:

bw = 2. * ti/3. ;
rorq = ((bw / to) ** (1. / q));  

To prevent ambiguity, most C++ programmers would add a 0 after the decimal point:

bw = 2.0 * ti / 3.0 ;
rorq = ((bw / to) ** (1.0 / q));  

The ".0" is often appended to force an integer constant to a floating point constant. The suffix 'f' could also be used:

bw = 2f * ti / 3f ;
rorq = ((bw / to) ** (1f / q));  
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • `2f` is legal? How about `0x2f`? – chux - Reinstate Monica Apr 16 '20 at 17:44
  • The hexadecimal notation is required to have the `0x` in front. The `0x` changes the parsing syntax. So, without the `0x`, the number is floating point. With the `0x`, the number is hexadecimal. – Thomas Matthews Apr 16 '20 at 17:46
  • Hmm with `float f = 2f ;`, I get `error: unable to find numeric literal operator 'operator""f'. Does your code compile without warning/error? (Me: g++ -std=c++0x -O0 -g3 -Wall -c -fmessage-length=0 -fext-numeric-literals -MMD -MP -MF"cpp2.d" -MT"cpp2.o" -o "cpp2.o" "../cpp2.cpp", gc, version 9.3.0) – chux - Reinstate Monica Apr 16 '20 at 18:44
0

This doesn't look like C++ at all.

As far as my cache of programming languages goes, the dots in bw = 2.*ti/3.; would represent element-wise operations, as used for multiplication, division and exponentiation in matlab. The double asterisk ** in rorq = ((bw/to)**(1./q)); pertains to exponentiation in some languages.

Pertaining to question title, multiplication and division is only performed by the use of * and / respectively in C++. You can use bitwise shift operators << and >> for some cases as well.

  • BTW, the operator `**` is a common exponentiation operator used in many languages, not just Matlab. – Thomas Matthews Apr 16 '20 at 17:26
  • @ThomasMatthews I did say 'pertains to exponentiation in some languages.' –  Apr 16 '20 at 17:27
  • Shift operators cannot be used for all cases of multiplication and division. For example, you can't multiply by 3 using a shift operation. Shift operations don't apply to floating point. Shift operations can be associated with raising a value to a power of 2 (positive or negative). – Thomas Matthews Apr 16 '20 at 17:28
  • @ThomasMatthews Yes true. I'll edit that (works for some cases). One additional point to be noted is that although bitwise operations may seem fast here, they work slow on interpreted languages like R. (tested it via benchmarks) –  Apr 16 '20 at 17:31