# Thread: exponential form etc

1. ## 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");

}```

2. 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. Originally Posted by KCfromNC
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

4. Originally Posted by jackson6612
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.

5. Originally Posted by jackson6612
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