Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 21st, 2005, 10:48 PM   #1
clearbit
Newbie
 
clearbit's Avatar
 
Join Date: Jun 2005
Location: far..far..away
Posts: 25
Rep Power: 0 clearbit is on a distinguished road
Predfined Functions (abs,labs,fabs)

Well, I was trying to come up with a way for whatever a user inputs a value for these functions to always use the correct data type. Such as a long or double.

round 1
Output of program:

Please enter in a number:2
abs :  2
labs :  2
fabs :  2


round 2

Please enter in a number: -2.3
abs : 2
labs: 2.3
fabs:2.3

round 3

Please enter in a number: 900000000
abs:
labs:900000000
fabs:

Iam pretty sure thats what the output should be, but i could be wrong. I'm working on a function for absolute values and Im trying to make an all in one, no need for individual Functions. Please reply if you have any ideas.
clearbit is offline   Reply With Quote
Old Jun 22nd, 2005, 6:56 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You seem to have a misperception of what a "long" is. It's an integer. You (may) also have a misperception of what "rounding" is. That said, investigate "overloading functions".
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 22nd, 2005, 7:58 PM   #3
clearbit
Newbie
 
clearbit's Avatar
 
Join Date: Jun 2005
Location: far..far..away
Posts: 25
Rep Power: 0 clearbit is on a distinguished road
no no, i understand a long is a longer version of an integer because after a certain number it will overflow and wrap itself around, but thats not what im asking, and I understand about "rounding" those predefined functions are of no benifit to me.

I was just asking (to simplify) if I use a long double data type will i be able to have regular output??

so if i enter 2.5 to find an absolute it wont show up as a long int, but a double and just a double, not 2.5000000.
clearbit is offline   Reply With Quote
Old Jun 22nd, 2005, 8:36 PM   #4
L7Sqr
Hobbyist Programmer
 
Join Date: Jun 2005
Location: here
Posts: 146
Rep Power: 4 L7Sqr is on a distinguished road
You could try using llabs for anything not involving a decimal point, fabsl for anything else (involving decimal point). Of course, this means scanning your input before deciding what to do with it.
The machine has no concept of what your data is, it simply interprets a sequence of ones and zeros and acts accordingly. You are responsible for dealing with that data in a reasonable fashion.
If you want the machine to print 2.5 instead of 2.5000000 (both of which are 'just doubles'), you need to specify. If you want to call a function to act upon an integer value, you must decide that and do so. And so on.
__________________
"...and though our kids are blessed their parents let them shoulder all the blame."
- The Quiet Things That No One Ever Knows [BrandNew]
L7Sqr is online now   Reply With Quote
Old Jun 23rd, 2005, 8:20 AM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Your post was misleading; if this is what you thought the output should be, you would be wrong.
Quote:
Please enter in a number: -2.3
abs : 2
labs: 2.3
fabs:2.3
labs (at least my labs) would not return a fractional decimal value.

When your user provides input, he/she provides keystrokes. "2.3" has to be converted from keystroke codes to some number. If it's converted to some floating point value such as a double or long double it will become some value which may not be precisely 2.3. Floating point gains range at the expense of precision. Many "rounding" operations (not all) are merely output formatting operations. How an integer or float is handled is in the hands of your implementation and compiler. 'Native" sizes vary.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 23rd, 2005, 10:33 PM   #6
clearbit
Newbie
 
clearbit's Avatar
 
Join Date: Jun 2005
Location: far..far..away
Posts: 25
Rep Power: 0 clearbit is on a distinguished road
Quote:
Originally Posted by DaWei
Your post was misleading; if this is what you thought the output should be, you would be wrong.

labs (at least my labs) would not return a fractional decimal value.

When your user provides input, he/she provides keystrokes. "2.3" has to be converted from keystroke codes to some number. If it's converted to some floating point value such as a double or long double it will become some value which may not be precisely 2.3. Floating point gains range at the expense of precision. Many "rounding" operations (not all) are merely output formatting operations. How an integer or float is handled is in the hands of your implementation and compiler. 'Native" sizes vary.
yeah sorry, the ouput was just something i made up ont the top of my head, i haven't coded the actual program yet, but i see what you are saying thnx
clearbit is offline   Reply With Quote
Old Jun 24th, 2005, 10:06 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
These are definitions for my library functions (MS C++).
Note that for C++, the fabs and abs functions are already overloaded; that is, they may be called with either a float, a double, or a long double (fabs), or with an int, a long, a double, or a long double (abs). In C, one would have to use fabs for a double or fabsf for a float and abs for an int or _abs64 for an __int64 (MS definition).
int abs(int n);
long abs(long n);                    // C++ only
double abs(double n);              // C++ only
long double abs(long double n); // C++ only
float abs(float n);                   // C++ only
__int64 _abs64(__int64 n);
long labs (long n);
float fabs(float x);                  // C++ only
double fabs(double x);
long double fabs(long double x); // C++ only
float fabsf(float x);
One could write a single function which incorporated the appropriate member of this list, depending upon the arguments in the declaration/definition. Note that the return value is not a valid item in distinguishing among overloaded functions.

The same identifier may not be used for different entities simply because it introduces an ambiguity -- the compiler has no way to know which entity to use. Some seemingly same-named entities appear to be used because the ambiguity is resolved with scope (in this area of the program use this one; in that area, use that one). Overloading uses a different mechanism. The compiler deduces the appropriate entity by part of the context (the argument list). The entity is then renamed (behind the scenes) by the compiler in order to carry the uniqueness of the function through the layer of abstraction to the machine.

If I'm beating a horse you've already ridden, I apologize. I can't tell from your post precisely what your level of familiarity is.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 3rd, 2005, 12:47 PM   #8
clearbit
Newbie
 
clearbit's Avatar
 
Join Date: Jun 2005
Location: far..far..away
Posts: 25
Rep Power: 0 clearbit is on a distinguished road
Heres my program, if you have any suggestions let me know

//sqr,pow,abs
#include <iostream.h>
#include <stdlib.h>
#include <math.h>
using namespace std;

int GetCal();
int GoSquaring();
int GoPowering();
int GetAbs();

int main(int argc,char **argv)
{
  GetCal();
  system("PAUSE");
      return 0;
}

int GetCal()
{
 int choice;

   cout << "Welcome To The Multi-Calculation Program\n\n"<<
        "1.Squaring\n" <<
        "2.Find a Power\n" <<
        "3.Find an Absolute Value\n\n";

   cout << "What Calculation do you wish to perform: ";
   cin >> choice;


 switch (choice)
 {
   case 1:
        GoSquaring();
       break;
   case 2:
        GoPowering();
       break;
   case 3:
        GetAbs();
        break;

   default:
      cout << "\nThat is not an option\n";
  }
 return 0;
    }



int GoSquaring()                  //Squaring function
{

 long double x;           //variables in my squaring,power, and absolute program
 long double sqred;
 int numbs_2sqr;

 cout << "How many numbers to square? ";       //asking for the number
 cin >> numbs_2sqr;                            // of squares to calculate
 while(numbs_2sqr > 0)     //loop to produce amount of squares needed
 {
 cout<<"Enter a number to square: ";
 cin >> x;
 sqred = sqrt(x);
 cout <<"The answer is: " << sqred <<"\n";
 numbs_2sqr--;      //decrement of loop variable
 }

return 0;
 }



int GoPowering()                 //Power function
 {
   double pwit;
   double expon;
   double powed;
   int numbs_2pow;

   cout << "How many numbers should you power? ";
   cin >> numbs_2pow;
   while(numbs_2pow > 0)  //loop to produce amount of pows needed
   {
   cout << "Enter number to power: ";
   cin >> pwit;
   cout << "Enter power: ";
   cin  >> expon;
   powed = pow(pwit,expon);
   cout << "The answer is: " << powed << "\n" ;
   numbs_2pow--;
   }
return 0;
}




int GetAbs()               //Absolute Value function
{
 int x;
 int absed;
 int numbs_2abs;

 cout << "How many Absolute Values to Calculate? ";       //asking for the number
 cin >> numbs_2abs;                            // of abs to calculate
 while(numbs_2abs > 0)     //loop to produce amount of abs needed
 {
 cout<<"Enter Value to Absolute: ";
 cin >> x;
 absed = abs(x);
 cout <<"The answer is: " << absed <<"\n";
 numbs_2abs--;      //decrement of loop variable
 }
 return 0;
 }
clearbit is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:52 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC