Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Decimal precision
Message
From
23/05/2012 12:06:49
 
 
To
23/05/2012 11:43:25
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01544101
Message ID:
01544157
Views:
37
This message has been marked as a message which has helped to the initial question of the thread.
>>I'ts not a loop, it just ends without finding a solution because a total tax of $500.00 does not exist
>>
>>What happens is that fedTax + provTax is 499.99 at one moment and the next iteration it is 500.01
>>
>>So I have added an extra if: fedTax + provTax > Totaltax, then adjust provTax to be totalTax - fedtax so that the sum of both of them is 500.00
>>
>>
>>
>>	totalTax  = 500.00
>>	f = .05
>>	p = .095
>>	
>>	base =  round(totalTax  / (f*p + f + p), 2)
>>
>>	
>>	for i = base  - .50 to base + 0.50 step .01
>>		fedTax = round(i * f, 2)
>>		provTax = round((i + fedTax) * p, 2)
>>		
>>		if ( fedTax + provTax == totalTax  )
>>		
>>			? 'Base ' , i
>>			? 'fedtax', fedTax 
>>			? 'provTax ', provTax 
>>			exit
>>		endif
>>		
>>		if( fedTax + provTax > totalTax  )
>>			provTax = totalTax  - fedtax
>>			? 'Base ' , i
>>			? 'fedtax', fedTax 
>>			? 'provTax ', provTax 
>>			
>>			exit
>>		endif
>>		
>>	endfor
>>
>
>The problem I had initially, and still have it with the IF addition, is that if I enter something else than 74.88 for the value, the program falls into an ongoing process. I have to force a brute force close of the application.


Try this. It finds out which direction to take and exits as soon as there is a sign reversal
		static void Main(string[] args)
		{
			double totalTax, fedTax, provTax;


			totalTax = 500.00;

			if (TrySolveTaxes(totalTax, out fedTax, out provTax))
				Console.WriteLine("TotalTax= {0} fedTax= {1} provTax= {2}", totalTax, fedTax, provTax);
			else
				Console.WriteLine("cannot solve");


			Console.ReadLine();

		}
		public static bool TrySolveTaxes(double totalTax, out double fedTax, out double provTax)
		{
			bool success = false;

			double fedPortion = .05;
			double provPortion = .095;

			// first shot
			double amount = Math.Round(totalTax / (fedPortion * provPortion + fedPortion + provPortion), 2);
			fedTax = Math.Round(amount  * fedPortion, 2) ;
			provTax = Math.Round((amount  + fedTax) * provPortion, 2) ;


			int deltaSign = Math.Sign(totalTax - (fedTax + provTax));

			if (deltaSign == 0)
				success = true;
			else
			{
				double direction = .01 * deltaSign;
				int oldDeltaSign = deltaSign;

				do
				{
					amount += direction;
					fedTax = Math.Round(amount * fedPortion, 2);
					provTax = Math.Round((amount + fedTax) * provPortion, 2);

					deltaSign = Math.Sign(totalTax - (fedTax + provTax));
					provTax = totalTax - fedTax;

				} while (deltaSign == oldDeltaSign);
				success = true;
				
			}

			return success;
		}
Gregory
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform