Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Would a double ever be implicitly converted to an unsigned int in arithmetic expressions?

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 by 1.0. The result is converted to unsigned int, then subtracted by ui. The result is converted to float, then assigned to fval.

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.
like image 590
AdamMenz Avatar asked Mar 11 '19 18:03

AdamMenz


People also ask

What types can int be implicitly converted to?

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.

Is int promoted to Double?

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 .

What is an implicit conversion?

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.

Is double to float implicit?

@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.

What are the implicit conversions between the built-in numeric types?

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.

Why is the 2 implicitly converted to a double?

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.

Are there any implicit conversions between the decimal and float types?

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:

What is the difference between int and unsigned int conversion?

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;


1 Answers

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.

like image 171
NathanOliver Avatar answered Nov 15 '22 06:11

NathanOliver