For the general principle, paxdiablo's answer contains the relevant parts. Most terminating decimal fractions cannot be exactly represented as binary floating point numbers, hence the value of the floating point variable is a little smaller or larger than the mathematical value of the number representation in the given string, so when you want to get the appropriate integer value after scaling, you should round and not truncate.
But in the specific example here, we have a different scenario. The closest IEEE754 double precision (64-bit binary) value to 1441.1441 is
1441.14409999999998035491444170475006103515625
which is indeed a little smaller than 1441.1441. But if that value is multiplied with 10000 as an IEEE754 double precision value, the result is exactly
14411441
What happens here is that, as is allowed per 5.2.4.2.2 paragraph 9
Except for assignment and cast (which remove all extra range and precision), the values yielded by operators with floating operands and values subject to the usual arithmetic conversions and of floating constants are evaluated to a format whose range and precision may be greater than required by the type.
(emphasis mine), the product is evaluated with a greater precision than required by the type (probably the x87 80-bit format), yielding a slightly smaller value, and when the result of the multiplication is converted to int
, the fractional part is discarded, and you get 14411440.
scanf("%lf",&num);
The value is stored in num
, so it must have exactly the precision of double
.
tmp=num*10000;
The product num * 10000
is neither stored nor cast to double
, so it may have greater precision, resulting in a smaller or larger value than the closest double
value. That value is then truncated to obtain the int
.
If you stored the product in a double
variable
num *= 10000;
tmp = num;
or cast it to double
before converting to int
,
tmp = (double)(num * 10000);
you ought to get the result 14411441 for the input 1441.1441
(but note that not all compilers always honour the requirement of converting to the exact required precision when casting or storing - violating the standard - so there's no guarantee that that will produce 14411441 with all optimisation settings).
Since many 64-bit platforms perform floating-point arithmetic using SSE instructions rather than the x87 coprocessor, the observed behaviour is less likely to appear on 64-bit systems than on 32-bit systems.