Decision Optimization

Decision Optimization

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

 View Only
Expand all | Collapse all

int fr = ftoi(frac(1.2)*10) doesn't work?

  • 1.  int fr = ftoi(frac(1.2)*10) doesn't work?

    Posted Mon August 04, 2014 01:12 AM

    Originally posted by: qtbgo


    Hi, I have the following OPL code in a mod file, but it reports error when I run it, can anyone tell me why?

    int fr = ftoi(frac(1.2)*10);

    execute{

       writeln(fr);
    }
     

    The error is: Bad Conversion between an integer object and a numeric object.


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 2.  Re: int fr = ftoi(frac(1.2)*10) doesn't work?

    Posted Mon August 04, 2014 03:32 AM

    Originally posted by: PhilippeLaborie


    Hello,

    The documentation of ftoi(x) states that x should be a floating point that represents an integer:

    Purpose: OPL function to convert a float representing an integer to an integer represented as an integer.

     

    The result of function ftoi is undefined when the integer cannot be represented exactly in type int.

     

    In your example, I suppose that the result of frac(1.2)*10 that is 0.2*10 cannot be represented exactly as an integer because 0.2 cannot be represented without loss of precision as a floating point. You could do something like:

    int fr = ftoi(round(frac(1.2)*10));
    execute{
       writeln(fr);
    

    Philippe

     


    #DecisionOptimization
    #OPLusingCPOptimizer


  • 3.  Re: int fr = ftoi(frac(1.2)*10) doesn't work?

    Posted Mon August 04, 2014 07:10 AM

    Originally posted by: qtbgo


    Thank you, Philippe.


    #DecisionOptimization
    #OPLusingCPOptimizer