comparing float numbers

I'm having problem comparing floating point numbers. In the division math tutor program I created below, I'm not sure what to write to compare them correctly and where.


const float SMALL = 0.00001;

float num1, num2;

if (fabs(num1 - num2) < SMALL)

{

cout << "Equal" << endl;

}

else

{

cout << “Not equal” << endl;

}
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
Assignment 4b
//
//  ********************************************************************************
//  Description: This program provides a menu to choose a math problem.
//  It then provides two random numbers, asks you to add, subtract, divide, or multiply.
//  Once you have the answer, press enter and the program tells you whether you got it right or not.
//
//  Created by Christopher Harrod on 3/7/15.
//  Status: Complete
//
// ********************************************************************************
#include <iostream>
#include <cstdlib>
#include <math.h>

int main(int argc, const char * argv[])
{
    int n1, n2, n3, n4, n5, n6, n7, n8, a1, a2, a3, a4, a11, a22, a33, a44, choice;
    std::cout << "What kind of problem would you like?\n";
    std::cout << "Addition, press 1\n";
    std::cout << "Subtraction, press 2\n";
    std::cout << "Multiplication, press 3\n";
    std::cout << "Division, press 4\n";
    std::cin >> choice;
    while (choice>4)
    {
    std::cout << "Please enter a number between 1 and 4.\n";
    std::cin >> choice;
    }

    n1 = rand() % 100 + 1;
    n2 = rand() % 100 + 1;


    if(choice == 1)
    {std::cout << n1 << "+" << n2 << "=" << "\n\n";
    a1 = n1+n2;

    std::cout << "What is the answer?";
    std::cin >> a11;
    if(a1==a11)
    std::cout << "Congratulations, you got it right!";
    else
    std::cout << "Sorry, you got it wrong.";
    return 0;
    }
    else if(choice == 2)
    {
    std::cout << n1 << "-" << n2 << "=" << "\n\n";
    a2 = n1-n2;
    std::cout << "What is the answer?";
    std::cin >> a22;
    if(a2==a22)
    std::cout << "Congratulations, you got it right!";
    else
    std::cout << "Sorry, you got it wrong.";
    return 0;
    }

    else if(choice == 3)
    {
    std::cout << n1 << "x" << n2 << "=" << "\n\n";
    a3 = n1*n2;
    std::cout << "What is the answer?";
    std::cin >> a33;
    if(a3==a33)
    std::cout << "Congratulations, you got it right!";
    else
    std::cout << "Sorry, you got it wrong.";
    return 0;
    }
    if(choice == 4)
    {std::cout << n1 << "/" << n2 << "=" << "\n\n";
    a4 = n1/n2;
    std::cout << "What is the answer?";
    std::cin >> a4;
    if(a4==a44)
    std::cout << "Congratulations, you got it right!";
    else
    std::cout << "Sorry, you got it wrong.";
    return 0;
    }
}
closed account (E3h7X9L8)
didnt read the code but float variables use the same operators as integers you just need to declare de answer a float too else it doesnt compare the decimals
well what i get when i run the division the answer is sometimes a very long number like 0.487474837385676. I declared the division answer input and output as float but it didnt fix the problem. How do I make my division problem more clean and compare floating point numbers correctly? The division problem is the last bit of code.
Last edited on
is this how i should do the last part

if(choice == 4)
{std::cout << n1 << "/" << n2 << "=" << "\n\n";
a4 = n1/n2;
std::cout << "What is the answer?";
std::cin >> a4;
if (fabs(a4 - a44) < small)
{std::cout << "Congratulations, you got it right!";
}
else
{std::cout << "Sorry, you got it wrong.";
return 0;
}
}
anybody? if the answer is .61789459 and i put .617. it says you got the wrong answer even if i have my variable small as .01
On my machine..
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;

int main()
{
	if ((.61789459f - .617f) < 0.1)
		cout << "equal" << endl;
	else
		cout << "not equal" << endl;

	system("pause");
	return 0;
}


equal
Last edited on
im sorry i dont get the answer and where i would put that in context to my code. also, they are random numbers, so i cant use the numbers i gave. that was just an example of the output.
Small change to the fourth part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if (choice == 4)
	{
		std::cout << n1 << "/" << n2 << "=" << "\n\n";
		a44 = n1 / n2;
		std::cout << "What is the answer?";
		std::cin >> a4;
		if (abs(a44 - a4) < 0.1)
			std::cout << "Congratulations, you got it right!";
		else
			std::cout << "Sorry, you got it wrong.";

		system("pause");
		return 0;
	}


What kind of problem would you like?
Addition, press 1
Subtraction, press 2
Multiplication, press 3
Division, press 4
4
42/68=

What is the answer?0.617
Congratulations, you got it right!Press any key to continue . . .


I think it works fine.
Also, seed the random number with time.
thank you, that worked. i still don't know how exactly it works but thank you.
Topic archived. No new replies allowed.