Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C++ Problem

Name: Anonymous 2010-11-01 18:24

This is my C++ program why does the sqrt() function not work correctly
#include <iostream>
#include <cmath>
using namespace std;

int main ()
{
    int a;
    cout << "Input a";
    cin >> a;
    int b;
    cout << "Input b";
    cin >> b;
    int c;
    cout << "Input c";
    cin >> c;
    int n;
    n=(-4.00*a*c);
    int q;
    q=sqrt(n);
    int d;
    d=q+b*b;
    int r;
    r=(-1.00+d)/(2.00*a);
    int s;
    s=(-1.00-d)/(2.00*a);
    if (r>0.00)
    cout << "(x+" << r <<") ";
    else if (r<0.00)
    cout << "(x" << r <<") ";
    if (s>0.00)
        cout << "(x+" << s <<") ";
    else if (r<0.00)
        cout << "(x" << s <<") ";
}

Name: Anonymous 2010-11-01 18:31

What do you mean? Your program runs perfect for me.

Name: Anonymous 2010-11-01 18:32

Probably because you used int to hold it instead of double.

Name: Anonymous 2010-11-01 18:33

Oh. It must be terminal that is messed up

Name: Anonymous 2010-11-01 18:34

>>3
YHBT

Name: Anonymous 2010-11-01 20:35

float InvInvSqrt(float x)
{
    union {
        float f;
        int i;
    } tmp;
    tmp.f = x;
    tmp.i = 0x5f3759df - (tmp.i >> 1);
    float y = tmp.f;
    return (1/(y * (1.5f - 0.5f * x * y * y));
}

NOW YOU'RE THINKING WITH QUAKE

Name: Anonymous 2010-11-01 22:55

Probably because you're picking positive values for a and c

Name: Anonymous 2010-11-01 23:09

>>1
sqrt works best when code is wrapped in code tags.

Name: Anonymous 2010-11-02 0:07

>>6
Unions, fucking magic.

Name: Anonymous 2010-11-02 6:21

>>9
They're like unicorns but without the c and r.

Name: Anonymous 2010-11-02 7:23

>>10
They're like onions with a s/u/o/i, but magic.

Don't change these.
Name: Email:
Entire Thread Thread List