Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.

 View Only
  • 1.  float precision problem

    Posted Mon January 17, 2011 05:51 AM

    Originally posted by: victortkl


    Hi, using opl script, the float precision problem occures
    i subtract 0.01 from 0.3 in each iteration, and got 0.199999999 instead of 0.2.
    how can i solve this problem

    here is my code example
    dvar float x;
    maximize x*2;
    subject to
    {
        x<=2;    
    }
     
    main
    {
        for (var i=0.2;i>=0;i=i-0.01)
        {
            writeln("i:\t"+i);    
        }    
    }
    


    here is the output:

    i: 0.2
    i: 0.19
    i: 0.18
    i: 0.17
    i: 0.16
    i: 0.15
    i: 0.14
    i: 0.13
    i: 0.12
    i: 0.11
    i: 0.09999999999999996
    i: 0.08999999999999997
    i: 0.07999999999999997
    i: 0.06999999999999998
    i: 0.05999999999999998
    i: 0.04999999999999998
    i: 0.03999999999999997
    i: 0.02999999999999997
    i: 0.01999999999999997
    i: 0.009999999999999969
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: float precision problem

    Posted Wed January 19, 2011 08:39 PM

    Originally posted by: SystemAdmin


    There is no function for setting display precision of script data. You can convert it into a string and do appropriate formatting as described in technote:
    http://www-01.ibm.com/support/docview.wss?uid=swg21401490

    For OPL model, display precision can be set as described in the documentation:
    IDE and OPL > Optimization Programming Language (OPL) > Parameters and Settings in OPL > OPL language options

    Regards,
    Faisal
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: float precision problem

    Posted Wed January 19, 2011 09:58 PM

    Originally posted by: victortkl


    thank you
    this problem also occurs when i write into database and make some data wrong
    but it's seems not a good alternative to change the number into string.
    regards
    victortkl
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: float precision problem

    Posted Fri January 21, 2011 06:48 PM

    Originally posted by: SystemAdmin


    Have you considered using SQL Function like TRUNCATE when you insert your data?
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: float precision problem

    Posted Mon January 24, 2011 05:25 AM

    Originally posted by: victortkl


    it's not a wise idea. because in every iteration, the model will use this value as a parameter. this imprecise value will cause some problem
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 6.  Re: float precision problem

    Posted Wed January 19, 2011 09:02 PM

    Originally posted by: John Cui


    Set your CPLEX algorithm to dual or primal simplex will help you fix the problem.
    Please give a try, and tell us the result.

    John Cui
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 7.  Re: float precision problem

    Posted Wed January 19, 2011 09:18 PM

    Originally posted by: John Cui


    Sorry,I misunderstood.

    John Cui
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 8.  Re: float precision problem

    Posted Thu January 20, 2011 01:56 PM

    Originally posted by: GuangFeng


    You cannot convert 0.2 and 0.01 into IEEE 754 float numbers without losing any precision (round off errors). The way you are doing it:

    i=i-0.01

    accumulates round off errors over time.

    The best way is to use integers. For example, if 0.2 represents 0.2 dollar, try to express it in cents: 20. Or at least you should avoid the accumulation of the round off errors. Try to use something as below at least, although it is still not perfect:

    main
    {
        for (var n=0;n<=20;n++)
        {
            writeln("i:\t"+((20-n)/100));    
        }    
    }
    

    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 9.  Re: float precision problem

    Posted Thu January 20, 2011 09:21 PM

    Originally posted by: victortkl


    thank you
    it's a good alternative. but still can solve this difficulty absolutely.
    here is the answer.

    i: 0.2
    i: 0.19
    i: 0.18
    i: 0.17
    i: 0.16
    i: 0.15
    i: 0.14
    i: 0.13
    i: 0.12
    i: 0.11
    i: 0.1
    i: 0.09
    i: 0.08
    i: 0.07000000000000001
    i: 0.06
    i: 0.05
    i: 0.04
    i: 0.03
    i: 0.02
    i: 0.01
    i: 0
    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 10.  Re: float precision problem

    Posted Fri January 21, 2011 05:40 PM

    Originally posted by: GuangFeng


    The codes demonstrate the idea of avoiding round off error accumulation. For your simple scenario, converting the numbers to integers is a better and easier alternative.
    #DecisionOptimization
    #OPLusingCPLEXOptimizer