简体   繁体   中英

C++; Floating Point exception; Without ./ operator

The example below, is from OpenCv documentation[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; then why do we get floating point exception for the second case?

[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. 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 .

The . character is part of the 1. double literal. / is an arithmetic operator so the right hand side expression becomes:

1. / (i+j+1);

and the result is a value of type double. Omitting the . character makes it an integer literal of 1 and the expression becomes:

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. For readability reasons the statement should include spaces where appropriate:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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