![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jun 2005
Location: far..far..away
Posts: 25
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jun 2005
Location: far..far..away
Posts: 25
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: here
Posts: 146
Rep Power: 4
![]() |
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] |
|
|
|
|
|
#5 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Your post was misleading; if this is what you thought the output should be, you would be wrong.
Quote:
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 |
|
|
|
|
|
|
#6 | |
|
Newbie
Join Date: Jun 2005
Location: far..far..away
Posts: 25
Rep Power: 0
![]() |
Quote:
|
|
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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); 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 |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Jun 2005
Location: far..far..away
Posts: 25
Rep Power: 0
![]() |
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;
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|