![]() |
|
#1
|
|||
|
|||
changing the value of x in c++iv written a program 2 calculate the value of a function f(x), and then written one so that the computer calculates it a different way for negative x.
how do i get c++ 2 do the following: if the user inputs a negative number, c++ ignores the negative sign and reads the value of x and puts this into the program iv used to calculate f(x). hope you understant what i mean. this is my program so far. i just need 2 change the second part but i dont kno how: CPP / C++ / C Code:
Last edited by LuciWiz : 02-Feb-2005 at 13:27.
Reason: Please insert your C code between [c] & [/c] tags
|
|
#2
|
||||
|
||||
|
C++ doesn't require '.h' in the #include statements, so you can lose those extensions if you want. You'd only use it if you wanted to force the use of the original C headers.
One way to do it is use the absolute value function to get the absolute value of 'x', then continue on w/ the 'else' statement. Alternatively, you could use parenthesis to force the program to multiply the negative values, thereby creating a positive. Example: Assuming 'x' is a negative number, ((x)*(x)) should give you a positive x^2 value. I don't remember offhand, but I believe C++ also has a 'power' function, allowing you to not have to manually multiply x*x. __________________
Common Sense v2.0-Striving to make the world a little bit smarter. |
|
#3
|
||||
|
||||
|
Quote:
Yes, it's called pow and it is located in the math header. Regards, Lucian __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#4
|
|||
|
|||
|
Quote:
For variables of type double, the function is fabs. Try the following with negative values for x and for positive values of x: CPP / C++ / C Code:
Regards, Dave |
|
#5
|
|||
|
|||
|
Quote:
Parentheses do not change the results unless they are used to override the built-in operator precedence: CPP / C++ / C Code:
And yes, you could use pow(x, 2) in place of x*x, but why would you want to do that? Regards, Dave |
|
#6
|
||||
|
||||
|
Ha ha, you got me.
![]() I suggested the power function to make the program more portable. Just thought it might be easier. <shrug> __________________
Common Sense v2.0-Striving to make the world a little bit smarter. |
|
#7
|
||||
|
||||
|
for integer data type,use abs
CPP / C++ / C Code:
for floating number,use fabs CPP / C++ / C Code:
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds |
|
#8
|
||||
|
||||
|
Quote:
__________________
Age is unimportant -- except in cheese |
|
#9
|
|||
|
|||
|
Quote:
I agree with your sentiment (I think), but my head is spinning: I'll buy this: "Anything is more portable than not using..." I'll buy this: "Nothing is more portable than using..." But: "Nothing is more portable than not using..." Oh, well... By the way, the function double pow(double, double) is in the C standard library (and, therefore the C++ standard library), so its use is portable between different platforms and/or different compilers for C and/or C++ (the only portability I am concerned with on this forum). I have always been a little leery of using this with negative values for the first argument, since there is a domain error if the first argument is negative and the second argument does not have an integer value. Since both arguments are floating point numbers (doubles), I always have roundoff error in the back of my mind. (Try it with something like pow(-2.0. 2.000001), for example). Since there is no roundoff error in the second argument of the expression pow(x, 2), it's technically OK in this application. My question of, "why would anyone want to do this instead of just using x*x," was based on my defensive approach to floating point numbers where roundoff error could be disastrous, and also to laziness (fewer keystrokes in "x*x"). Why do something (using pow() for squaring a number) that will probably be OK most of the time and requires more typing? Regards, Dave |
|
#10
|
||||
|
||||
|
Well, I was thinking that if the program needed to be modified for use w/ powers beyond 2 or 3, then maybe using the power function would be easier to use. That way you don't have to start counting and keeping track of "x's" and running into logic errors.
I suppose for a quick and dirty program that probably won't be used for anything besides practice it doesn't matter. But isn't it wise to think ahead for problems like that? Don't want another 64k barrier or Y2K fiasco. ;-) __________________
Common Sense v2.0-Striving to make the world a little bit smarter. |
Recent GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Changing monitors on a Linux machine | JdS | Computer Software Forum - Linux | 10 | 23-Dec-2004 09:49 |
| Problems with changing display resolution | vsseym | Computer Software Forum - Windows | 2 | 27-Jul-2004 10:01 |
| Pointer values changing unexpectedly | spudtheimpaler | C Programming Language | 11 | 04-Mar-2004 16:37 |
| Changing a line in a file | Allowee | Computer Software Forum - Linux | 1 | 10-Jan-2004 03:09 |
| Changing font while using getstr | BobbyDouglas | MySQL / PHP Forum | 1 | 03-Dec-2003 22:08 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The