2

In C++ we can write something like

float f = 3.55;

and it is a legal statement, whereas the type of real number numerals is double and we are storing that double into floating point number. It essentially means storing 8 bytes into 4 bytes (a possible data loss)? My question is that when I write

long l = 333; 
int y = l;

I get an error because long value is converted into int value (possible data loss). but why don't I encounter a problem when storing 8 byte double real numeral in floating point (4 byte)?

halfer
  • 19,824
  • 17
  • 99
  • 186
Muhammad Adeel Zahid
  • 17,474
  • 14
  • 90
  • 155
  • 2
    When converting one integer value to another you face the risk of losing the value altogether. When converting one floating point format to another you only risk losing precision. Maybe? Just a guess. – Jonathan Potter Mar 04 '15 at 06:01
  • 1
    There is no error using g++. So maybe its compiler specific? – Marcin Mar 04 '15 at 06:03
  • 1
    @JonathanPotter: `double` typically has greater precision *and* range than `float`. In any case, conversions between any two numeric types can be done implicitly; no diagnostic is required. – Keith Thompson Mar 04 '15 at 06:29
  • Don't use [Turbo C](http://stackoverflow.com/questions/3920351/what-is-wrong-with-using-turbo-c)/[Turbo C++](http://stackoverflow.com/questions/1961828/why-not-to-use-turbo-c) – phuclv Mar 04 '15 at 08:22

2 Answers2

2

From §4 Standard conversions [conv] C++11:

Standard conversions are implicit conversions with built-in meaning. Clause 4 enumerates the full set of such conversions. A standard conversion sequence is a sequence of standard conversions in the following order:

...

Zero or one conversion from the following set: integral promotions, floating point promotion, integral conversions, floating point conversions, floating-integral conversions, pointer conversions, pointer to member conversions, and boolean conversions.

So conversion between two numeric types is allowed implicitly as it makes sense also if used carefully. For example When you calculate Amount(int) from P(int), R(float) and T(int);

And from §4.8 Floating point conversions [conv.double],

  • A prvalue of floating point type can be converted to a prvalue of another floating point type. If the source value can be exactly represented in the destination type, the result of the conversion is that exact representation. If the source value is between two adjacent destination values, the result of the conversion is an implementation-defined choice of either of those values. Otherwise, the behavior is undefined.
  • The conversions allowed as floating point promotions are excluded from the set of floating point conversions.

It appears double to float conversion is implicitly performed by the compliant C++ compiler. (At the cost of potentially loosing the precision)

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
0

Your example is not an error and should compile.

When you assign a larger integer type to a smaller integer type (or perform any conversion that doesn't quality as a promotion), an integral conversion occurs and precision may be lost.

Similarly, floating point conversion occurs when you assign one floating point type to another floating point type; the result is either the same value, or a value close to it, unless the source value exceeds the range of the destination type.

Collin Dauphinee
  • 13,664
  • 1
  • 40
  • 71