Why a float in a double?

I'm curious why this line of code possible.
double var1 = 23.0f;
In comparing var1 to a normal float with the same value of 23.0 will test to be equal, but comparing this same var1 to a double var2 = 23.0d will show them as unequal.
What is the point of declaring a double with a float inside it if you can only test it's equality with other floats and not other doubles?
What is the point of declaring a double with a float inside it if you can only test it's equality with other floats
You should not compare floating point values to other at all. Most compilers with properly set warning levels should issue a warning like "comparison of floating point values with == or != is dangerous".

On your problem: cannot reproduce:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <iomanip>

#define CHECK_IMPL(x) #x << " -> " << (x) << '\n'
#define CHECK(x) CHECK_IMPL(x)

int main()
{
    double d = 23.0f;
    float f = 23.0f;
    double dd = 23.0;
    std::cout << std::boolalpha << CHECK(d == f) << CHECK(d == dd);
}
d == f -> true
d == dd -> true
http://coliru.stacked-crooked.com/a/39e6aff266ed605b

And I would expect that whole numbers wit magnitude this small would be represented exactly.
Yes, integers with an absolute value below 2^24 are represented exactly and comparisons and conversions are performed exactly.
Topic archived. No new replies allowed.