Public Group

# float question.

This topic is 3962 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

In my book I have an assignment that told me to put exact numbers in the user-input areas, and repeat until you put the exact one...but I was wondering how I would go about setting a range of x - y (ex. 1.00-4.00). This is the regular assignment part that it asked for, but how would I change it to allow user input of a range 1.00 - 4.00?
void GPA()
{
float GPA;
bool correct;

std::cout << std::endl << std::endl;

do
{
std::cout << "Enter your GPA: ";
std::cin >> GPA;

correct = GPA == (float) 3.82;  //change to range of 1.00-4.00
}

while(!correct);

}



##### Share on other sites
Your question is a bit confusing. Do you mean that you want it to only accept input between 1.0 and 4.0 and if the input is outside this range it spits out an error and asks for your GPA again?

Just check to see if GPA is greater than or equal to the lower bound, and lesser than or equal to the upper bound before executing the line that can set the variable correct to true.

##### Share on other sites
If I understand your question correctly, all you need to do is change one that line:
correct = (GPA >= 1) && (GPA <= 4);

##### Share on other sites
Quote:
 Original post by zell901In my book I have an assignment that told me to put exact numbers in the user-input areas, and repeat until you put the exact one...but I was wondering how I would go about setting a range of x - y (ex. 1.00-4.00). This is the regular assignment part that it asked for, but how would I change it to allow user input of a range 1.00 - 4.00?*** Source Snippet Removed ***

With that code, the correct will always be false unless the user inputs exactly 3.82.

You might want to do a series of ifs
float GPA = 0f; // Initializing because... I felt like it :)bool correct = false; // I just like initializing my bools :Ddo{    cout << "Enter your GPA: ";    cin >> GPA;    if (GPA > 4f || GPA < 1f)    {        correct = false;    }    else    {        correct = true;        doSomeOtherCoolStuff();    }    while (!correct);

However, I do hope I understood your question.

EDIT: Woops, too slow. :)

##### Share on other sites
Quote:
 Original post by MJPasking homework-related questions isn't allowed here.

If you've read the original post, the original assignment was to check for a set value (3.82), which he completed. He's looking to further his knowledge beyond the set assignment, which seems admirable to me.

##### Share on other sites
Quote:
Original post by Kippesoep
Quote:
 Original post by MJPasking homework-related questions isn't allowed here.

If you've read the original post, the original assignment was to check for a set value (3.82), which he completed. He's looking to further his knowledge beyond the set assignment, which seems admirable to me.

You're right, I didn't see that. Sorry zell901.

##### Share on other sites
Thank you, sorry my question was kind of vague, but the replies helped me figure it out.

##### Share on other sites
Going a little off topic here, comparing floats (or even doubles) for equality is A Bad Thing, since they have a finite range (They're only 32-bits which means around 4 billion unique possible values). It's a bit like saying "If x == 1/3rd" when you're dealing with decimal numbers; you can't represent 1/3rd exactly in base 10, so you'd end up with "if x == 0.33333", which isn't 1/3rd.
If you need to check for equality, you should really be checking if the value is very close to what you expect.
correct = GPA == (float) 3.82;
I'd write:
correct = (GPA >= 3.81999f) && (GPA <= 3.82001f);
(Note that postfixing 'f' to a constant makes it a float rather than a double).

This might not be a problem for some numbers, but will for others. Googling for "Floating point epsilon" gives some useful results like This which might be worth reading.

##### Share on other sites
Elaborating on Steve's post -- there's a reason you might care if you are comparing doubles to floats. Recall that a float is (generally) 32bits. This allows us finite single precision floating point values. Doubles on the other hand, are often 64bits -- hence, allow a much greater variance in the values you can represent.

Really high precision math probably wants doubles -- which might let you call them 'long' doubles -- which will do different things depending on your compiler. It might just ensure that you get your full 64bits, or might even not do anything at all (confusing :S) -- for Visual C++ 2005, long double is identical to regular double :)

Luckily, we often want to avoid lots of precision (because it makes comparing stuff tedious and eats up our storage). Games are all about making people think stuff is infinite while working with a really limited finite amount of resources -- hence, using floats is usually fine :)

~Shiny

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5
A4L
11

• 12
• 16
• 26
• 10
• 44
• ### Forum Statistics

• Total Topics
633768
• Total Posts
3013743
×