0

Im very bad but I need this for practical reason : why does my code always prints "2500" when 'revenus" is superior to 15000 ?

It's about progressive tax scale : we give him a revenue, and he gives us the tax we have to pay. Scale 1 : NOT any tax under 15 001 € (excluded). Scale 2 : Tax 10% between 15 001 and 4000 € (includeds). Scale 3 : Tax 15% between 40 001 and 80 000 € (included). Scale 3 Tax 25% whatever is above 80 000 € (excluded).

This could be some stupid syntax error because of the combination of the if statements, tell me if you see some blatant error.

revenus = 17305
copyrevenus = 17305
impots = 0

taux1 = 0/100
taux2 = 10/100
taux3 = 15/100
taux4 = 25/100

full1 = 0  #When the first scale is fully filled (superior or equal to the scale's max amount) i don't bother with multiplication, so this is for 15 000 * taux1 etc.
full2 = 2500
full3 = 6000 

if copyrevenus > 15000
 impots += full1
 revenus - 15000
 if copyrevenus > 40000
   impots += full2
   revenus - 25000
     if copyrevenus > 80000
       impots += full3
       revenus - 40000
       impots += revenus * taux4
     else
     impots += revenus * taux3
     end
 else
 impots += revenus * taux2
 end
else
 impots = revenus * taux1
end

puts impots
  • 1
    French taxes haha. Your `taux` are wrong because you're using Integer divisions. I'll check the rest in a jiffy – Fravadona Apr 02 '21 at 18:25
  • *"[W]hy does my code always prints "2500" when 'revenus" is superior to 15000?"* It does not it prints "0" (as is). It prints "2500" when revenus is greater than 40000 and less than or equal to 80000 – engineersmnky Apr 02 '21 at 18:46
  • Ok yes that was obvious im blind... Thanks ! I fixed integers and the -= assignment, and it works for all scales now... Thank you guys ! – Maître Michel1 Apr 02 '21 at 18:56
  • "This could be some stupid syntax error" – It *cannot possibly be* a syntax error, because if it *were* a syntax error, the code could not even be parsed, let alone run. The fact that you are getting *any result at all*, even if the result is wrong, already shows that it is *impossible* to be a syntax error. – Jörg W Mittag Apr 03 '21 at 07:49

1 Answers1

0

Answered by Fravadona and mu is too short : I fixed the divisions to make the result float, and my substractions didn't assign any value to "revenus", so now it is fixed.

revenus = (write the amount)
fixrevenus = revenus
impots = 0

taux1 = 0.0/100.0
taux2 = 10.0/100.0
taux3 = 15.0/100.0
taux4 = 25.0/100.0

full1 = 0 
full2 = 2500
full3 = 6000 

if fixrevenus > 15000
 impots += full1
 revenus -= 15000
  
 if fixrevenus > 40000
   impots += full2
   revenus -= 25000
   
     if fixrevenus > 80000
       impots += full3
       revenus -= 40000
       impots += revenus * taux4
     else
     impots += revenus * taux3
     end
 
 else
 impots += revenus * taux2
 end

else
 impots += revenus * taux1
end

puts impots