exponential form etc

This is a discussion on exponential form etc within the C++ Programming forums, part of the General Programming Boards category; Hi No matter what value of "a" I use the "b" is stuck at 2,60,000 (1+2.6e5). Why is so? How ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    exponential form etc

    Hi

    No matter what value of "a" I use the "b" is stuck at 2,60,000 (1+2.6e5). Why is so?

    How could I also get the value of "b" in exponential form?

    Is the the "F" I used in the declaration "float a = 2.6e5F" really important? As far as my limited knowledge goes "F" could be omitted. But there should be some reason to include this. I have seen a author using it in the book.

    It would be extremely kind of you if you could help me with the above queries. Thanks a lot for your time.






    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
     	
     	float a = 2.6e5F;
     	float b = 1 + a;
     	
     	cout << "enter a = ";
     	cin >> a;
     	
     	cout << b << endl;
     	
     	system("pause");
     	
    }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Q1 : because statements get executed in the order they're written. If you want to make b = 1 + a after the user enters a, put b=1+a after the cin statement.

    Q2 : cout << scientific << b << endl;

    Examples here : scientific - C++ Reference

    Q3 : F means the constant is single precision floating point. Without it, constants with a decimal point in them are considered doubles. Probably doesn't matter in most cases, but there may be some unexpected stuff that happens if the value can be represented by a double but not a float.

    The bigger problem is when people use integer constants and expect a floating point value.

    float f = 1/2;

    f will be zero. The two constants are integers, 1/2 in integer math is 0 since you can't represent a fraction in an integer. This 0 is then converted into 0.0f and assigned to the variable, and you see problems later. This can be fixed in several ways :

    float f = (float)1/2;
    float f = 1f/2;
    float f = 1./2;

    and so on. Just something to make sure at least one of the vars is floating point.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by KCfromNC View Post
    Q3 : F means the constant is single precision floating point. Without it, constants with a decimal point in them are considered doubles. Probably doesn't matter in most cases, but there may be some unexpected stuff that happens if the value can be represented by a double but not a float.

    The bigger problem is when people use integer constants and expect a floating point value.

    float f = 1/2;

    f will be zero. The two constants are integers, 1/2 in integer math is 0 since you can't represent a fraction in an integer. This 0 is then converted into 0.0f and assigned to the variable, and you see problems later. This can be fixed in several ways :

    float f = (float)1/2;
    float f = 1f/2;
    float f = 1./2;

    and so on. Just something to make sure at least one of the vars is floating point
    .
    Thanks a lot, KCfromNC. I'm really grateful for your help.

    Unfortunately I couldn't completely fully understand the quoted part. It would be helpful if you can explain it a bit.

    Best wishes
    Jackson
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    Quote Originally Posted by jackson6612 View Post
    Hi

    No matter what value of "a" I use the "b" is stuck at 2,60,000 (1+2.6e5). Why is so?
    Because C++ code executes from top to bottom, and as of yet, it is incapable of time travel.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by jackson6612 View Post
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
     	
     	float a = 2.6e5F;
     	float b = 1 + a;
     	
     	cout << "enter a = ";
     	cin >> a;
     	
     	cout << b << endl;
     	
     	system("pause");
     	
    }
    This suggests to me that you're thinking about '=' in the mathematical way. In C++ and most languages, '=' means something else, it means assignment. For example, it is perfectly legitimate to write:

    Code:
    b = 1; // Now b has a value of 1
    b = 1 + b; // 1 + b is 1 + 1 which is 2, so now b has a value of 2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. exponential question
    By libchk in forum C Programming
    Replies: 3
    Last Post: 11-09-2007, 03:09 AM
  2. mathematics <exponential>
    By xddxogm3 in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 11-17-2004, 11:48 PM
  3. Negative Exponential
    By vasanth in forum Tech Board
    Replies: 2
    Last Post: 09-01-2004, 09:31 AM
  4. long form of year to short form?
    By bc120 in forum C Programming
    Replies: 2
    Last Post: 12-31-2001, 04:34 PM
  5. exponential
    By hei in forum C Programming
    Replies: 2
    Last Post: 10-10-2001, 11:46 AM

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