0

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!

Curtis Fenner
  • 1,382
  • 1
  • 9
  • 18
Daniel Bandeira
  • 360
  • 1
  • 2
  • 12
  • 1
    What programming language? What are `Number1` and `Number2`? (Specifically, what types are they?) – Raymond Chen Oct 08 '18 at 01:46
  • 1
    Generally speaking, ALUs only work on operands of the same type - so if you're going to divide a `double`, the other operand is going to have to be converted to `double` as well, whether you explicitly write that in your code or not. – jasonharper Oct 08 '18 at 02:04
  • Jasonharper... That is what I want to know after all... So, generally, operands work with varible of the same type. I was wondering there would be a ALU operator for working witg Double and Int type (in a common computer), seeing that such operation could be found in, peheaps, majority of codes involving statistical calculations. – Daniel Bandeira Oct 08 '18 at 19:56

1 Answers1

0

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.

Chai T. Rex
  • 2,972
  • 1
  • 15
  • 33
  • Yes! About that I don't doubt a point. I've done a umbiguous question. I don't want to focus on the casting, but on the operator in ALU. Working with (double) operator (double) is the same as doing with (double) operator (int)? – Daniel Bandeira Oct 08 '18 at 19:34
  • As far as I know on x86 at least, double precision division requires two `double`s, so with an `int` on the bottom, it has to convert it to a `double`. Having a `double` from the start avoids that step. – Chai T. Rex Oct 08 '18 at 19:37
  • Such features I was asking for.. Thank you – Daniel Bandeira Oct 12 '18 at 13:12