Value return by a method does not change???

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nilushika
    New Member
    • Sep 2013
    • 20

    #1

    Value return by a method does not change???

    Hi this is my simple c++ programme, it has Thermister class which calculate resistance when varying Temp,but each time when calling to setTemp() eventhough it change value of Temp,value given by getResistance() mthd does not change.It always gives 1 when calling through a for loop.here is the code any help please...
    Code:
    #include <iostream>
    #include <cmath>
    class AbstractResistor
    {
    public:
    	virtual double getResistance() = 0;
    };
    
    class  Thermistor:public AbstractResistor
    {
    private:
        double a,b,c,Temp;
    public:
    Thermistor(double a,double b,double c,double Temp)
    {
       this->a=a;
       this->b=b;
       this->c=c;
       this->Temp=Temp;
    }
    ~Thermistor(){}
    
    double getResistance()
    {
    double y=(a-(1/Temp))/c;
    double x=sqrt(pow(b/(3*c),3)+pow((y/2),2));
    return exp(pow(x-y/2,1/3)-pow(x+y/2,1/3));
    }
    void setTemp(double Temp)
    {
        this->Temp=Temp;
    }
    };
    
    
    int main()
    {
    Thermistor* t=new Thermistor(1.4*pow(10,-3),2.37*pow(10,-4),9.9*pow(10,-8),328.15);
    std::cout <<"Resistance of Thermistor= "<<t->getResistance();
    
    for(int i=0;i<=100;i++)
    {
        t->setTemp(i+273.15);
        std::cout << t->getResistance()<<std::endl ;
    }
    return 0;
    }
  • computerfox
    Contributor
    • Mar 2010
    • 276

    #2
    No, based on the equation you give here:
    Code:
    pow(x-y/2,1/3)-pow(x+y/2,1/3)
    It will always cancel out and give 1.

    Are you sure that, that's the correct formula?

    Also, I'm not sure why you're not doing this, but it's standard practice in C++ to make this:
    Code:
    #include <iostream>
    #include <cmath>
    into this:
    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    It will remove the need to use std all over the place in your code.

    Comment

    • computerfox
      Contributor
      • Mar 2010
      • 276

      #3
      Took another look.
      Make this:
      Code:
      exp(pow(x-y/2,1/3)-pow(x+y/2,1/3))
      into this:
      Code:
      exp(pow(x-y/2.0,1.0/3.0)-pow(x+y/2.0,1.0/3.0))
      Otherwise, the result of the pow method will result in an integer instead of a double due to the divisions returning an integer value.

      Hope that helps!
      Last edited by computerfox; May 23 '15, 04:50 AM. Reason: More clarification

      Comment

      • computerfox
        Contributor
        • Mar 2010
        • 276

        #4
        Just for reference, here is what the C version would look like:

        Code:
        #include <stdio.h>
        #include <math.h>
        double get_resistance(double a,double b,double c,double Temp){
         double y=(a-(1/Temp))/c;
         double x=sqrt(pow(b/(3*c),3)+pow((y/2),2));
         return exp(pow(x-y/2.0,1.0/3.0)-pow(x+y/2.0,1.0/3.0));
        } 
        
        int main(){
         double temp=328.15;
         printf("%.3f",get_resistance(1.4*pow(10,-3),2.37*pow(10,-4),9.9*pow(10,-8),temp));
         int i=0;
         while(i<=100){
          temp=i+273.15;
          printf("%.3f\n",get_resistance(1.4*pow(10,-3),2.37*pow(10,-4),9.9*pow(10,-8),temp));
          i++;
         }
         //system("PAUSE");
         return 0;
        }

        Comment

        • kiseitai2
          New Member
          • Jul 2007
          • 93

          #5
          I do not recommend writing out using namespace std; unless you are absolutely certain that no conflicts will arise. I prefer to omit that line and instead do std::symbol_nam e, which is what the person asking did. The reason is that importing all of the contents in an stl header can potentially conflict or overshadow something you have written. Maybe I am paranoid about things that may have a low probability of happening, but I would rather see people develop good habits. The scope identifier is there so you can use what you need and ignore the rest. Other than that, use decimals (double) with pow. I suspect that pow is raising a number to 0 because 1/3 is an integer division. Integer divisions in which the numerator is smaller than the denominator yields 0. A number raised to 0 yields 1.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Amen to that

            One time I had to merge I Microsoft code with mine and we both used "group". Took six months to go through all that code and install a namespace.

            Using std:: everywhere guarantees that library code does not conflict with yours. Of course, you should install your own namespace so you don't conflict with someone else.

            Comment

            Working...