简体   繁体   English

C ++计算评估为0

[英]C++ calculation evaluated to 0

I'm parsing a file and trying to decode coordinates to the right unit. 我正在解析文件,并尝试将坐标解码为正确的单位。 What happens is that this code is evaluated to 0. If I type it into gdb the result is correct. 发生的是该代码的求值为0。如果我将其键入gdb,则结果正确。

int pLat = (int)(
        (argv[6].data() == "plus" ? 1 : -1)
        * (     atoi(argv[7].data()) 
              + atoi(argv[8].data()) / 60. 
              + atoi(argv[9].data()) / 36000.)
        * 2.145767 * 0.0001);

I'm doing a (degrees, minutes, tenth seconds) conversion to wgs. 我正在将(度,分,十分秒)转换为wgs。 Is there something wrong with this code? 这段代码有什么问题吗?

You are casting it to int, which means you are only taking the integral part. 您将其强制转换为int,这意味着您仅参与了整体部分。

(int)0.7 == 0 . (int)0.7 == 0 If the expression you cast to an int is < 1, the result will be 0 due to the cast. 如果强制转换为int的表达式<1,则由于强制转换,结果将为0。 Since 2.145767 * 0.0001 is a very small number, the chances of this happening are pretty high. 由于2.145767 * 0.0001是一个非常小的数字,因此发生这种情况的可能性非常高。

Consider using floats or doubles exclusively. 考虑专门使用浮点数或双精度数。

In general: 一般来说:

(int)xy == x for all floats and doubles xy . (int)xy == x对于所有浮点数和xy两倍。 (ignoring possible overflow) (忽略可能的溢出)

Assuming you're trying to convert degrees to WGS84 coordinates, there are two errors: 假设您尝试将度数转换为WGS84坐标,则有两个错误:

  • The conversion factor is out by a factor of 10 (180/2 23 is approximately 2.145767*10 -5 , and you have 2.145767*10 -4 ) 转换系数超出了10倍(180/2 23约为2.145767 * 10 -5 ,而您拥有2.145767 * 10 -4
  • You are multiplying by the conversion factor when you should be dividing by it. 您应将其除以转换因子。 This will give you a very small number, and the cast to int will give zero. 这将为您提供一个非常小的数字,而对int将为零。

仅为argv [6..9]插入随机值似乎会产生小于1的数字。当转换为int ,这些值将被截断为0。如果转换因子正确,则可能需要使用浮点数类型代表结果。

I'll assume for now that you have defined 我现在假设您已经定义了

vector<string> argv;

and initialized it somehow. 并以某种方式对其进行了初始化。

string::data doesn't do what you seem to want. string::data不能满足您的需求。 Actually, it does almost nothing. 实际上,它几乎什么都不做。 It just returns a const pointer to a version of the string, which isn't guaranteed to be null-terminated. 它只是返回一个指向字符串版本的const指针, 不能保证该字符串以null结尾。

To compare a string to a character literal, just use == . 要将string与字符文字进行比较,只需使用==

To pass a string to a C function, use string::c_str . 要将string传递给C函数,请使用string::c_str

And, you are putting a fractional number into an int . 并且,您要将小数放入int

double pLat = ( // use a floating-point type
        (argv[6] == "plus" ? 1 : -1) // compare string using ==
        * (     atoi(argv[7].c_str()) // get C string using c_str
              + atoi(argv[8].c_str()) / 60. 
              + atoi(argv[9].c_str()) / 36000.)
        * 2.145767 * 0.0001);

You are converting to int in the end which will cap all values to the smaller(!) int value. 最后,您将转换为int,这会将所有值限制为更小(!)int值。 C does not round in this case. C在这种情况下不舍入。

[Edit: Sorry, my previous version of this answer contained a wrong part.] [编辑:对不起,此答案的先前版本包含错误的部分。]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM