C
C
Clean Coder2019-03-29 12:11:48
C++ / C#
Clean Coder, 2019-03-29 12:11:48

Why does the program output -nan (ind)?

Wrote a function to rotate a straight line.

// Line - y = _k * x + _b
void Line::rotation(double angle) {
    double temp = _k;
    _k = (_k + tan(angle))/(1 - _k*tan(angle));
    _b *= _k/temp;
}

double Line::rotationAngle(const Line &l) { // counterclock-wise from l to this
    return atan((_k - l._k)/(1. + _k*l._k));
}

When entering the following data
Line Line1(0, 0), Line2(2, 1);

    cout << Line1;
    cout << Line2;

    Line1.rotation(Line2.rotationAngle(Line1));

    cout << Line1;

For some reason it gives the following
y = 0x + 0
y = 2x + 1
y = 2x + -nan(ind)

The program calculated the coefficient of the rotated straight line correctly (_k = 2), but for some reason then it cannot divide 2 by 2:
_b *= _k/temp;
Although if temp is replaced by 2, then everything is ok. Will issue: It seems that there are no roots and divisions by zero. What is the reason? y = 2x + 0

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ronald McDonald, 2019-03-29
@HustleCoder

It seems to be no roots and divisions by zero.

If the tangent is 0.5 with a factor of 2, then division by zero appears.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question