This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 2 years ago.
I'm trying to create a module that will separate the decimal from a number as I need it for a project.
However when I test it the integer comes out fine but the decimal always gets an error:
def seperate(decimal):
integer = int(decimal)
dec = decimal-integer
print(dec, integer)
if I say try to enter 2015.677 it give this: 0.6769999999999072 2015
what is wrong?
Actually what i suggest is that you can try this alternative approach,
And for this to work properly we always need to pass a floating value to it, and it also works for your conditions
def seperate(decimal):
integer = int(decimal)
decimal = str(decimal)
dec = decimal[decimal.index('.')+1:]
return(int(dec), integer)
what's the error you are getting?
I tried your code, and it worked just fine on Python 3.7
decimal = 3.5
integer = int(decimal)
dec = decimal-integer
print(dec, integer)
If you give us the error, perhaps I can help you a little bit more.
Regards!
Edit:
After reading the update on the rounding problem, i used the "round" function and returned the 0.677 part of your decimal just fine.
Related
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 2 years ago.
I came across the following strange result in Python (I use Spyder environment). Any idea what is going on? And how can I fix this? I truly don't want to put 20 zeros in front of my variable nor using numpy for such a simple work makes sense!
int(121212000000000000000000000000000000000000000000000000)
Out[27]: 121212000000000000000000000000000000000000000000000000
int(121212*1e20)
Out[28]: 12121199999999999802867712
int(121212*10e20)
Out[29]: 121211999999999993733709824
It has to do with floating point precision.
You can use the decimal module like so:
>>> from decimal import Decimal
>>> Decimal(121212) * Decimal('10e20')
Decimal('121212000000000000000000000')
For more info, see the following Python tutorial.
This question already has answers here:
How to round to 2 decimals with Python? [duplicate]
(21 answers)
Closed 4 years ago.
How would I format my float digits so that there is always 7 digits no matter the direction?
The value could be any of the following but I always want 7 like below
0.0054233
1234567
123.1224
1.992923
The portion I have right now is
return "%.7f" % fd
How can I adjust that to get my desired output? or maybe link me to something similar I could try and read from..>?
Try if this can work for you:
n = 123.456789
decimals = 7 - len(str(int(n)))
print('{:.{prec}f}'.format(n, prec=decimals))
#=> 123.4568
The drawback is that it rounds up.
It depends on the context of the program, in my opinion... If you just want the numbers to the right to be 6 decimals, use:
"{:.6f}".format(whatevervar)
In other contexts maybe convert it to a string and use len() to evaluate the number of digits.
Hope this helps!
EDIT: Seeing your comments, I would recommend using a conditional to define what you are trying to do. When the number has no decimals (check this thread for how to do it: How to check if a float value is a whole number ) leave it as it is, when it has decimals, round it up with the code I posted above.
This question already has answers here:
Is floating point math broken?
(31 answers)
Why does Python return 0 for simple division calculation?
(6 answers)
Closed 5 years ago.
This is driving me mad... Of all the years I've been using python, this is just now starting to present itself. How I managed to dodge it up until now is beyond me.
If you open a python idle and try this equation...
4/32*100
You'll get '0' as an answer. Now try the same equation using floats....
4.0/32.0*100.0 (or just the first number 4.0/32*100)
You now get an actual percentage.
WTF!?
Is this some kind of python error!? Even a calculation can do the equation and spit out a percentage.
So why can't python see a 4 as 4.0. Better question... What is the interpreter actually seeing if it's not seeing a 4(4.0)?
Someone please clear this up so I can feel professional with python again (lol).
In Python 2, int type division ignores the decimal values of the division.
For example, 1/2 = 0.5, but in int type division, 1/2 will evaluate to 0 because it ignores the decimal values.
Thus, in your case with 4/32*100, 4/32will first evaluate to 0 and then 0*100 will finally equal 0.
On the other hand, in float type division, it will evaluate answers as we would expect (not in a strictly precise definition though, look here for further information).
For Python 2.x, dividing two integers or longs uses integer division, also known as "floor division"(applying the floor function after division)
For Python 3.x, "/" does "true division" for all types.
To make python perform true division, cast any of the denominator for numerator to become float.
float(4)/32*100
or
4/float(32)*100
or doing below to make python 2 division behave like python 3 division
from __future__ import division
4/32*100
This question already has answers here:
round() doesn't seem to be rounding properly
(20 answers)
Closed 6 years ago.
The community reviewed whether to reopen this question 5 months ago and left it closed:
Original close reason(s) were not resolved
The Python round() function will theoretically take negative numbers to round to places left of the decimal. [ I.e. round(150, -2) => 200]
However, it seems to be very susceptible to floating point error.
For example, given a large number say 2e25, it gives weird results.
2e25 === 20000000000000000000000000
But, round(2e25, -23) gives a value like
20000000000000000273942742
When it should just be getting 20000000000000000000000000
I know there's a formatting function, a la this thread:
round() in Python doesn't seem to be rounding properly
However, that only seems to work for rounding to the right of the decimal. Am I wrong? Is there another way to do this? Very frustrating trying to get the math right.
Thanks!
The problem is that 2e25 doesn't actually equal 20000000000000000000000000.
>>> 2e25 == 20000000000000000000000000
False
>>> 2e25 == 20000000000000001811939328
True
The float type doesn't have enough precision to represent such a large integer exactly. Unless you have a good reason for using floating-point values, use integers instead.
This question already has answers here:
rounding up with python
(2 answers)
How do you round UP a number?
(28 answers)
Closed 9 years ago.
I have been writing a program for a project and have tried using the round() function to round a floating point variable up to the next whole number. In the case of this program, it calculates how much paint is required (in gallons) to paint a certain amount of square footage. The exercise assumes that paint is only available in whole gallons, so my calculation needs to round up all fractions of a gallon to the next whole gallons.
Any thoughts?
Thanks,
Scott
Use math.ceil():
print math.ceil(3.4)
# 4.0
I found the error. Apparently I need to import the math module into my program. When I read the Python documentation, it stated that the math module was always available. Now that I have added "import math" to the beginning of my program, I am not getting the syntax error.
If anyone is interested in the syntax I used, here is the code:
def calculateGallonsNeeded(fltWallArea,AREA_UNIT):
fltGallonsNeeded = math.ceil(fltWallArea / AREA_UNIT)
return fltGallonsNeeded
Thanks for all the help.
Scott