TAN Function

I am having great difficulty in getting my program to trig functions such as tan.

I have tried changing float to double but keep getting errors of invalid operands of types int & double and have no idea what i'm doing wrong

by the way I know it looks messy currently but I am just transposing a basic program over and will tidy up later

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  #include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int RFQ;
    cout << "Enter design number \n";
    cin >>RFQ;

    int ZSYS;
    cout << "ZSYS \n";
    cin >>ZSYS;

    int D;
    cout << "Slip ring DIA in mm\n";
    cin >> D;

    int ZO;
    cout << "Slip ring impedance ZO= \n";
    cin >> ZO;

    int EF;
    cout << "Feeder dielectric constant = \n";
    cin >> EF;

    int ESR;
    cout << "Slip ring dielectric constant = \n";
    cin >> ESR;

    int ZF;
    cout << "Feeder impedance = \n";
    cin >> ZF;

    int LF;
    cout << "Feeder length in mm = \n";
    cin >> LF;

    int F;
    cout << "Frequency = \n";
    cin >> F;

    int YO, YSYS, U, P, X;
    YO = 1/ZO;
    YSYS = 1/(2*ZSYS);
    U = 2*M_PI*sqrt(ESR)*M_PI*D*F/(1000*4*300);
    float S=YO/(YO^2+(YSYS*tan(U))^2)
    P=YSYS*YO*(1+(tan(U))^2)
    X=tan(U)*(YO^2-YSYS^2)



    return 0;

}
Last edited on
If I assume all else here is reasonable, X is declared as an integer, as is Y0 and YSYS.

What would you expect if 1/Z0 when truncated to an integer? It is likely going to be zero if Z0 is greater than 1.0.

Likewise, if ZSYS is more than 0.5, YSYS will end up as zero.

Yet, beyond that, since X is an integer, the result of tan(u), being a double, is not going to produce a value meaningful to X as an integer.

P=YSYS*YO*(1+(tan(U))^2)

^ IS THE XOR OPERATOR. IS THIS SUPPOSED TO BE A POWER?

I also recommend double instead of float as a habit in your code, unless you have a reason not to do so.
Last edited on
@Niccolo

I haven't taken P & X out as integers as I can't get the program past the tan function in S but it would be something that I would have done if I found a solution to the problem I am having.

YSYS does need to be something other than 0 so should I change that to float?

@jonnin yes I am using ^ as a power function. What should I use instead?
for small powers, just multiply it times itself: x*x or x*x*x
math has pow(base, exp) eg pow(x,2) but its rigged for double powers which takes more math than a dumb multiply and is slower. Many of us have written integer power only pow functions to deal with that when we need speed.

c++ will NOT warn you if you used xor when you meant power. Its legal and it will compile and run, and you will get utter gibberish out.

11^2 is 9 for example

this is one place where you need to check any new language you use. some do it this way (xor) and some make it power (typically minor languages that are not suited for bitwise work anyway, but some major ones use it as power as well).
Last edited on
U is also declared as an integer.

tan does not take integers.

Also,

(1000*4*300)

Is written as an integer literal, so the division is by integer.

1000.0*4.0*300.0 might do better there.

Change everything to doubles unless you have a reason to use integers (and why did that ever make sense anyway?)
Last edited on
^^ that is true across the board. add a .0 to all constants in your code to make them doubles like 1.0/Z or whatever.

http://www.cplusplus.com/reference/cmath/ just in case you need a list of what you can do in basic math.

and, while we are on the subject.... trig is in radians in c++, you know this, right?

once you get it rolling, try a redo to eliminate redundant terms. tan(u)*tan(u) can be stored and reused rather than computed many times ... the compiler MAY figure that out and do it for you, but then again, it may not.
Last edited on
Look closely at lines 48-50. Missing semicolons.

Your aversion to using whitespace sure doesn't help readability IMO. For an example:

U = 2 * M_PI * sqrt(ESR) * M_PI * D * F / (1000 * 4 * 300);
Topic archived. No new replies allowed.