I have the following code:
float test = 1/(10^(15));
std::cout << "test: " << test << std::endl;
But I get "test: 0" as the value. I want "test: 0.000000000000001".
How do I achieve this?
There are multiple problems here.
10 ^ 15
It looks like you expect "^" to be the exponent operator, that is ten raised to the 15th power.
It is not. In C and C++, "^" is a bitwise exclusive-or operator.
The second problem is that all values in the expression are integer values. This means that (1/(10^15))
is computed as follows:
1) 10^15=5, remember that ^
is really a bitwise exclusive-or operator.
2) 1/5=0, because these are integer values, this is integer division, which is why you get the value of 0.
The best way to fix this is use scientific notation:
float test = 1/(1e15);
std::cout << "test: " << test << std::endl;
The scientific notation serves two purposes simultaneously: one, you get the right value, and two, this is automatically a floating point value, so the division is carried out as a floating point division.