Differences between int vs long, float vs double.

Quick question,

I know what each of these are, so far I know that an int can go into a float but not the other way around,

how does that work for a float vs double?

And can someone precisely explain to me why that is so for int vs long, float vs doubles?

Thanks!
Double is a more precise float. C++ does not place restrictions of floating point values representation, but IEEE 754 is usually used (float is single precision and double is double precision). Actually double is usually native floating point type for modern CPU and should be used unless you have very compelling reasons to use floats instead.
http://en.wikipedia.org/wiki/IEEE_floating_point

There are several integral types: char, short, int, long and long long. There is not much restrictions on them:
First of all, their relative sizes should be like this:
sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)  
Size of char is always 1.
Additionally there is restrictions on minimum size in bits:
1
2
3
4
short: 16
int: 16
long: 32
long long: 64
  
This might help you too:
http://en.cppreference.com/w/cpp/language/types

I know that an int can go into a float but not the other way around
Float can go into int, but you will lose fraction part. Additionally there might be problem with very large floats being too alrge to be represented as int without overflowing.
Likewise large ints might be too unrepresentable as floats exactly (you will lose some info)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main()  {
    float f = INT_MAX;
    int i = f;
    if (i != INT_MAX)
        std::cout << "Float loses information\n";
    else
        std::cout << "Float does not loses information\n";
    double d = INT_MAX;
    int ii = d;
    if (ii != INT_MAX)
        std::cout << "Double loses information\n";
    else
        std::cout << "Double does not loses information\n";
}
Float loses information
Double does not loses information
So if int can go into a float, and vice versa, what happens with a long and a double?
Do they work the same way as well?
Any integral variable (char, short, int...) can be cast into floating point (float, double, long double) and back.
so char -> double, float -> long, etc. are possible. You just need to mind the precision when converting to floating point and range when converting to integral.
Topic archived. No new replies allowed.