Divide error during debug

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mudman

    Divide error during debug

    I'm running Visual Studio.NET and am experiencing a problem during debugging
    (i assume this will also be the case during normal operation). If the
    variable "test" is a double and "denom" a finite integer, the operation test
    = 1 / denom; results in test having a value of 0.000000! However, if denom is
    also a double, test has the correct value. Dumb question perhaps, but am i
    missing something?
  • William DePalo [MVP VC++]

    #2
    Re: Divide error during debug

    "mudman" <mudman@discuss ions.microsoft. com> wrote in message
    news:94BBC357-2088-4DA1-881F-D69E095C43A1@mi crosoft.com...[color=blue]
    > I'm running Visual Studio.NET and am experiencing a problem during
    > debugging
    > (i assume this will also be the case during normal operation). If the
    > variable "test" is a double and "denom" a finite integer, the operation
    > test
    > = 1 / denom; results in test having a value of 0.000000! However, if denom
    > is
    > also a double, test has the correct value. Dumb question perhaps, but am i
    > missing something?[/color]

    If you have something like this

    int denom;
    double test;

    test = 1 / denom;

    then test will be assigned a value of zero (where denom is not 0). That's
    because the division happens in integers, it yields a result of 0 and
    integer 0 is converted to double 0.

    If on the other hand you have an expression that contains integers and
    doubles then the integers are promoted to doubles before the division and
    assignment happen.

    Is that what you see?

    Regards,
    Will


    Comment

    • mudman

      #3
      Re: Divide error during debug

      This would indeed explain what i am experiencing, although it is somewhat
      illogical- essentially, the expression is being interpreted as:

      test = (double)((int)( 1 / denom));

      Thanks.
      [color=blue]
      > If you have something like this
      >
      > int denom;
      > double test;
      >
      > test = 1 / denom;
      >
      > then test will be assigned a value of zero (where denom is not 0). That's
      > because the division happens in integers, it yields a result of 0 and
      > integer 0 is converted to double 0.
      >
      > If on the other hand you have an expression that contains integers and
      > doubles then the integers are promoted to doubles before the division and
      > assignment happen.[/color]

      Comment

      • William DePalo [MVP VC++]

        #4
        Re: Divide error during debug

        "mudman" <mudman@discuss ions.microsoft. com> wrote in message
        news:28C9D12A-1933-4350-864E-307D76939CBB@mi crosoft.com...[color=blue]
        > This would indeed explain what i am experiencing, although it is somewhat
        > illogical- essentially, the expression is being interpreted as:
        >
        > test = (double)((int)( 1 / denom));[/color]

        Is is that, or essentially that?

        Your C style cast above

        (int)

        forces the devision to be done in integers, no? And that zero result is cast
        to double.

        Why not try

        test = (double) 1 / denom;
        [color=blue]
        > Thanks.[/color]

        You are welcome.

        Regards,
        Will


        Comment

        Working...