assert function

I am trying to write a function that calculates the sqrt of a float. I have to test two cases, when x = 3 and when x = -3.

So obviously I want to see what happens when x = -3. I want to abort the function by using assert();

My code isn't working. I know that I initialized userInput = 0 and then made the condition for assert ( userInput > 0 ) which doesn't make sense.

But then if I don't initialize userInput, the program will not work.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  #include <iostream>
#include <stdio.h>
#include <math.h>
#include <assert.h>

using namespace std;

float squareRoot(float x){

	float userInput = 0, Result;
	assert(userInput > 0);
	Result = sqrt(userInput);
	printf("sqrt(%f) = %f\n", userInput, Result);

	return 0;

}

int main()
{
	
	float x = 3;
	float x2 = -3;

	squareRoot(x);
	squareRoot(x2);

	system("Pause");
	return 0;

}
closed account (48T7M4Gy)
For a start you need to set userInput to x. eg float userInput = x; at line 10
Get rid of userInput.

The input comes from main - x and x2. So in the squareRoot function, just use x instead of userInput (which you have now deleted lol)
closed account (48T7M4Gy)
oops again, that was pretty obvious
I fixed the code but I still get this error: http://i.imgur.com/JWHh4pl.png

Is it because of the assert function or there is something wrong with my code?

code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <assert.h>

using namespace std;

float squareRoot(float x){

	float Result;
	assert(x > 0);
	Result = sqrt(x);
	printf("sqrt(%f) = %f\n", x, Result);

	return 0;

}

int main()
{
	
	float x = 3;
	float x2 = -3;

	squareRoot(x);
	squareRoot(x2);

	system("Pause");
	return 0;

}
> Is it because of the assert function

It is because of the assert macro. The output has:
Assertion failed, x > 0, file ... line 11

Note: should be x >= 0
Note: std::sqrt() returns a special error value (usually nan) in case of a domain error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <cmath>
// #include <cassert>
// #include <stdexcept>

float sqrt( float x )
{
    const float result = std::sqrt(x) ;

    // assert( x >= 0 ) ;
    // if( x < 0 ) throw std::domain_error( "sqrt of negative number!" ) ;
    if( std::isnan(result) ) std::cerr << "result is not a number!\n" ;

    return result ;
}

int main()
{
    const float a = sqrt(3.0f);
	std::cout << a << '\n' ;

	const float b = sqrt( -3.0f );
	std::cout << b << '\n' ;
}

http://coliru.stacked-crooked.com/a/5b06ef995582bd99
So you're saying that my code has an error or it's ok?
Anyhow, I have to use assert function in my code, this is some sort of school assignment and I have to demonstrate how I used it.
Last edited on
Change assert(x > 0); to assert(x >= 0); and your code would be ok.
Okay. Thanks!

Is this ok now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <assert.h>

using namespace std;

float squareRoot(float x){

	assert(x >= 0);
	float Result;
	Result = sqrt(x);
	printf("sqrt(%f) = %f\n", x, Result);

	return 0;

}

int main()
{
	
	float x = 3;
	float x2 = -3;

	squareRoot(x);
	squareRoot(x2);

	system("Pause");
	return 0;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//#include <iostream>
#include <cstdio> // <stdio.h>
#include <cmath> // <math.h>
#include <cassert> // <assert.h>

// using namespace std; // avoid

// either something like 'void print_square_root(float x)'
// or return the square root from the function and print it in main() 
float squareRoot(float x){

	assert(x >= 0);
	// float Result;
	float Result = std::sqrt(x);
	//printf("sqrt(%f) = %f\n", x, Result);

	// return 0;
        return Result;
}

int main()
{

	float x = 3;
	float x2 = -3;

	float rx = squareRoot(x);
	std::printf("sqrt(%f) = %f\n", x, rx );

	float rx2 = squareRoot(x2);
	std::printf("sqrt(%f) = %f\n", x2, rx2 );

	// system("Pause"); // avoid
	// return 0; // not really required
}
Holy shit, now I realized that all I program is absolute crap xD

Thanks man. You really helped me with this.
Topic archived. No new replies allowed.