Not getting right results with abs(x) neg, <cstllib>

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rafael Olarte
    New Member
    • Jan 2007
    • 14

    Not getting right results with abs(x) neg, <cstllib>

    I really appreciate any feed back in order to resolve this challenge.

    My goal is to be able to obtain the error value as follows:

    Fahrenheit Centigrade Approximation % Err
    -45.38 -42.99 -37.69 12.3

    Unfortunately, I am unable to get that result of "12.3" using the abs(x) function.

    I really do not know how to deal with negative numbers...

    As follows, this is the code I have so far.

    Thanks so much for any help I can get in order to resolve this small piece of information.

    :)
    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    
    {
    
    const float temp1 = 38;
    const float temp2 = -45.38;
    const float temp3 = 89.3495;
    
    float tempFahrenheit1;
    float tempCelsius1;
    
    float tempFahrenheit2;
    float tempCelsius2;
    
    float tempFahrenheit3;
    float tempCelsius3;
    
    float friendCal1;
    float friendCal2;
    float friendCal3;
    
    
    float x;
    float y;
    float z;
    
    float resultError1;
    float resultError2;
    float resultError3;
    
     tempFahrenheit1 =temp1 - 32;
     tempCelsius1 = tempFahrenheit1 * 5/9;
    
     tempFahrenheit2 = temp2 - 32;
     tempCelsius2 = tempFahrenheit2 * 5/9;
    
     tempFahrenheit3 = temp3 - 32;
     tempCelsius3 = tempFahrenheit3 * 5/9;
    
     //Friends calculation
     friendCal1 = (temp1/2) - 15;
     friendCal2 = (temp2/2) - 15;
     friendCal3 = (temp3/2) - 15;
    
     //Percentage Error
     x = friendCal1 - tempCelsius1;
     resultError1 = fabs(x)/tempCelsius1 * 1.0;
    
     y = friendCal2 - tempCelsius2;
     resultError2 = fabs(y)/tempCelsius2 * 1.0;
    
     z = friendCal3 - tempCelsius3;
     resultError3 = fabs(z)/tempCelsius3 * 1.0;
      
     
    
     cout <<fixed << showpoint << setprecision(2);
    
     cout << setw(12) << "Fahrenheit" << setw(12) << "Centigrade" << setw(15)
                 << "Approximation" << setw(12) << "% err" << endl; 
     
     cout << setw(12) << temp1 
          << setw(12) << tempCelsius1 
          << fixed << setw(15) << setprecision(2) << friendCal1 
          << fixed << setw(15) << setprecision(2) << resultError1 << endl; 
    
     cout << setw(12) << temp2
          << setw(12) << tempCelsius2
          << fixed << setw(15) << setprecision(2) << friendCal2 
                 << fixed << setw(15) << setprecision(2) << resultError2 << endl; 
    
     cout << setw(12) << temp3
          << setw(12) << tempCelsius3
          << fixed << setw(15) << setprecision(2) << friendCal3
          << fixed << setw(15) << setprecision(2) << resultError3 << endl; 
    
     return 0;
    
    }
    Last edited by horace1; Feb 7 '07, 02:58 PM. Reason: added code tags
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    try putting abs around the whole expression, e.g.
    Code:
     //Percentage Error
     x = friendCal1 - tempCelsius1;
     resultError1 = fabs(x/tempCelsius1 * 1.0);
    
     y = friendCal2 - tempCelsius2;
     resultError2 = fabs(y/tempCelsius2 * 1.0);
    
     z = friendCal3 - tempCelsius3;
     resultError3 = fabs(z/tempCelsius3 * 1.0);

    Comment

    • Rafael Olarte
      New Member
      • Jan 2007
      • 14

      #3
      Thank you! It worked excellently well. I had to modified the 1.0 for 100. since it was 100%, and everything worked nice, and smooth.

      :)

      Comment

      Working...