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: 9,048 Threads: 326 Joined: Sep 2016 Reputation: 388 Likes received: 1182 #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: 2,269 Threads: 25 Joined: Jan 2018 Reputation: 216 Likes received: 548 #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 Da Bishop Posts: 1,299 Threads: 5 Joined: May 2017 Reputation: 146 Likes received: 289 #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 Unexpected output palladium 4 326 Jan-11-2020, 03:26 PM Last Post: palladium Unexpected output: if statement CabbageMan 1 286 Sep-04-2019, 04:12 PM Last Post: ThomasL Comaparing Float Values of Dictionary Against A Float Value & Pick Matching Key firebird 2 555 Jul-25-2019, 11:32 PM Last Post: scidam Unexpected Output using classes and inheritance langley 2 421 Jul-04-2019, 09:33 AM Last Post: langley Unexpected output when searching for a string from os.popen output FujiJean 3 877 Oct-02-2018, 11:39 AM Last Post: volcano63 Unexpected twice output Samo 3 771 Sep-29-2018, 10:00 PM Last Post: Samo Unexpected simpy simulation output bakas 1 1,755 Sep-07-2018, 01:14 AM Last Post: scidam unexpected output for global variable bhdebasish 1 764 Aug-22-2018, 04:55 PM Last Post: Larz60+ A little unexpected output from a while loop linuxnoob 3 852 Jul-17-2018, 01:02 AM Last Post: gontajones Unexpected output, TypeError and traceback error fier259 2 1,036 May-06-2018, 10:52 PM Last Post: fier259

Forum Jump:

Users browsing this thread: 1 Guest(s)