Click here to Skip to main content
15,891,184 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Algorithm Request Pin
Francesco Aruta28-Jun-06 4:46
Francesco Aruta28-Jun-06 4:46 
AnswerRe: Algorithm Request [modified] Pin
Viorel.28-Jun-06 4:27
Viorel.28-Jun-06 4:27 
GeneralRe: Algorithm Request Pin
Sarath C28-Jun-06 4:42
Sarath C28-Jun-06 4:42 
GeneralRe: Algorithm Request Pin
Viorel.28-Jun-06 4:49
Viorel.28-Jun-06 4:49 
AnswerRe: Algorithm Request Pin
kylur28-Jun-06 4:28
kylur28-Jun-06 4:28 
GeneralRe: Algorithm Request [modified] Pin
Tnarol28-Jun-06 5:20
Tnarol28-Jun-06 5:20 
AnswerRe: Algorithm Request Pin
Chris Losinger28-Jun-06 5:41
professionalChris Losinger28-Jun-06 5:41 
AnswerRe: Algorithm Request [modified] Pin
Zac Howland28-Jun-06 6:18
Zac Howland28-Jun-06 6:18 
String operations are your best bet for the most accuracy and flexibility (easy to control the number of significant figures). Since you don't want to go that route, this might get you started:

To count left side of decimal
<br />
unsigned long countLeftOfDecimal(double x)<br />
{<br />
	if (1 > abs(x))<br />
  	{<br />
     		return 0; // x is between -1 and 1<br />
	}<br />
	else if (1 == abs(x))<br />
	{<br />
		return 1; // x is either -1 or 1<br />
	}<br />
	else<br />
	{<br />
		return ceil(log10(abs(x)));<br />
	}<br />
}<br />
<br />
unsigned long countRightOfDecimal(double x)<br />
{<br />
	long y = x;	// truncate the decimal<br />
	double x1 = x - (double)y;	// keep only the decimal<br />
	long count = 0;<br />
	while (0.0 != x1)<br />
	{<br />
		double x2 = x1 * 10.0;	// move the decimal 1 place<br />
		y = x2;			// truncate the decimal<br />
		x1 = x2 - y;		// keep only the decimal<br />
		++count;<br />
	}<br />
	return count;<br />
}<br />


Keep in mind that you will run into problems when you have numbers like 1.99999999999... with this algorithm -- which is why converting to a string for counting significant figures is a better solution, and may actually be faster than any non-string solution you come up with that doesn't have problems with infinite repeating decimals.

If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week

Zac

-- modified at 15:16 Wednesday 28th June, 2006
AnswerRe: Algorithm Request Pin
Rilhas9-Jul-06 6:25
Rilhas9-Jul-06 6:25 
QuestionFileMapping problem Pin
Kharfax28-Jun-06 3:51
Kharfax28-Jun-06 3:51 
AnswerRe: FileMapping problem Pin
Matt Godbolt28-Jun-06 4:07
Matt Godbolt28-Jun-06 4:07 
GeneralRe: FileMapping problem Pin
Kharfax28-Jun-06 4:38
Kharfax28-Jun-06 4:38 
AnswerRe: FileMapping problem Pin
Jun Du28-Jun-06 7:12
Jun Du28-Jun-06 7:12 
GeneralRe: FileMapping problem Pin
Kharfax28-Jun-06 7:37
Kharfax28-Jun-06 7:37 
GeneralRe: FileMapping problem Pin
Kharfax28-Jun-06 8:41
Kharfax28-Jun-06 8:41 
QuestionProblem found using SHBrowseForFolder Pin
ajayraj jayaraj28-Jun-06 3:42
ajayraj jayaraj28-Jun-06 3:42 
AnswerRe: Problem found using SHBrowseForFolder Pin
Sarath C28-Jun-06 4:09
Sarath C28-Jun-06 4:09 
QuestionDirectShow Universal Source Filter Pin
shadow0128-Jun-06 3:17
shadow0128-Jun-06 3:17 
AnswerRe: DirectShow Universal Source Filter [modified] Pin
Justin Tay28-Jun-06 3:22
Justin Tay28-Jun-06 3:22 
GeneralRe: DirectShow Universal Source Filter Pin
shadow0128-Jun-06 4:02
shadow0128-Jun-06 4:02 
QuestionFree Design Patterns book Pin
knoxplusplus28-Jun-06 3:15
knoxplusplus28-Jun-06 3:15 
AnswerRe: Free Design Patterns book Pin
Cedric Moonen28-Jun-06 3:21
Cedric Moonen28-Jun-06 3:21 
GeneralRe: Free Design Patterns book Pin
knoxplusplus28-Jun-06 3:36
knoxplusplus28-Jun-06 3:36 
Questiontypename/class [modified] Pin
knoxplusplus28-Jun-06 3:11
knoxplusplus28-Jun-06 3:11 
AnswerRe: typename/class Pin
_AnsHUMAN_ 28-Jun-06 3:18
_AnsHUMAN_ 28-Jun-06 3:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.