简体   繁体   English

C ++; 浮点异常; 没有./运算符

[英]C++; Floating Point exception; Without ./ operator

The example below, is from OpenCv documentation[1]. 下面的示例来自OpenCv文档[1]。

Mat H(100, 100, CV_64F);
for(int i = 0; i < H.rows; i++)
    for(int j = 0; j < H.cols; j++)
        H.at<double>(i,j)=1./(i+j);

This works perfectly fine. 这工作得很好。 But in the last line what is ./ operator? 但是最后一行是./运算符? And if I replace it with / it gives me floating point exception. 如果我用/替换它,则会给我浮点异常。

So, in both cases we have infinity when i and j are 0; 因此,在两种情况下,当i和j为0时,我们都是无穷大。 then why do we get floating point exception for the second case? 那为什么第二种情况我们会得到浮点异常呢?

[1] http://docs.opencv.org/trunk/d3/d63/classcv_1_1Mat.html [1] http://docs.opencv.org/trunk/d3/d63/classcv_1_1Mat.html

./ is not an operator. ./不是运算符。 The dot binds with the 1 , making it a double constant. 点与1绑定,使其成为双常量。 It's equivalent to this: 等效于:

1.0 / (i+j+1)

Only a bit shorter. 只是短一点。

When you omit the dot, the expression is evaluated using integer arithmetic, giving all zero for all entries but 0, 0 . 省略点时,将使用整数算术对表达式求值,除0, 0所有条目的全零。

The . . character is part of the 1. double literal. 字符是1. double文字的一部分。 / is an arithmetic operator so the right hand side expression becomes: /是算术运算符,因此右侧表达式变为:

1. / (i+j+1);

and the result is a value of type double. 结果是double类型的值。 Omitting the . 省略. character makes it an integer literal of 1 and the expression becomes: 字符使得文字的整数1和表达式变为:

1 / (i+j+1);

where both operands are integer values and the result is an integer value. 其中两个操作数都是整数值,结果是整数值。 Spaces in C++ code make no difference to compiler. C ++代码中的空格对编译器没有影响。 For readability reasons the statement should include spaces where appropriate: 出于可读性原因,该语句应在适当的地方包含空格:

H.at<double>(i,j) = 1. / (i+j+1);

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

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