Search code examples
precisionalu

ALU, double and int


Sometimes, writing a code, situations such

 (double)Number1/(int)Number2     //division of a double type varible by a int one.

appears to me (and I think, to all of you more or less often) and I never knows what really happens if I rewrite (double) over (int).

    (double)Number1/(double)Number2

Is the performace the same? And the precision? And the time taken to perform it... Changes? Does the compiler, in general case (if it is possible to say such thing), write the same binary file. i. e., exe file? Does the called ALU operator chang?

I believe that a formal answer would depends on architecture of machine, compiler and language and a lot of stuff more. But... In these cases, how to have a notion about what would happen in "my code" and what choice would be better (if there is an appreciable difference)?

Thank you all for your replies!


Solution

  • The precision can be different.

    For example, if Number2 is originally a double, converting it to an int with (int)Number2 before the division can lose a lot of information both through truncating any bits after the binary point and by truncating any integral bits that don't fit in the int.