本文介绍了如何在C中将负零转换为正零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在学习Objective C,并且正在做经典的Calculator示例.

Hello I'm learning Objective C and I was doing the classic Calculator example.

问题是当我将零乘以任何负数时,我得到一个负零,然后将结果放入(双精度)类型!

Problem is that I'm getting a negative zero when I multiply zero by any negative number, and I put the result into a (double) type!

要查看发生了什么,我玩了调试器,这就是我得到的:

To see what was going on, I played with the debugger and this is what I got:

(gdb)打印(两倍)-2 * 0
$ 2 = -0

(gdb) print (double) -2 * 0
$2 = -0

在第二种情况下,当我将其强制转换为双精度类型时,它变为负零!如何在应用程序中解决该问题?我需要打双打.如何修复结果,以便当结果应为零时得到零?

In the second case when I cast it to a double type, it turns into negative zero! How can I fix that in my application? I need to work with doubles.How can I fix the result so I get a zero when the result should be zero?

推荐答案

我做了一个简单的测试:

I did a simple test:

double d = (double) -2.0 * 0;

if (d < 0)
    printf("d is less than zero\n");
if (d == 0)
    printf("d is equal to zero\n");
if (d > 0)
    printf("d is greater than zero\n");

printf("d is: %lf\n", d);

它输出:

因此,要解决此问题,您可以在应用程序中添加简单的if-check:

So, to fix this, you can add a simple if-check to your application:

if (d == 0) d = 0;

这篇关于如何在C中将负零转换为正零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 02:41
查看更多