1

I'm sorry but I couldn't find an answer for this question using Google. I'm not an English speaker and maybe I haven't searched using the right words.

I remember that a data type's size depends on the size of the word of the architecture (or maybe I'm wrong). But, I'm not sure if the size also depends on operating system.

Now I'm working with Windows 7 32 bits and I'm not sure if a float will change its size when I use Windows 7 64bits.

I'm using Visual Studio 2012 to compile my solution using Win32 project.

Will the size change if I change windows version from 32 to 64bits?

I'm not asking about what is the different between float and double. I'm asking if float will have the same size in memory (number of bits in memory) on the same computer if I change operating system from Windows 7 32 bits to Windows 7 64 bits (or viceversa).

VansFannel
  • 45,055
  • 107
  • 359
  • 626
  • 4
    [It depends on the platform.](http://en.cppreference.com/w/cpp/language/types) It probably will not change here. – asu Oct 28 '16 at 14:43
  • @EliSadoff I'm not asking about what is the different between float and double. I'm asking if float will have the same size in memory (number of bits in memory) on the same computer if I change operating system from Windows 7 32 bits to Windows 7 64 bits (or viceversa). – VansFannel Oct 28 '16 at 14:52
  • 1
    If all you care about is Windows 32-bit vs Windows 64-bit (and not arbitrary operating systems as the title of your question may lead one to believe), then the only fundamental types that change size are pointers. `float` and `double` remain 4 bytes and 8 bytes large, correspondingly. In fact, Windows 64 can run both 32-bit and 64-bit programs; for the former, everything mostly looks like they are running under Windows 32 (including pointers that are 4 bytes large). – Igor Tandetnik Oct 28 '16 at 14:53
  • @IgorTandetnik Yes, you are right. With Windows 64bits you can address more memory because the pointers are bigger. Sorry, but I don't know how to explain it in English. – VansFannel Oct 28 '16 at 14:57
  • 1
    All modern processors use the [IEEE 754](https://en.wikipedia.org/wiki/IEEE_floating_point) standard for floating point, which is very tightly specified. If a processor doesn't use IEEE, it probably can't change its word size either - I haven't seen one in many years. – Mark Ransom Oct 28 '16 at 14:58
  • generally the answer will be [similar to int types](http://stackoverflow.com/q/2331751/995714). http://stackoverflow.com/a/589690/995714 – phuclv Oct 28 '16 at 15:14

2 Answers2

5

Formally, the size of floating-point types depends on the particular compiler used.

In practice, though, for systems based on chips with built-in floating-point hardware (i.e. all except for some embedded systems), the compiler will use the floating-point hardware, and all compilers that target that chip will use the same sizes for float and double.

Further, desktop hardware has settled in on the IEEE 754 floating-point standard, which sets out the size of float and double (long double is a rather different matter). So when you're targeting desktop systems, it's safe to assume that float will be the same size everywhere, and that double will be the same size everywhere.

If you're concerned about that, you can use std::numeric_limits<float>::is_iec559 and std::numeric_limits<double>::is_iec559 to check whether the floating-point implementation that your compiler uses conforms to IEEE 754 (IEC 559 is an older name for the IEEE 754 standard).

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
1

3.9.1 Fundamental types

There are three floating point types: float, double, and long double. The type double provides at least as much precision as float, and the type long double provides at least as much precision as double. The set of values of the type float is a subset of the set of values of the type double; the set of values of the type double is a subset of the set of values of the type long double. The value representation of floating-point types is implementation-defined.

Emphasis is mine. The C++ standard does not require any particular representation of floating point types. The size of a floating data type can change, or may remain the same, between different operating systems, different versions of the operating system, or even different compilers for the same operating system, or even different compilation options with the same compiler.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148