Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 float multiplication - unexpected output inesk Unladen Swallow Posts: 1 Threads: 1 Joined: Dec 2018 Reputation: 0 Likes received: 0 #1 Dec-11-2018, 10:41 AM (This post was last modified: Dec-11-2018, 10:41 AM by inesk. Edited 1 time in total.) Hello, I have a unexpected output on a simple multiplication: ```0.55*50000 `````````Output: 27500.000000000004 ``````While if I run an equivalent expression ```0.55*5*10000 `````````Output: 27500.0 ``````Which is the right output. Is is also the case for other expressions like : 0.55*100 that gives: 55.00000000000001 Anybody knows what this is due to ? Many thanks, Inès Larz60+ aetate et sapientia Posts: 7,268 Threads: 283 Joined: Sep 2016 Reputation: 259 Likes received: 1017 #2 Dec-11-2018, 10:43 AM (This post was last modified: Dec-11-2018, 10:43 AM by Larz60+. Edited 1 time in total.) floating point is multiplication of two numbers, an integer times an exponent and therefore is a close approximation, not exact Quote:Which is the right output. Both are in their situation. Gribouillis Posts: 1,507 Threads: 11 Joined: Jan 2018 Reputation: 133 Likes received: 355 #3 Dec-11-2018, 10:59 AM (This post was last modified: Dec-11-2018, 10:59 AM by Gribouillis. Edited 1 time in total.) See this thread or this one DeaD_EyE Giant Foot Posts: 863 Threads: 5 Joined: May 2017 Reputation: 73 Likes received: 185 #4 Dec-11-2018, 10:59 AM You need to learn more about floating point numbers. Floating point numbers can't represent all decimal fractions. ```import webbrowser webbrowser.open(str(0.1 + 0.2) + '.com') ```http://0.30000000000000004.com/ If you need something, which does not have this problem: decimal fractions Larz60+ and Gribouillis like this post My code examples are always for Python >=3.6.0 Almost dead, but too lazy to die: https://sourceserver.info All humans together. We don't need politicians! « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Tracing a multiplication table w/ Python trace() NationalRex22 0 65 Jun-11-2019, 03:31 AM Last Post: NationalRex22 ValueError: could not convert string to float michaelbs007 1 328 Jun-08-2019, 08:44 PM Last Post: micseydel Help | float to Int in xlrd Vinci141 0 136 May-13-2019, 07:00 AM Last Post: Vinci141 Inclusive (closed) range with float numbers mapg 4 206 Apr-27-2019, 09:09 PM Last Post: Gribouillis syntax error near unexpected token btom529 5 267 Apr-21-2019, 09:03 PM Last Post: btom529 Getting an unexpected generator object ? mcgrim 19 424 Mar-29-2019, 11:35 AM Last Post: gontajones Dictionary adds an unexpected list erina 1 188 Mar-14-2019, 01:13 PM Last Post: ichabod801 unexpected EOF while parsing whatloop 3 412 Mar-09-2019, 06:59 PM Last Post: whatloop Using Decimal Type instead of Float Bytales 1 298 Mar-02-2019, 06:28 PM Last Post: Bytales Unexpected round behavior pythonCoder 1 162 Feb-19-2019, 02:39 PM Last Post: marienbad

Forum Jump:

Users browsing this thread: 1 Guest(s)