473,398 Members | 2,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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.

:)
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9.  
  10. {
  11.  
  12. const float temp1 = 38;
  13. const float temp2 = -45.38;
  14. const float temp3 = 89.3495;
  15.  
  16. float tempFahrenheit1;
  17. float tempCelsius1;
  18.  
  19. float tempFahrenheit2;
  20. float tempCelsius2;
  21.  
  22. float tempFahrenheit3;
  23. float tempCelsius3;
  24.  
  25. float friendCal1;
  26. float friendCal2;
  27. float friendCal3;
  28.  
  29.  
  30. float x;
  31. float y;
  32. float z;
  33.  
  34. float resultError1;
  35. float resultError2;
  36. float resultError3;
  37.  
  38.  tempFahrenheit1 =temp1 - 32;
  39.  tempCelsius1 = tempFahrenheit1 * 5/9;
  40.  
  41.  tempFahrenheit2 = temp2 - 32;
  42.  tempCelsius2 = tempFahrenheit2 * 5/9;
  43.  
  44.  tempFahrenheit3 = temp3 - 32;
  45.  tempCelsius3 = tempFahrenheit3 * 5/9;
  46.  
  47.  //Friends calculation
  48.  friendCal1 = (temp1/2) - 15;
  49.  friendCal2 = (temp2/2) - 15;
  50.  friendCal3 = (temp3/2) - 15;
  51.  
  52.  //Percentage Error
  53.  x = friendCal1 - tempCelsius1;
  54.  resultError1 = fabs(x)/tempCelsius1 * 1.0;
  55.  
  56.  y = friendCal2 - tempCelsius2;
  57.  resultError2 = fabs(y)/tempCelsius2 * 1.0;
  58.  
  59.  z = friendCal3 - tempCelsius3;
  60.  resultError3 = fabs(z)/tempCelsius3 * 1.0;
  61.  
  62.  
  63.  
  64.  cout <<fixed << showpoint << setprecision(2);
  65.  
  66.  cout << setw(12) << "Fahrenheit" << setw(12) << "Centigrade" << setw(15)
  67.              << "Approximation" << setw(12) << "% err" << endl; 
  68.  
  69.  cout << setw(12) << temp1 
  70.       << setw(12) << tempCelsius1 
  71.       << fixed << setw(15) << setprecision(2) << friendCal1 
  72.       << fixed << setw(15) << setprecision(2) << resultError1 << endl; 
  73.  
  74.  cout << setw(12) << temp2
  75.       << setw(12) << tempCelsius2
  76.       << fixed << setw(15) << setprecision(2) << friendCal2 
  77.              << fixed << setw(15) << setprecision(2) << resultError2 << endl; 
  78.  
  79.  cout << setw(12) << temp3
  80.       << setw(12) << tempCelsius3
  81.       << fixed << setw(15) << setprecision(2) << friendCal3
  82.       << fixed << setw(15) << setprecision(2) << resultError3 << endl; 
  83.  
  84.  return 0;
  85.  
  86. }
  87.  
Feb 7 '07 #1
2 1360
horace1
1,510 Expert 1GB
try putting abs around the whole expression, e.g.
Expand|Select|Wrap|Line Numbers
  1.  //Percentage Error
  2.  x = friendCal1 - tempCelsius1;
  3.  resultError1 = fabs(x/tempCelsius1 * 1.0);
  4.  
  5.  y = friendCal2 - tempCelsius2;
  6.  resultError2 = fabs(y/tempCelsius2 * 1.0);
  7.  
  8.  z = friendCal3 - tempCelsius3;
  9.  resultError3 = fabs(z/tempCelsius3 * 1.0);
  10.  
Feb 7 '07 #2
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.

:)
Feb 7 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mark | last post by:
Hi Guys, I have a page with a list of links that when clicked submit data to a clients search engine, The result opens in another window and either displays a list of items or diaplays "No Items...
14
by: Richard Cavell | last post by:
Hi, Using GCC on my G4, if I have a calculation like this: #include <stdint.h> uint64_t a = 0xffff * 0xffff ; the result will be clobbered to 32 bits because that's the length of an...
22
by: SF | last post by:
Hello All, In a windows C learning project I am wokring on I use the system function to run a command, I want to suck the results into a vairable. The system function seems to only return an...
0
by: | last post by:
Is there any sample about getting the results from executing an exe using asp? Thanks for answering.
1
by: jonnyothan | last post by:
The following code isn't producing expected results: class Selectable { bool IsSelected() const; }; class Unit : public Selectable { };
8
Alireza355
by: Alireza355 | last post by:
I have two tables: table1: number explanation 10 something here 11 something here 12 something here 13 something...
8
Alireza355
by: Alireza355 | last post by:
Dear all, Is there a way to have a little indentation in a textbox in a subform in datasheet view? I tried this: =" " & in the recordsource, but I keep getting an error.
1
by: ebasshead | last post by:
Hi Everybody, Behind a combo box on an open form, I have the below code to run a query, which it does, and pops up the query results.. MaxofID, NPPricePerWeek and IDProperty2, in data sheet view. ...
4
by: Brad Galinson | last post by:
I have a long sql query that, among other things, accesses multiple tables with an inner join and some left outer joins. I am expecting it to give a single set of results but when it finds more than...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.