Dividing by two integers does not return expected result

前端 未结 6 600
甜味超标
甜味超标 2021-01-25 13:18

I\'m currently writing a program that requires a preview of a live display, but the preview, of course, is scaled down. However, when I scale the PictureBox down, t

相关标签:
6条回答
  • 2021-01-25 13:18

    C#'s math is "correct". The understanding of what is being done is .. missing :-)

    The expression 4 / 3 (of type int / int) will evaluate to the integer value 1 as it is using integer division (both operands are integers). The resulting 1 is then implicitly coerced to a double value on assignment.

    On the other hand 4d / 3 will "work" (and results in a double 1.333_) because now it is double / int -> double / double (by promotion) -> double using the appropriate floating point division.

    Similarly, for Height / 4 (assuming Height is an integer), these would work:

    (double)Height / 4          // double / int -> double
    Height / 4d                 // int / double -> double
    (double)Height / (double)4  // double / double -> double
    

    Happy coding!

    0 讨论(0)
  • 2021-01-25 13:22

    You are doing integer division.

    what you need to do is this :

    private void FindOptimalRes(PictureBox picBox)
    {
        double h = Height / 4D; // or Height / 4.0
        double ratio = 4D / 3D; // or 4.0 / 3.0
        picBox.Size = new Size((int)(h * ratio), (int)h); // Size is now correct [133,100]
    }
    

    when you do a mathematical operation with an integer literal (no decimal places) it is implicitly typed as an int.

    Simply appending a capital D at the end of your literals (4D, 3D) will type them as doubles, and your math will be correct. Alternatively you can write 4.0, 3.0

    0 讨论(0)
  • 2021-01-25 13:23

    You are doing integer division:

    double ratio = 4 / 3; // evaluates to 1
    

    This won't give you the value you are looking for because the decimal point is being truncated, thus evaluating to 1 instead of 1.333. At least one of the operands needs to be a double:

    double ratio = 4.0 / 3.0; // evaluates to 1.333
    

    Same goes for Height. Change the 4 to 4.0.

    0 讨论(0)
  • 2021-01-25 13:26

    4 and 3 are both ints, so it gets turned to 1. Make them something floating-point:

    double ratio = 4.0 / 3.0;
    

    Note that you're also making the same mistake with Height (it doesn't matter right now, but it will - change it to 4.0). And if this is the actual code, why divide by four to multiply by four again?

    private void FindOptimalRes(PictureBox picBox)
    {
        picBox.Size = new Size(Height / 3, Height / 4);
    }
    
    0 讨论(0)
  • 2021-01-25 13:28

    Make sure division result is double

    double ratio = (double) 4 / 3; // double division 
    

    and no need to set your input values to double.

    var num1 = // an integer number
    var num2 = // an integer number
    
    //result is integer, because of integer/integer uses 'integer division'
    double result = num1 / num2; 
    
    //result is double , because of you forced to 'double division'
    double result = (double) num1 / num2;
    
    0 讨论(0)
  • 2021-01-25 13:35

    Maybe you should do a decimal division and not integer division:

    double h = Height / 4.0;
    double ratio = 4 / 3.0;
    

    If C# Math was off many things around the world would be off as well.

    0 讨论(0)
提交回复
热议问题