In C++ Primer, by Stanley B. Lippman, the section on "Implicit Conversions" says that:
int ival; unsigned int ui; float fval; fval = ui - ival * 1.0;
ival
is converted to double, then multiplied by1.0
. The result is converted tounsigned int
, then subtracted byui
. The result is converted tofloat
, then assigned tofval
.
But I don't think so: I think that in fact ival
is converted to double then multiplied by 1.0
then ui
is which is of type unsigned int
is converted to double not the contrary and then the result of multiplication is subtracted from the converted to converted to double ui
value. finally convert this final double value to float and assign it to fval
.
To ensure what I say:
ival = 5;
ui = 10;
fval = 7.22f;
dval = 3.14;
std::cout << typeid(ui - ival * 1.0).name() << std::endl; // double
std::cout << (ui - ival * 1.7) << std::endl; // 1.5 this proves that the unsigned int ui is converted to double not the contrary that is because C++ preserves precision. otherwise the decimal part is truncated.
Any integral numeric type is implicitly convertible to any floating-point numeric type. There are no implicit conversions to the byte and sbyte types. There are no implicit conversions from the double and decimal types. There are no implicit conversions between the decimal type and the float or double types.
For example, an int value is assigned to a double variable. This conversion is legal because doubles are wider than ints. Here are the Type Promotion Rules: All byte and short values are promoted to int .
An implicit conversion sequence is the sequence of conversions required to convert an argument in a function call to the type of the corresponding parameter in a function declaration. The compiler tries to determine an implicit conversion sequence for each argument.
@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.
The following table shows the predefined implicit conversions between the built-in numeric types: The implicit conversions from int, uint, long, ulong, nint, or nuint to float and from long, ulong, nint, or nuint to double may cause a loss of precision, but never a loss of an order of magnitude.
The 2 is implicitly converted to a double because foo is a double. You do have to be careful because if foo was, say, an integer, integer division would be performed and then the result would be stored in halfFoo.
There are no implicit conversions between the decimal type and the float or double types. A value of a constant expression of type int (for example, a value represented by an integer literal) can be implicitly converted to sbyte, byte, short, ushort, uint, ulong, nint, or nuint, if it's within the range of the destination type:
Conversion to the unpromoted underlying type is better for the purposes of overload resolution ; a bit field type can be converted to int if it can represent entire value range of the bit field, otherwise to unsigned int if it can represent entire value range of the bit field, otherwise no integral promotions apply;
Your assumption is correct and the book is wrong.
fval = ui - ival * 1.0;
can be rewritten as
fval = ui - (ival * 1.0);
so that gives us
float = unsigned - (int * double)
The (int * double)
becomes a double
because of the usual arithmetic conversions giving us
float = unsigned - double
which again results is in a double
and we assign that double
to the float
variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With