PDA

View Full Version : C++: ^ Operator cannot deal with operand of 'double' type ???



matsis
11-16-2005, 04:15 AM
Am working on improving my automatic proportional toebrake gauge and would like to give it a more progressive reaction by using the ^ Operator (power) in the following manner:
trigger_key_event(KEY_AXIS_LEFT_BRAKE_SET,16384 - 32768*(1-(rudder_pos.var_value.n+0.03)/-0.45)^3 );

However I get the following error report:
C2296: '^' : Ungültig, da der linke Operand vom Typ 'double ' ist
C2296: '^' : Invalid, as the left Operand is of Type 'double '

The left operand here is this expression:
(1-(rudder_pos.var_value.n+0.03)/-0.45)
wherein
MODULE_VAR rudder_pos= {RUDDER_DEFLECTION};//Rudder Position radians
is a FLOAT64


If I read the definition of the ^ operator correctly it states that
"Syntax

Result = Number^Exponent
...
Number: An arbitrary numerical Value.
..."

The explanation of numerical value:
"Ein beliebiger Ausdruck, der als Zahl ausgewertet werden kann. Elemente eines Ausdrucks können beliebige Kombinationen von Schlüsselwörtern, Variablen, Konstanten und Operatoren enthalten, die als Ergebnis eine Zahl liefern."

"An arbitrary Value that can be analized as a number. Elements of a value can contain arbitrary combinations of keywords, variables, constants and operators, the result of which must be a number."

No talk at all of double types (numbers) not being allowed ???
Any insights from seasoned C++ programmers?

n4gix
11-16-2005, 04:16 PM
Hmmm... according to the C++ help file here, the ^ is a bitwise-exclusive-OR:

The bitwise operators perform bitwise-AND (&), bitwise-exclusive-OR (^), and bitwise-inclusive-OR (|) operations.

The bitwise-exclusive-OR operator compares each bit of its first operand to the corresponding bit of its second operand. If one bit is 0 and the other bit is 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.

The ^ operand is only valid as an exponent in Visual Basic, AFAIK... ;)

////////////////////////////////

In C++, the correct operator is:

pow ( val, exp );

Remarks
The pow function computes x raised to the power of y.

pow does not recognize integral floating-point values greater than 264, such as 1.0E100.

pow has an implementation that uses Streaming SIMD Extensions 2 (SSE2). See _set_SSE2_enable for information and restrictions on using the SSE2 implementation.

C++ allows overloading, so you can call any of the various overloads of pow. In a C program, pow always takes two double values and returns a double value.

/////////////////////////////////

Try this instead:

trigger_key_event(KEY_AXIS_LEFT_BRAKE_SET,16384 -
32768*(1-pow((rudder_pos.var_value.n+0.03)/-0.45),3 ));

At most you should only receive a warning about 'conversion from float to double, possible loss of data' or something like that... ;)

matsis
11-19-2005, 06:38 PM
Thanks Bill, I was indeed looking at the Visual Basic help section, not C++...
The pow appears to work, though I needed to include math.h to make it compile. The compiler just complained of a conflict in the argument and that he converted something.
This is how the code line looks now (slightly different from your proposal):
trigger_key_event(KEY_AXIS_LEFT_BRAKE_SET,16384-32768*pow((1-(rudder_pos.var_value.n+0.03)/-0.45),3) );

n4gix
11-20-2005, 01:42 PM
LOL! I've found I need to be very careful when using the Index of the MSVC++ IDE to make sure I'm looking at either C or C++ code examples... ;)

I once spun my wheels wondering why int( fval ); wouldn't work to convert a float to an unsigned integer...

I see I miscounted your parenthesis when proposing that solution... Oops! I'm glad you got it sorted out though!