+ 1

C+ multiplying a double with an undeclared number

Hi masterminds, I have an operation to multiple a double (already declared, say double x) with an undeclared variable (e.g. x *= (1.0 + 0.05 * 3 * 2) ) What would happen? Would this return a number with the same precision as x (i.e. 14 significant numbers)? Thanks!!!

23rd Nov 2020, 1:03 AM
Jin
Jin - avatar
2 Réponses
+ 7
(1.0 + 0.05 * 3 * 2) is a literal expression. Meaning it is an expression that uses numeric literals and since 1.0 or 0.05 will be evaluated as a double implicitly, the entire expression shall be evaluated as a double, because it is the largest type used in the expression. Since x is a double and the expression is a double there will be no further conversion and therefore no issue. However, if x were an int you'd have an error due to implicit lossy conversion. (Trying to implicitly put a larger type in a smaller container). If the expression was made up solely of integers (int) and x were to remain a double, an implicit conversion would take place without issue, because double is a compatible larger type than int.
23rd Nov 2020, 2:05 AM
ChaoticDawg
ChaoticDawg - avatar
+ 6
C+ is not a language.
23rd Nov 2020, 7:55 AM
Sonic
Sonic - avatar