Hey how can i convert scientific notations ??
if i use float in my program
is it possible to change it without casting ????
Hey how can i convert scientific notations ??
if i use float in my program
is it possible to change it without casting ????
example, please?
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
run the program and enter 273.15Code:#include <iostream> using namespace std; int main() { char againKel; cout << "\n================================ Kelvin Converter ==============================\n\n" ; do { cout << "Enter °K (Kelvin) degree to convert :"; float kTemp2Conv; // Kelvin Temperature to Convert if (cin >> kTemp2Conv) { float kCelsius = kTemp2Conv - 273.15; float kFahrenheit = (kTemp2Conv * 1.8) - 459.67; float kRankine = kTemp2Conv * 1.8; cout << "\nAnswer: " << kTemp2Conv << " K (Kelvin) in Celsius scale = " << kCelsius; cout << "\nAnswer: " << kTemp2Conv << " K (Kelvin) in Fahrenheit scale = " << kFahrenheit; cout << "\nAnswer: " << kTemp2Conv << " K (Kelvin) in Rankine scale = " << kRankine; cout << "\n\n"; } else { cout << "\nINVALID INPUT !"; cout << "\nPlease enter numerical values.\n"; } cin.clear(); cin.ignore(100, '\n'); cout << "\nDo you want to convert temperature again ?"; cout << "\nEnter Y for yes, N for no."; cout << "\nEntering wrong option other than Y/N will result in getting back to \nConvert Temprature menu."; cout << "\n[y]/[n]:"; cin >> againKel; } while (againKel == 'y'|| againKel == 'Y'); }
and then look at the answer of celsius
273.15 K= 0 C
but it shows me a very long answer
-6.10352e-06 this is what i Got now
is there any way to convert the scientific digit ?
Last edited by manzoor; 11-13-2007 at 09:09 AM.
In this case, what you are showing is the "rounding error", which is caused by the fact that floating point numbers don't have infinite precision, so when you mathematically operate on the values, some decimal poins down from your actual value may be remaining in the number. If you set the precision for printing, it will print only so many decimal places. Look up the "iomanip" methods on the iostreams, and you'll find several useful functions.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
well after reading your post I googled
and found this
setprecision()
is this the right functio ?
or fixed()?
You can also try using double everywhere instead of float or changing your calculations to explicitly use floats instead of double:
You should be getting warnings about the above lines in your original code, some warnings should be treated with greater attention.Code:float kCelsius = kTemp2Conv - 273.15f; float kFahrenheit = (kTemp2Conv * 1.8f) - 459.67f; float kRankine = kTemp2Conv * 1.8f;
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
but what about those ? functions i mentioned in my earlier post aren't they valid ?
is this code working ??
Code:// modify basefield #include <iostream> #include <iomanip> using namespace std; int main () { double a,b,c; a = 3.1415926534; b = 2006.0; c = 1.0e-10; cout.precision(5); cout << a << '\t' << b << '\t' << c << endl; cout << fixed << a << '\t' << b << '\t' << c << endl; cout << scientific << a << '\t' << b << '\t' << c << endl; return 0; }
Last edited by manzoor; 11-13-2007 at 09:48 AM.