1. ## Math function deg/rad query

I've gone and got myself confused with this little program It's meant to find the height of a triangle in degrees, when the angle of elevation (y) and the distance accross (x) is known, and output it in some sort of table (formatting, however, is not important at this stage.) It does this, but of course only in Radians.

I put the chunk of code following "float height....." in to convert to degrees, but it seems to do nothing. Even when I change the values, it has no effect on the program.

Here's the code I have, and note, this is a UK keyboard setup, so I couldn't work out the "hash" symbol. For the sake of this, I'll just write hash. How can I make this thing output Degrees instead of Radians?

Code:
```hash include <cstdlib>
hash include <iostream>
hash include <iomanip>
hash include <math.h>

using namespace std;

float height (float x, float y)
{return x*(180/3.141593);
return y*(180/3.141593);
}

int main()
{ for (float x=0; x <=60; x+=10)
{ for (float y=0; y <=85; y+=5)
cout << setw(12) << (tan(y)*x);
cout << endl;
}
}```
Thanks,
George

2. First, you can't return multiple values in c/c++. For this, you need to use pointers in C.
Second, You need to actually call the function that does the conversion.

3. Originally Posted by GeorgeV
Here's the code I have, and note, this is a UK keyboard setup, so I couldn't work out the "hash" symbol.
The hash key is next to the return key in the middle row on a standard UK keyboard

Code:
```float height (float x, float y)
{return x*(180/3.141593);
return y*(180/3.141593);
}```
As nempo said, you can't return multiple values from a function, although since you're using C++, don't use pointers, use references, which will allow you to directly modify the values in-situ. eg,

Code:
```void height (float & x, float & y)
{
x = x *(180/3.141593);
y = y *(180/3.141593);
}```

you could also investigate the struct keyword which will allow you to create your own type to combine X and Y together.
Code:
```struct coordinate
{
double x, y;
};

/* ... */

coordinate to_degrees( double x, double y )
{
coordinate coo;
coo.x = x *(180/3.141593);
coo.y = y *(180/3.141593);
return coo;
}```
(By the way, I reccommend using double's instead of float's - the double type has a much greater precision. the typical precision of float is around 6 significant figures on most modern desktops)

4. 1) As Bench82 suggested, use doubles.
2) Instead of a function that converts two arguments simultaneously, consider a function that converts a single argument, and calling it twice. Then you don't need pointers or references at all, you can use the regular return value. Also, by using the inline keyword, assuming the compiler takes the hint, the function call gets reduced to a single multiplication and there's no more overhead than calling a two-argument function once.
3) Consider expressing pi symbolically.
Code:
```#include <cmath>

const double Pi = 4.*std::atan(1.);```
(among other possible expressions for pi).

5. Code:
```#define PI 3.14159f
#define DEGTORAD(x) (float)( (x)*(PI)/(180.0f) )```
You won't need doubles for this particular operation. Floats offer enough precision here to get good results.

To use this:
Code:
`float fCosX=cosf(DEGTORAD(angle_in_degrees));`

6. Also, indent your program consistently. This is a lot tougher to read
Code:
```int main()
{ for (float x=0; x <=60; x+=10)
{ for (float y=0; y <=85; y+=5)
cout << setw(12) << (tan(y)*x);
cout << endl;
}
}```
than this.
Code:
```int main()
{
for (float x=0; x <=60; x+=10)
{
for (float y=0; y <=85; y+=5)
cout << setw(12) << (tan(y)*x);
cout << endl;
}
}```
Code:
`hash include <math.h>`
You do know that it's "#include", not "hash include", right? The '#' symbol is pronounced hash, among other ways. (I've heard it called pound, sharp, number sign, tic-tac-toe board, etc, though hash seems to be the most common.)

Also, when you use C header files such as <math.h> in C++, you should use the C++-ized header files like <cmath>. (Drop the .h suffix, add a c prefix.) This puts the functions in the namespace std. Though since you just use the entire namespace, it doesn't really matter.

7. floats are fine,

use cmath not math.h,

pi is already there as M_PI,

"hash include" ? it's #include,

#define isn't recommended, use const type var = value;

8. floats are fine
Already mentioned. I disagree, but that's only because I've had enough troubles with floats in the past that I always use doubles, just out of habit . . .
use cmath not math.h
I said that, and while it's a common thing that people say around here, math.h isn't actually deprecated. (Just recommended.) Using math.h is fine, strictly; the contents will just be in :: instead of std::. But with a using namespace std statement, they're pretty much equivalent.
pi is already there as M_PI
Most implementations have M_PI, it's true, but not all. It isn't standard. It's better to calculate PI at runtime, such as with 4.*atan(1), or use your own constant (see google), if you want portability.

9. Thanks everybody for all the tips!

Re the "hash" key: I can't find it on this keyboard. It's pictured on the '3' key, but this only gives the '£' sign when combined with 'Shift'.

Next to the return key you say, Bench82? Unfortunately I'm just not seeing it. (Of course, I won't be programming on this laptop.)

As someone pointed out, floats offer enough precision for this task, but I've switched to doubles anyway.

10. and people say no one uses trigraphs anymore.
Code:
```

5.2.1.1 Trigraph sequences
1 All occurrences in a source file of the following sequences of three characters (called
trigraph sequences12)) are replaced with the corresponding single character.
??= #
??( [
??/ \
??) ]
??' ^
??< {
??! |
??> }
??- ~
No other trigraph sequences exist. Each ? that does not begin one of the trigraphs listed
above is not changed.
2 EXAMPLE The following source line
printf("Eh???/n");
becomes (after replacement of the trigraph sequence ??/)
printf("Eh?\n");

```
code block to appease cboard server.

11. > As someone pointed out, floats offer enough precision for this task, but I've switched to doubles anyway.

Unless your program does a lot of floating-point computation, or you need a large array of floating-point types and can speed up memory access with a smaller type, you may as well use doubles, since they're currently the largest standard floating-point type.

> and people say no one uses trigraphs anymore.

I starting programming in C in 1990 and have never used trigraphs, since every keyboard I've used even back then had all the basic characters. I only know about them from K&R2.

Edit: I'm wrong about doubles being the largest standard floating-point type - actually according to Stroustrup, long double is also standard. From K&R2 it appears that they are standard even in C90. But an 8-byte IEEE 754 double has 52 bits in the mantissa which gives about 15 or 16 decimal digits of precision (more than a pocket calculator), as opposed to about 7 in a 4-byte IEEE 754 float which has 23 mantissa bits, so long double is overkill for most purposes.

12. Originally Posted by robatino
I starting programming in C in 1990 and have never used trigraphs, since every keyboard I've used even back then had all the basic characters. I only know about them from K&R2.
I was speaking in absolutist terms.

13. #define isn't recommended, use const type var = value;
For something as simple as this it's just fine.

14. Originally Posted by GeorgeV
Next to the return key you say, Bench82? Unfortunately I'm just not seeing it. (Of course, I won't be programming on this laptop.)
In that case your laptop has an American keyboard, but windows is treating it like a UK one.

15. Again, thanks for the help. I'm now using <cmath> instead, and at the moment I'm also using references. So this bit reads something like so:

Code:
```void height (float & x, float & y)
{
x = x *(180/M_PI);
y = y *(180/M_PI);
}```
But I'm having trouble calling the 'height' function properly. What is the correct syntax I should be using?

Popular pages Recent additions