涉及双精度数和整数的错误 C# 代码

发布于 2024-07-15 22:37:39 字数 826 浏览 4 评论 0原文

    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。

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

    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();
    }

Can anyone see any immediate problems with that code?
The variables tr and tb always evaluate to 0.

I'm happy to provide more information if it is needed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泪是无色的血 2024-07-22 22:37:39

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

这可以通过转换操作数之一来解决:

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

尽管 fovyfovx 变量已经是双精度数,但这不是问题。

您能否给出一个完整的示例,我们可以自己编译和测试?

编辑:Koistya Navin 的编辑太过分了。 只需要表达式的一个操作数为双精度数,即可使用双精度算术计算整个表达式。 (不过,它必须是正确的表达式 - 如果您执行 (a/b) * c 并将 c 转换为 double,则乘法将使用 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();
}

You haven't given the types for the other variables - in particular, what are the types of camera.fov_x and camera.fov_y? If they're both integers, then the lines initializing angx and angy will be evaluated using integer arithmetic.

This can be fixed by casting one of the operands:

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

The fovy and fovx variables are already doubles though, this isn't the problem.

Could you give a complete example of this, which we can compile and test ourselves?

EDIT: Koistya Navin's edit is overkill. You only need one operand of an expression to be a double for the whole thing to be computed using double arithmetic. (It needs to be the right expression though - if you do (a/b) * c and cast c a double, the multiplication will be done with double arithmetic but a/b might still be done as integers.)

Here's the listing changed appropriately to make sure double arithmetic is used everywhere it should be:

// 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();
}
划一舟意中人 2024-07-22 22:37:39

不要忘记将整数转换为双精度数。 例如:

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

Don't forget to cast your integers to doubles. For example:

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();
} 

Quick Reference:

 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
请止步禁区 2024-07-22 22:37:39

它知道这不是您原来问题的一部分,但使用 SetPixel(..) 效率不是很高,如果您计划在光线跟踪引擎中使用它,可能会出现问题。

您可能想使用 LockBits() 方法,请参阅此 答案这个了解更多详细信息。 另一种方法是使用“不安全”C# 代码访问数据,该代码允许您使用指向数据的指针。 有关详细信息,请参阅此问题,我通过使用“获得了~x2的加速”不安全”的代码。

It know it's not part of your original question, but using SetPixel(..) is not very efficient and could be a problem if you are planning to use it in a ray-tracing engine.

You might want to use the LockBits() method, see this answer and this one for more details. They other way to go is to access the data using "unsafe" C# code, which allows you to uses pointers to the data. See this question for more info, I got a ~x2 speed-up by using "unsafe" code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文