Python Forum
multiplying integer to decimal
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
multiplying integer to decimal
#1
I was trying a multiplication in python
c = 0.72*5

instead of giving me 3.6 it returned 3.599999997 something like that...
whats that about ???? !!!
Reply
#2
https://en.wikipedia.org/wiki/Floating-point_arithmetic
Reply
#3
It's the way floating point arithmetic work, Basic Answers.
There is a Decimal module.
>>> from decimal import Decimal
>>> Decimal('0.72') * Decimal('5')
Decimal('3.60')
Reply
#4
So you are saying that every time I have to do any mathematical work on python I have to import the decimal library and convert each number into decimal....that might get tedious for large programming
Reply
#5
in any case you would like to use string formatting when print float numbers.

>>> x = 0.72*5
>>> x
3.5999999999999996
>>> '{:0.2f}'.format(x)
'3.60'
>>> 'the result of calculations is {:0.2f}'.format(x)
'the result of calculations is 3.60'
>>>
Reply
#6
(Jun-19-2017, 07:03 AM)ArnabRoyBatman Wrote: So you are saying that every time I have to do any mathematical work on python I have to import the decimal library and convert each number into decimal....that might get tedious for large programming
No. Everyone (Python, Fortran, C, C#...) uses floating point, this is how rockets are sent to Saturn. Of course you have to learn to live with its idiosyncrasies. The cases where you want infinite precision are fairly rare (mostly when dealing with money).
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#7
You can also use scaled calculations like
>>> c = (72 * 5) / 100
>>> c
3.6
>>>
Reply
#8
(Jun-19-2017, 11:20 PM)Larz60+ Wrote: You can also use scaled calculations like
>>> c = (72 * 5) / 100
>>> c
3.6
>>>

This is Python, not assembler :)
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#9
(Jun-19-2017, 07:03 AM)ArnabRoyBatman Wrote: So you are saying that every time I have to do any mathematical work on python I have to import the decimal library and convert each number into decimal....that might get tedious for large programming

This is not a Python problem. Please read what floating point is.
It depends what you want. To do scientific calculations, you won't use decimal. For financial calculations you have to.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  number of digits in a decimal.Decimal number object Skaperen 0 771 Aug-26-2023, 07:38 PM
Last Post: Skaperen
  multiplying an iterator Skaperen 9 3,261 Oct-12-2021, 07:07 PM
Last Post: Skaperen
  About integer objects vs integer values Nwb 18 7,653 Apr-29-2019, 07:41 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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