Scientific notation

This is a discussion on Scientific notation within the C++ Programming forums, part of the General Programming Boards category; Hey how can i convert scientific notations ?? if i use float in my program is it possible to change ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    164

    Scientific notation

    Hey how can i convert scientific notations ??

    if i use float in my program

    is it possible to change it without casting ????

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,700
    example, please?
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    	{	
    		char againKel;
    		cout << "\n================================ Kelvin Converter ==============================\n\n" ;		
    		
    		
    		do {
    		cout << "Enter &#176;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');
    	}
    run the program and enter 273.15

    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 08:09 AM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    well after reading your post I googled

    and found this

    setprecision()

    is this the right functio ?

    or fixed()?

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    You can also try using double everywhere instead of float or changing your calculations to explicitly use floats instead of double:
    Code:
    float kCelsius = kTemp2Conv - 273.15f;
    float kFahrenheit = (kTemp2Conv * 1.8f) - 459.67f;
    float kRankine = kTemp2Conv * 1.8f;
    You should be getting warnings about the above lines in your original code, some warnings should be treated with greater attention.
    "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

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    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 08:48 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formating Scientific Notation Output
    By RedSoxFan in forum C Programming
    Replies: 2
    Last Post: 12-21-2005, 09:38 AM
  2. Scientific notation
    By Lionmane in forum C Programming
    Replies: 9
    Last Post: 06-01-2005, 11:10 PM
  3. Reading scientific notation
    By PunkyBunny300 in forum C Programming
    Replies: 1
    Last Post: 04-24-2003, 09:22 PM
  4. Scientific Notation
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 02-18-2002, 02:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21