-2.3+1.0Answer with-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.

num = "-2.3"
num1 = float(num)
x = num1 + 1.0
Supplemental information (FW/tool ​​version, etc.)

Python 3.6

  • 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