Jump to content

Counting Decimal Places (C++)


ARandomOWL

Recommended Posts

So in a program I am making I need to count the ammount of DPs in a float. I cannot use standard libraries, etc. So I came up with the following code:

 

float a = 0.1415;	   // Any number
int count = 1;		  // DP count

while(a / (int)a != 1)  // Loops until a / (int) a == 1 (ie. a has no DPs)
{
 a *= 10;			  // *a by 10
 count++;			  // Add 1 to count
}					   // End of loop
printf("%i", count);	// Prints count

 

The problem is that it hangs somewhere in the loop (no way of telling where). Anyone see any wrong with it? Or have another idea of how to count DPs (keep in mind I have no libraries to work with). Thanks for reading :)

 

Edit: Even when I change "While()" to something simple like: "While (a < 1.0)" it still hangs :(

 

Edit2: Seems that no matter what I put in the while statement except "while(1)" causes a crash

Edited by AJW256

Share this post


Link to post
Share on other sites

umm, (int)0.1415 = 0, no? wouldnt you get a divbyzero on that?

 

try changing the loop to while (a - (int)a > 0). Its equivalent logic.

Also, dont you have a debugger that you can step through the code with?

 

-Shannon

Edited by viperk1

Share this post


Link to post
Share on other sites

It is "a / (int) a" that is the problem. When ever I have this in my code it hangs. Even when a is above 1. Maybe something to do with mixing ints & floats?

 

Edit:

// when a = 1.1415:
printf("%i\n", (int)a);
//shows "1" (as it should). Whereas:
printf("%i\n", a/(int)a);
// causes hang

Edited by AJW256

Share this post


Link to post
Share on other sites

Idk, fired it up using java (bc I have the JDK installed and ready to go) and it complains heavily about the use of a float for 0.1415, swapped it out for a double and life was good, birds sang, the prog compiled and it ran happily.

 

		double a = 0.1415;	   
	int count = 1;		  

	while(a / (int)a != 1)  
	{
	  a *= 10;			  
	  count++;			  
	} 

	System.out.println(count);

Share this post


Link to post
Share on other sites

You're quite likely to suffer from rounding errors trying to do it that way. Try 1.00001 - 1.0 for instance. The result won't be 0.00001 as you would expect. It'll be 1.00000000000655e-05 = 0.0000100000000000655.

 

Using that algorithm with 10.0123456789 gives an answer of 15 here because of this.

 

I think the best way would probably be to convert the double to a string, find the decimal point and get the length from the first digit after this point (index of point + 1) up until the end of the string.

 

digits.jpg

w799.png

Share this post


Link to post
Share on other sites

Thanks Markie, the problem with ur method is that it requires me to convert the number to a string. I need to be able to count the DPs to be able to convert the double to a string. Unless there is another way of converting double to string.

 

So basically I need some code to convert a double to string, this is my original problem. Hope that made sense.

Share this post


Link to post
Share on other sites

Thanks Markie, the problem with ur method is that it requires me to convert the number to a string. I need to be able to count the DPs to be able to convert the double to a string. Unless there is another way of converting double to string.

 

So basically I need some code to convert a double to string, this is my original problem. Hope that made sense.

Just go find out what the standard string libraries use and copy that code.

Share this post


Link to post
Share on other sites

you can use sprintf to convert a number to a string, and it is part of stdio

 

without string functions, I'd probably evaluate the sizeof the resulting string, and step through the digits (characters) right-to-left until the digit (character) was something other than zero

 

or something... I can't remember if sprintf would include the trailing zeros or not...

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...