Alchemista or anyone who knows C++ , i have a question (related to math too)
Anyway I'm just making an easy converter, but i was wondering about this right.. when converting from Far to Celsius, why is it that when i do this: cout << value << " Farenheit is " << value*5/9-32*5/9 << " Celsius." << endl; the program works, but..
when I did: cout << value << " Farenheit is " << (value-32)*(5/9) << " Celsius." << endl; it didn't ?
if this is any help when i type in 90 farenheit it is showing me 0 celsius but when i use the first method i do get 33 celsius
You need to be aware of the various data types in C++
When you perform division on two integers it will perform integer division. The remainder will simply be chopped off instead of being included in the quotient.
(value - 32.0) * (5.0/9.0)
ohh okay i see.. so i have to use .0 after decimals?
i mean after integers..
when .0 is added to a literal the compiler will interpret it as a floating point type
oh okay thanks =)!
this is what my stuff looks like so far..
#include <iostream> using namespace std; int main () { char choice; float value; cout << "Select choice:" << endl << "1) Kilometers -> Meters" << endl << "2) Meters -> Kilometers" << endl << "3) Feet -> Meters" << endl << "4) Meters -> Feet" << endl << "5) Farenheit -> Celsius" << endl << "6) Exit" << endl; for ( ;; ) { cout << "Select: " << flush; cin >> choice; switch ( choice ) { case '1': cout << "Kilometers=" << flush; cin >> value; cout << value << " kilometers is " << value*1000.0 << " meters." << endl; break; case '2': cout << "Meters=" << flush; cin >> value; cout << value << " meters is " << value/1000.0 << " kilometers." << endl; break; case '3': cout<< "Feet=" << flush; cin >> value; cout << value << " feet is " << value*0.3048 << " meters." << endl; break; case '4': cout<< "Meters=" << flush; cin >> value; cout << value << " meters is " << value/0.3048 << " feet." << endl; break; case '5': cout<< "Farenheit=" << flush; cin >> value; cout << value << " Farenheit is " << (value-32.0)*(5.0/9.0) << " Celsius." << endl; break; case '6': return 0; default: cout << "Unknown choice!" << endl; } } }
that's if u're interested.. i mean nothing serious.. Ohhh can u differentiate too?
What do you mean? Like numerically? Or symbolically?
both but id say numerically would be good enough..
C++ is a general purpose programming language and its very low level (close to the machine). The idea of a mathematical function doesn't exist so you can't really symbolically differentiate.
i see.. i read that before in a thread on some programming site..
You could write all the code to do this, including all the various rules (chain rule, product rule, power rule, etc) and then develop a way to encode symbolic mathematical expressions but this gets very complicated.
okay so it is possible i might do it eventually as a project or something.. now how about numerically?
You can also just numerically differentiate, and that's very simple. Recall the difference quotient: \[\frac{f(x + h) - f(x)}{h}\] To take the numerical approximation of the derivative at x, just use a very small h.
yes..
Why ".." is there something you don't understand?
no im just thinking of putting that into code.. can you give me an example?
ohh wait a second.. hmm if i did h=0, then it would give me an error wouldnt it?
err
thats wrong one second
float h = 0.001; int x = 5; int approx = ((x + h)*(x + h) - x*x)/h;
hmm but what if i wanted a derivative of some function.. suppose: x^2+x+1 would that be possible? because u are saying that x is an integer already..
like f(x) = x^2+x+1 f'(5)-?
its simpler just to define a function, but im assuming you haven't gotten that far yet.
okay anyway i gtg will continue tomorrow thanks for the help, bye
typedef float FUNC_REALTOREAL ( float input ); float myPolynomial ( float input ) { return input * input + input + 1; } float derivative ( FUNC_REALTOREAL *realToRealFunc, float x, float h ) { return ( realToRealFunc ( x + h ) - realToRealFunc ( x ) ) / h; } int main () { int result = derivative ( myPolynomial, 5, 0.001 ); }
Some of the above might not be accessible until you learn a bit more C/C++.
ALCHEMISTA I NEED YOUR HELP!!! go to a new thread please!
Join our real-time social learning platform and learn together with your friends!