Python Forum
Why is this multiplication not working?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is this multiplication not working?
#1
Hello,I'm very new to Python, and I'm having trouble with some basic math.  I've getting a user to input a number and assigning it to a variable.  Then I'm multiplying it by the value of another variable, which I've set to 0.1 and displaying the result.  When the input is 3, and I multiply it by the other variable, instead of getting .3, I'm getting .3000000000004.

What could cause the multiplication to return what appears to be an incorrect value?
Reply
#2
Show the code, it's impossible to answer correctly without.
Reply
#3
testMultiplier=0.1
testInput=int(input("Enter a number"))
testOutput=testInput*testMultiplier
print (testOutput)


If I input 3 when asked, it spits out 3.000004
Reply
#4
Quote:instead of getting .3, I'm getting .3000000000004.
It's the way floating point arithmetic work,Basic Answers Python doc.
There is a Decimal module.
>>> 0.4 - 0.1
0.30000000000000004

>>> from decimal import Decimal
>>> Decimal('0.4') - Decimal('0.1')
Decimal('0.3')
Reply
#5
thanks, that was very helpful.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Multiplication Table code alexsendlegames100 3 1,317 Jun-06-2022, 09:45 AM
Last Post: Gribouillis
  Try to solve GTG multiplication table problem. Frankduc 6 1,937 Jan-18-2022, 08:26 PM
Last Post: Frankduc
  Need help with my Python code (Multiplication) NeedHelpPython 2 1,635 Oct-04-2021, 12:09 PM
Last Post: Pedroski55
  List conversion and multiplication johnkyp 5 3,105 Jan-02-2020, 08:20 AM
Last Post: perfringo
  Matrix Multiplication Issue VIJENDRA 1 1,832 Dec-19-2019, 06:16 PM
Last Post: Gribouillis
  Multiplication between a list and a variable doug2019 2 2,123 Oct-08-2019, 04:10 AM
Last Post: doug2019
  Multiplication Table number margins CJ707 4 2,368 Sep-18-2019, 02:16 PM
Last Post: CJ707
  multiplication by successive addition Zebrol 1 3,471 Sep-14-2019, 05:37 PM
Last Post: ichabod801
  float multiplication - unexpected output inesk 3 3,258 Dec-11-2018, 10:59 AM
Last Post: DeaD_EyE
  Print every multiplication of recursion Arontbt 4 3,679 Apr-24-2018, 10:01 PM
Last Post: Arontbt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020