Getting Mathematical Errors Trying to calculate base price of gas


Getting Mathematical Errors Trying to calculate base price of gas



I am trying to solve a problem that requires me to write code to find the base price of a gallon of gas. I am provided with the cost of a gallon of gas, the amount of federal and state tax, and the percent for sales tax. I've tried formatting the equation a couple different ways and the math is always wrong. This is the closest I've gotten (the output is $3.289 which is only .073 cents off, which when I do the calculations by hand happens to be the amount of sales tax so it is as if it is ignoring the end of the equation completely). Could I please get some assistance? I've included the entire problem I was given below.


double pumpPrice=3.89, fedTax=.184, stateTax=.417, salesTax=2.25;
double basePrice, totalTax, percentTax, profit_bigOil ;

basePrice = pumpPrice-fedTax-stateTax-((salesTax*basePrice)/100) ;

cout <<"The base price for a gallon of gas is $" <<basePrice << endl;



Create a program, i.e. a Netbeans project with the following inputs



0) Take your pump price, whatever you paid at the pump along with taxes and calculate percentage that you pay in taxes.


Pump Price = Base Price + Fed Tax + State Tax + Base Price * Sales Tax



1) Oil Company Profit -> https://www.forbes.com/2011/05/10/oil-company-earnings.html#6c3f9a9f2dc8


7cents/gallon to 6.5%/gallon



2) Taxes -> http://www.latimes.com/politics/essential/la-pol-ca-essential-politics-updates-california-s-increased-gas-tax-goes-into-1509552219-htmlstory.html


Fed Tax -> 18.4 cents/gallon State Tax -> 41.7 cents/gallon State Sales Tax -> 2.25%



Calculate the following



1) The base price for a gallon of gas = $



2) The total tax on a gallon of gas = $



3) Percentage price due to gas tax = %



4) Oil Company profit range = %





Create a program, i.e. a Netbeans project with the following inputs 0) Take your pump price, whatever you paid at the pump along with taxes and calculate percentage that you pay in taxes. Pump Price = Base Price + Fed Tax + State Tax + Base Price * Sales Tax 1) Oil Company Profit -> forbes.com/2011/05/10/oil-company-earnings.html#6c3f9a9f2dc8 7cents/gallon to 6.5%/gallon
– Alli Vanzant
Jul 2 at 4:12






2) Taxes -> latimes.com/politics/essential/… Fed Tax -> 18.4 cents/gallon State Tax -> 41.7 cents/gallon State Sales Tax -> 2.25% Calculate the following 1) The base price for a gallon of gas = $ 2) The total tax on a gallon of gas = $ 3) Percentage price due to gas tax = % 4) Oil Company profit range = %
– Alli Vanzant
Jul 2 at 4:17





Your statement (basePrice = ...) looks like a mathematical equation. However this is not how C++ (or any other imperative language) works. It needs instructions on how to compute basePrice, not equations. In this case you're using uninitialized basePrice variable in the right-hand side of assignment, which produces undefined behavior
– Ap31
Jul 2 at 4:18


basePrice = ...


basePrice


basePrice





Ok, I understand that but then how would I write those instructions?
– Alli Vanzant
Jul 2 at 4:29





Alli, now that you've added the formula, it appears my assumption was correct. What you need to do is just realise that basePrice + basePrice * salesTaxFraction is the same as basePrice * (1 + salesTaxFraction) (as per the distributive law of algebra). Then you only have one basePrice you need to worry about and the method in my answer will work fine.
– paxdiablo
Jul 2 at 4:29



basePrice + basePrice * salesTaxFraction


basePrice * (1 + salesTaxFraction)


basePrice




1 Answer
1


basePrice = pumpPrice-fedTax-stateTax-((salesTax*basePrice)/100) ;
// ^^^^^^^^^ ^^^^^^^^^



That statement is attempting to set basePrice so it's probably a bad idea to use the uninitialised basePrice in the calculation.


basePrice


basePrice



Methinks you need to work out the formula you need to use before translating that into code. A good start would be to take the base price and document how each of the taxes is applied to it to get the pump price. Then it's a simple matter of reversing that process.



For example (and I have no idea if this is actually how you US bods do it with your overly complex tax system), let's say that the base price has sales tax of 2.25% applied, and then the absolute values of state and federal taxes added. That would mean:


pumpPrice = basePrice * (1 + salesTax / 100) + stateTax + federalTax



The reverse of that (working out base price from pump price) would come from:


pumpPrice = basePrice * (1 + salesTax / 100) + stateTax + federalTax
=> pumpPrice - federalTax = basePrice * (1 + salesTax / 100) + stateTax [*a]
=> pumpPrice - federalTax - stateTax = basePrice * (1 + salesTax / 100) [*b]
=> (pumpPrice - federalTax - stateTax) / (1 + salesTax / 100) = basePrice [*c]



[*a] Subtract federal tax from both sides.
[*b] Subtract state tax from both sides.
[*c] Divide both sides by sales tax (adjusted) rate.



And, voila, swap the sides and you have your formula:


basePrice = (pumpPrice - federalTax - stateTax) / (1 + salesTax / 100)





I understand that but I don't see any other way to do it. Maybe my understanding of the question as a whole is wrong.
– Alli Vanzant
Jul 2 at 4:12





I just added a comment with the entire problem
– Alli Vanzant
Jul 2 at 4:13





@AlliVanzant, apologies but, without the actual process used for converting base to pump price, that comment's about as useful as "Given the xyzzy and plugh, calculate the twisty factor" :-)
– paxdiablo
Jul 2 at 4:14





Sorry the entire comment didn't load. I feel like I'm stuck here. I can't get sales tax without base price and can't get base price without sales tax and it just doesn't seem to be doable. I've been trying all weekend
– Alli Vanzant
Jul 2 at 4:19





@AlliVanzant: In your question, you say you're given the sales tax percentage. You only have one unknown, not two.
– ShadowRanger
Jul 2 at 4:30






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages