有问题的C#代码涉及双精度和整数

时间:2009-04-05 08:25:39

标签: c# integer double

    for (iy = 0; iy < h; iy++)
    {
        double angy = (camera.fov_y / h) * iy;
        for (ix = 0; ix < w; ix++)
        {
            double angx = (camera.fov_x / w) * ix;
            //output[ix,iy].r = (int)Math.Round(255 * (angy / camera.fov_y);
            //output[ix,iy].b = (int)Math.Round(255 * (angy / camera.fov_y); 
            double tr = (angx / camera.fov_x) * 255D;
            double tb = (angy / camera.fov_y) * 255D;
            Console.Write("({0},{1})",Math.Round(tr),Math.Round(tb));

            output.SetPixel(ix, iy, Color.FromArgb(Convert.ToInt32(tr), 0, Convert.ToInt32(tb)) );
            Console.Write(".");
        }
        Console.WriteLine();
    }

有人能看到该代码的任何直接问题吗? 变量trtb始终评估为0。

如果需要,我很乐意提供更多信息。

3 个答案:

答案 0 :(得分:1)

您还没有给出其他变量的类型 - 特别是camera.fov_xcamera.fov_y的类型是什么?如果它们都是整数,那么初始化angxangy的行将使用整数运算来计算。

这可以通过转换其中一个操作数来修复:

double angy = ((double) camera.fov_y / h) * iy;

虽然fovyfovx变量已经翻倍,但这不是问题。

你能举出一个完整的例子,我们可以自己编译和测试吗?

编辑:Koistya Navin的编辑太过分了。您只需要一个表达式的一个操作数作为使用双算术计算的整个事物的double。 (它需要是正确的表达式 - 如果你执行(a/b) * c并将c转换为double,则乘法将使用双重算术完成,但a / b仍可以作为整数完成。)

这里的列表已经适当改变,以确保在任何地方都应该使用双算术:

// Changed loops to declare the variable, for stylistic purposes
for (int iy = 0; iy < h; iy++)
{
    // Changed here - cast camera.fov_y
    double angy = ((double) camera.fov_y / h) * iy;
    for (int ix = 0; ix < w; ix++)
    {
        // Changed here - cast camera.fov_x
        double angx = ((double) camera.fov_x / w) * ix;
        //output[ix,iy].r = (int)Math.Round(255 * (angy / camera.fov_y);
        //output[ix,iy].b = (int)Math.Round(255 * (angy / camera.fov_y); 
        double tr = (angx / camera.fov_x) * 255D;
        double tb = (angy / camera.fov_y) * 255D;
        Console.Write("({0},{1})", Math.Round(tr), Math.Round(tb));

        output.SetPixel(ix, iy, Color.FromArgb(Convert.ToInt32(tr), 
                                               0,
                                               Convert.ToInt32(tb)) );
        Console.Write(".");
    }
    Console.WriteLine();
}

答案 1 :(得分:1)

它知道它不是原始问题的一部分,但使用SetPixel(..)效率不高,如果您打算在光线追踪引擎中使用它,可能会出现问题。

您可能希望使用LockBits()方法,有关详细信息,请参阅此answerthis one。他们的另一种方法是使用“不安全”的C#代码访问数据,这允许您使用指向数据的指针。有关详细信息,请参阅this question,我使用“不安全”代码获得了~x2加速。

答案 2 :(得分:0)

不要忘记将你的整数投射到双打。例如:

for (iy = 0; iy < h; iy++)
{
    double angy = ((double) camera.fov_y / h) * iy;
    for (ix = 0; ix < w; ix++)
    {
        double angx = ((double) camera.fov_x / (double) w) * (double) ix;
        output[ix,iy].r = (int) Math.Round(255 * (angy / camera.fov_y);
        output[ix,iy].b = (int) Math.Round(255 * (angy / camera.fov_y);
        double tr = (angx / camera.fov_x) * 255D;
        double tb = (angy / camera.fov_y) * 255D;
        Console.Write("({0},{1})",Math.Round(tr), Math.Round(tb));
        output.SetPixel(ix, iy, Color.FromArgb(
            Convert.ToInt32(tr), 0, Convert.ToInt32(tb)) );
        Console.Write(".");
    }
    Console.WriteLine();
} 

快速参考:

 int * double = double
 int / double = double
 double * int = double
 double / int = double

 int * int = int
 int / int = int // be carefull here!

 1 / 10 = 0 (not 0.1D)
 10 / 11 = 0 (not 1)
 1D / 10 = 0.1D
 1 / 10D = 0.1D
 1D / 10D = 0.1D