[Solved] Condition for Precision Numbers

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

[Solved] Condition for Precision Numbers

Post by zodiac976 »

I ran into a problem in my code and I am wonder if anyone can help me
figure out how to set up a condition to check to see if the user entered
a precision number and prompt them that the entry is invalid if the
precision number is greater than 2 decimal places.

I am having a hard time figuring this annoying problem out, of course
most problems I run into end up being incredibly simple :lol:.
Last edited by zodiac976 on Wed Jun 24, 2009 11:18 am, edited 1 time in total.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Condition for Precision Numbers

Post by avansc »

mmmm

if you have the string.
trim all the leading and trailing zeros. the trailing ones are the important part.
reverse the string.
then find the fist occurrence of the character '.'

donate x to be the place there '.' was found. if x > n (n being the precision you want), then you can say that the string is precise.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Condition for Precision Numbers

Post by MarauderIIC »

Have the user input into a [c++] string, find the first occurrence of "." (myString.find(....)) . Subtract the location of that from the length of the string (myString.length()). If >= 2, it's okay.

I would think that if they entered trailing zeroes, then it would count towards precision. For instance, "2.00" is more precise than "2". Therefore you wouldn't want to trim them.

Of course, since you're handling strings, you now have to find some way to make sure that it's a valid float. Looks like atof() will work for that. If you're using C++ strings, you'll have to do atof(myString.c_str()).
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: Condition for Precision Numbers

Post by zodiac976 »

I am still struggling with this at the moment but I am getting closer
to finishing it. This is a lot harder than I thought but I am using
strings. My problem now is reversing the string into a new string
then converting it to an int to truncate it then check. The reverse
iterator isn't assigning the contents of the first string into the new
string. :cry:
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: Condition for Precision Numbers

Post by zodiac976 »

I believe this code below works for checking if the user
entered more than 2 decimal places, like if your program
uses money which is always 2 decimal places.

Anyways I tested this code as much as possible and it took
me quite a while to figure out...almost pulled my hair out :lol:.

I can't say that it will work perfect but I tested it and it seems
to be outputting me the right answer every time.

I hope this helps anyone and if anyone wants to find any
bugs in it or something bad about it tell me :).

Code: Select all

/*This little bit of code is for checking user input for
decimals for anything related to money which evaluates to
2 decimal places*/

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
	double userInput, checkInput;
	string newInput;

	cout << "Enter a precision number: ";
	cin >> userInput;
	cin.ignore();

	checkInput = userInput - int(userInput); //Subtracts leaving only the decimal numbers

	stringstream output;
	output << checkInput;
	newInput = output.str();   //Converts newInput into string

	//If newInput is for instance 1.0, 1.00, etc, it is valid
	if(int(newInput.length()) == 1 || int(newInput.length()) == 2)
		cout << "Number is valid\n";
	else if(int(newInput.length()) == 3)
		cout << "Number is 1 decimal place\n";
	else if(int(newInput.length()) == 4)
		cout << "Number is 2 decimal places\n";
	else
		cout << "Invalid entry...number is more than 2 decimals\n";

	return 0;
}
Sorry forgot to say thanks to both of you ;).
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Condition for Precision Numbers

Post by avansc »

MarauderIIC wrote:I would think that if they entered trailing zeroes, then it would count towards precision. For instance, "2.00" is more precise than "2". Therefore you wouldn't want to trim them.
since when is 2.00 more precise than 2.

is 2.02 less precise than 2.0200000000000000000000000000000000

or for that matter, which one is more precise 0002.0 or 2.0000

so x+1 != x+1.00000

mmmmmmm...... might wanna rethink all that.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Innerscope
Chaos Rift Junior
Chaos Rift Junior
Posts: 200
Joined: Mon May 04, 2009 5:15 pm
Current Project: Gridbug
Favorite Gaming Platforms: NES, SNES
Programming Language of Choice: Obj-C, C++
Location: Emeryville, CA
Contact:

Re: Condition for Precision Numbers

Post by Innerscope »

since when is 2.00 more precise than 2.

is 2.02 less precise than 2.0200000000000000000000000000000000

or for that matter, which one is more precise 0002.0 or 2.0000

so x+1 != x+1.00000
You should consider that precision is not the same as equivalence, I think Marauder's right on this one.
Current Project: Gridbug
Website (under construction) : http://www.timcool.me
User avatar
zodiac976
Chaos Rift Regular
Chaos Rift Regular
Posts: 156
Joined: Thu Jun 18, 2009 10:03 am
Current Project: Booklet & Text RPG
Favorite Gaming Platforms: PC, PS3, PSP
Programming Language of Choice: C++
Location: AL
Contact:

Re: Condition for Precision Numbers

Post by zodiac976 »

I think the more decimal places the more precise.
User avatar
Bakkon
Chaos Rift Junior
Chaos Rift Junior
Posts: 384
Joined: Wed May 20, 2009 2:38 pm
Programming Language of Choice: C++
Location: Indiana

Re: Condition for Precision Numbers

Post by Bakkon »

avansc wrote:since when is 2.00 more precise than 2.
Always.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Condition for Precision Numbers

Post by avansc »

so hang on. lets assume that i said that you had to measure within 3 decimal places precision.
would it be wrong if i said its 2m instead of 2.000m

precision only comes into play when 10^-1 + 10^-2 + ... + 10^-n numbers are not zero scanning from the back.

ie.

1.11110000
is the same precision as
1.1111

those trailing 0's mean nothing.

precision is the quality of a quanta being reproduced.
ie.
i can reproduce the number 1
with 1, 1.0, 1.00 and so on, and every time the quality of the quanta is infinitely precise. so 1 is PRECISELY the PRECISION of 1.000000000000000000000000000000000000000*

edit: but i will say for his application he would need the 0's as he had defined it.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Condition for Precision Numbers

Post by MarauderIIC »

would it be wrong if i said its 2m instead of 2.000m
Scientifically, yes. 2 means that it can be 2.1 or 2.2 or 2.3 or 2.001.

2.000 means it cannot be 2.1 or 2.01 or 2.001 but it can be 2.0001. It has to do with the precision of your measuring tool.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Condition for Precision Numbers

Post by avansc »

yes, but not if you now that there are just 0's trailing as
they dont mean anything.
anyways, it does not really matter.

accuracy and precision are also not the same thing, although they are closely related.

actually interesting. they have these places. and im not sure what they are called in the states. but its like a board or associations of standards.
and whenever you wanna make lets say a measuring tool you go to them to calibrate you micrometer or whatever. they have like there rooms that are kept
at the right temp and humidity and they have metal rods that are i guess known to be exactly certiain lengths that you can calibrate your tools with.

just thought that was interesting, thought in this day and age we would have had an iPhone app or website that could do that for us. lolz
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Post Reply