Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Number bug, calculation is wrong d3fi Programmer named Tim Posts: 12 Threads: 5 Joined: Jul 2019 Reputation: 0 Likes received: 0 #1 Aug-26-2019, 09:11 PM so here is my code ``` elif im.lower() in ("inh, inhalation, inhala"): # inhalation barn = (input("Barn eller voksen? ")) if barn.lower() in ("voksen", "vok"): # to adults print("1 mg blandes op i 2 ml saltvand og gives på nebulisator") elif barn.lower() in ("barn", "ba", "bar"): # to child vaegt = int(input("Hvad vejer barnet? ")) inhalation = vaegt * 0.1 #Here is what should be calculated if inhalation > 1: print("1 mg blandes op i 2 ml saltvand og gives på nebulisator") else: print(inhalation, "mg blandes op i 2 ml saltvand og gives på nebulisator") #This is broken, i think. ```if you type in "7" in line 6, the output will be 0.7000000000000001. it just needs to be 0.7. why does it do that? i am writing in JetBrains pycharm jefsummers Verb Conjugator Posts: 545 Threads: 1 Joined: May 2019 Reputation: 50 Likes received: 73 #2 Aug-26-2019, 09:31 PM https://docs.python.org/3/tutorial/floatingpoint.html Floating point is an approximation. Remember we are working with base 2. The above has a nice discussion of floating point and the inherent errors, in any programming language, of using it. perfringo Da Bishop Posts: 1,355 Threads: 7 Joined: Jun 2018 Reputation: 137 Likes received: 298 #3 Aug-27-2019, 05:29 AM (This post was last modified: Aug-27-2019, 05:29 AM by perfringo. Edited 1 time in total.) Python has built-in module decimal which purpose defined as: "The decimal module provides support for fast correctly-rounded decimal floating point arithmetic." ```>>> import decimal >>> decimal.getcontext().prec = 2 # set precision to 2 (default is 28) >>> vaegt = decimal.Decimal(input("Hvad vejer barnet? ")) Hvad vejer barnet? 7 >>> inhalation = vaegt * decimal.Decimal(0.1) >>> print(inhalation) 0.70 >>> inhalation Decimal('0.70') ```It's important to note: Quote:The significance of a new Decimal is determined solely by the number of digits input. Context precision and rounding only come into play during arithmetic operations. This means that: ```>>> decimal.Decimal(7 * 0.1) Decimal('0.70000000000000006661338147750939242541790008544921875') >>> decimal.Decimal(7) * decimal.Decimal(0.1) Decimal('0.70') ``` I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame. « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post python gives wrong string length and wrong character thienson30 2 380 Oct-15-2019, 08:54 PM Last Post: Gribouillis Correct number wrong position func. albry 5 1,032 Jan-11-2019, 04:01 PM Last Post: Larz60+ [Regex] Findall returns wrong number of hits Winfried 8 1,854 Aug-23-2018, 02:21 PM Last Post: Winfried

Forum Jump:

Users browsing this thread: 1 Guest(s)