Thread: Loss of data with trig function

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    24

    Loss of data with trig function

    So I can't figure out how to change my result into an exact decimal.

    Code:
    #include <cmath>
    #include <iostream>
    #include <cstdlib>
    #include <iomanip>
    
    #define Pi double(3.14159)
    
    using namespace std;
    
    void Switch(double x, double y, double z, char Type)
    {
    	int h = 0;
    	switch (Type)
    	{
    		case 'e':
    			cout << "\n\tThe Area of the Equilateral is = " << 1.0/2.0 * x * sqrt(x*x -(x/2)*(x/2));
    			break;
    		case 's':
    			cout << "\n\tThe Area of the Square is = " << x * x;
    			break; 
    		case 'p':
    			h =tan(54 * Pi/180);
    			cout << "\n\tThe Height is = " << h;
    			break; 
    		
    		default:
    			cout << "\n\tinvalid entry";
    	}
    	
    }
    My problem is that the tan function returns 1 instead of like 1.37etc...

    I tried making them all into double by puting .0 at end of each number, i.e. 180.0. That didn't work

    I also tried removing the #define Pi and putting instead in my code, double Pi = 3.14159. That didn't work either.

    Any simple suggestions? I am only in a beginning c++ class mind you so the simpler the suggestion the better.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You have h defined as an int, so the tan() return value is getting implicitly cast as an int. Change h to a double.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    24
    Wow...i fail... Yeah sorry for wasting your time on something so simple. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Packet Container Class
    By ChaoticXSinZ in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2010, 12:07 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM