Re: How to get a double from the quotient of two int values?




"Adam Funk" <a24061@xxxxxxxxx> wrote in message
news:ddsor4$31f2$1@xxxxxxxxxxxxxxxxxxxx
> int a = 3;
> int b = 7;
> double x, y;
> x = a / b;
> y = (1.0 * a) / b;
> System.out.println("x = " + x);
> System.out.println("y = " + y);
>
> produces
>
> x = 0.0
> y = 0.42857142857142855
>
>
> I understand *why* x is 0.0 (the / operator returns an int, which is then
> implicitly cast to a double).
>
> But I'd like to know if "(1.0 * a) / b" is the correct way to get the
> double
> answer?

Cast the integers to doubles.

y = (double)a / (double)b

- Oliver


.



Relevant Pages

  • Re: Graphics class problem
    ... Other posters have given you hints for implementing the first question. ... int java.awt.image.BufferedImage.getRGB ... Prev by Date: ...
    (comp.lang.java.gui)
  • Re: Whats the deal with size_t?
    ... >> implicitly cast an int to a size_t as necessary. ... the code above fails to compile because sizeof requires ... its arguments will need to be converted to struct foo **, ...
    (comp.lang.c)
  • Re: Whats the deal with size_t?
    ... >> implicitly cast an int to a size_t as necessary. ... >> compiler not support that feature? ... when the prototype of qsort calls for a size_t, ...
    (comp.lang.c)
  • Re: How to get a double from the quotient of two int values?
    ... If you explicitly cast either operand to a double you'll get the double ... division operator and the other operand will be implicitly cast. ... > int a = 3; ...
    (comp.lang.java)
  • Re: Macros
    ... int half = prod; ... and then the will be implicitly cast to int for the assignment. ... surprising that prodcan produce different results depending ...
    (comp.lang.c)