Hi, what does this output mean?
Program code is as follows:Code:Discriminant = sqrtf(-16) or -1.#INDi
Thanks a lot!Code:cout << "Discriminant = sqrtf(" << discriminant << ") or "; d1 = sqrtf(discriminant); cout << d1 << "i" << endl << endl;
Hi, what does this output mean?
Program code is as follows:Code:Discriminant = sqrtf(-16) or -1.#INDi
Thanks a lot!Code:cout << "Discriminant = sqrtf(" << discriminant << ") or "; d1 = sqrtf(discriminant); cout << d1 << "i" << endl << endl;
It means that the sqrt() of discriminant could not be calculated properly. The square root of negative numbers is not defined.
Of course, you could always use
As for a more technical description of why it's "-1.#INDi" . . . I'm not entirely sure, but I'd guess that perhaps the trailing "i" means it's an imaginary number, which is what you get when you sqrt() a negative number. Google for more info.Code:sqrt(fabs(discriminant))
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
http://www.randelshofer.ch/fhw/gri/float.html
#IND stands for indefinite.
Other things you can get from time to time are #INF and #NAN.
> I'm not entirely sure, but I'd guess that perhaps the trailing "i" means it's an imaginary number
Nah, that's just the trailing i in the cout statement.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Hi..
Thanks dwks and Salem.. what you guys said made a lot of sense. It was the first time I have encountered that -1.#IND and now, at least I have an idea of what it stands for.
Yes, the i came from the cout statement. However, I am trying to get the formula for the square root of the discriminant. Suppose, discriminant is equal to -16. Answer should be 4. How do I efficiently write the formula?
Any ideas? I will experiment on the fabs function.
Thanks guys..
Have you considered using the std::complex class template from <complex>? For example:However, I am trying to get the formula for the square root of the discriminant. Suppose, discriminant is equal to -16. Answer should be 4. How do I efficiently write the formula?
Code:#include <iostream> #include <complex> template<typename T> void print(std::ostream& out, const std::complex<T>& x); int main() { std::complex<double> x(-16); std::cout << "sqrt("; print(std::cout, x); std::cout << ") = "; print(std::cout, sqrt(x)); std::cout << std::endl; } template<typename T> void print(std::ostream& out, const std::complex<T>& x) { std::ios::fmtflags old_flags = out.flags(); out << x.real() << std::showpos << x.imag() << "i"; out.flags(old_flags); }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)