-1.3I would like to try, but I am having trouble because it is not a correct number.
The current output looks like this:
Applicable source code
When I changed the value and executed it, there are cases where it works, and I am not sure what to do.
Supplemental information (FW/tool version, etc.)
num = "-2.3" num1 = float(num) x = num1 + 1.0 print(x) #output -2.3
Answer # 1
float may not be a good representation of the decimal point and may be an approximation. Without calculation
Although it rarely appears on the table, an error occurs when calculating.
That is exactly what the questioner encountered.
For such a case, decimal is prepared, so using it will not cause an error.
Official documentation about decimal
from decimal import * num = "-2.3" num1 = Decimal(num) Note that x = num1 + Decimal("1.1") # 1.1 is a string representation. print(x) # -1.2 # Useless example 1 num2 = Decimal(num) y = num2 + Decimal(1.1) # 1.1 is a float, so an error occurs print(y) # -1.199999999999999911182158030
Answer # 2
The numeric representation of float type is called floating point.
In floating point, the number with a decimal point is represented by a binary number, but there is an error because it can not be expressed when converting from a decimal number with a decimal point you are handling to a binary number
In addition, an error will occur when converting the binary result to the decimal number.
So, if you want to avoid that error, write the code in decimal not using the float type
- python - about float type calculation problem (easy and sorry)
- about a problem with python
- i have a question about a problem with python i tried many times
- python - about the problem that the screen display does not work when i have multiple apps in django
- about the problem that python fails to determine the exact match of a string
- about the problem that cabocha can not be bound in python
- python - about import error of flask
- python - about sytemctl using error snap in slurm
- python - about getting all dataframes
- about python animation axis setting
- about python errors
- about python regular expressions
- python - about dp matching
- about string search for python dictionaries
- python - about "itertoolscombinations"
- python - about the configuration of "adminsiteurls" in django source
- about updating mac python
- python - about sorting characters when manipulating strings using the set function
- about the output result of iloc method of python
- [python] about fonts and frames of ttklabelframe
- python - i want to get a string only when spaces and hyphens are exact matches, but i can't process it with one code
- how to write to count the number of specific elements of dictionary values in python3
- python - output of private variables of class, list in class
- python num error
- exclude even and odd lines in python csv
- python - i want to install fasttext
- about python 3d scipyinterpolate
- i want to convert json in python
- python - about the return value of the assertis method
- how to pass mac python path