int s = 1e9;
What is type of 1e9
and how precise is it? (Is it exactly equal to 1000000000?)
Printing type of a value/variable to stdout
would be useful if it's possible.
int s = 1e9;
What is type of 1e9
and how precise is it? (Is it exactly equal to 1000000000?)
Printing type of a value/variable to stdout
would be useful if it's possible.
1e9
is a double
that has an exact representation in the IEEE floating point representation. The C++ standard doesn't mandate IEEE floating point.
s
is an int
, so the double
value will be automatically converted to an int
. How this takes place is to some extent up to the implementation. On most machines nowadays, this conversion means s
will be given an initial value of 1000000000.
For what it's worth, you can write code that shows that 1e9
has type double
:
#include <iostream>
void show_type(double value) {
std::cout << "type = double\n";
}
void show_type(...) {
std::cout << "Type != double\n";
}
int main() {
show_type(1e9);
return 0;
}
Of course, if you don't know what type it has, it's quite a bit more work to provide an overload for every possible type, but the principle is the same nonetheless.
If you change it to float
or double
it's going to be pretty precise, but not every computation with it will be exact (thnks to Kerrek SB), there are limits to precision.
Note, that the precision is not the property of notation, anyway, the notation is the exactness itself.
9.0 / 3.0
to be exactly 3.0
... Why not? –
Makeshift double x = 9.0, y = 3.0;
Now the value of x/y
should be reliably 3.0
, non? My point is that there are computations that are indeed exact, and predictably so. (Likewise for 1e9 / 1e8
.) –
Makeshift © 2022 - 2024 — McMap. All rights reserved.