EGL Development User Group

EGL Development User Group

EGL Development User Group

The EGL Development User Group is dedicated to sharing news, knowledge, and insights regarding the EGL language and Business Developer product. Consisting of IBMers, HCL, and users, this community collaborates to advance the EGL ecosystem.

 View Only
  • 1.  float to int conversion strangeness?

    Posted Mon October 30, 2017 07:47 PM

    Hey All,

     

    Hoping someone might be able to shed some light on this

     

            function main()                f float = 0.97;                i int = f * 16;                j int = 0.97 * 16;                Syslib.writeStdout(i::","::j);        end 

    Can anyone explain why i=16 and j=15?  I would have expected both to be 15.  I even wrote the following java code just to make sure I wasn't going crazy

     

            public static void main(String [] args) {                float f = 0.97f;                int i = (int)(f * 16);                int j = (int)(0.97f * 16);                System.out.println(i+","+j);        }

    And sure enough, both are 15.  Changing from 0.97 to 0.96 in the EGL example makes both come out to 15, which leads me to believe that EGL is automatically rounding the result (rather than truncating) before casting from floating-point to int.  But why? And why wouldn't it round in both cases?  Is there something about a variable vs inline floats that EGL treats the float-to-int conversion operation differently?

     

    Sorry if this is a stupid question, I tried searching google and the forums for information about what EGL is doing in the background for these conversions, but wasn't really able to find very much.

     

    Cheers,

    D.R.

    dras


  • 2.  Re: float to int conversion strangeness?

    Posted Wed November 01, 2017 04:24 AM

    Hi,

    that's realy strange.

    I'm able to reproduce this with RBD 9.5.1.

     

    I would suggest you to create a service request, this seems to be a bug.

     

    Kind Regards!

    Marcel-D


  • 3.  Re: float to int conversion strangeness?

    Posted Wed November 01, 2017 04:38 PM

    Thank you for your response Marcel-D.  To be honest I think creating a service request might be more trouble than its worth (my group doesn't have access to our organization's IBM CSN, I'd have to punt it over to another group to submit the service request).  Really, I could just use the built-in floor() or round() functions before assigning/casting to INT, so its a pretty easy issue to work around.  I just wasn't sure if it was a bug, or if I just wasn't understanding something about the language itself.

    dras


  • 4.  Re: float to int conversion strangeness?

    Posted Thu November 02, 2017 03:07 AM

    Okay.

     

    if you use num(5,2) instead of float it's working also.

     

    Kind Regards!

    Marcel-D


  • 5.  Re: float to int conversion strangeness?

    Posted Thu November 02, 2017 12:50 PM

    YES!  ok so I think I'm starting to make sense of what's going on, I guess that floats get automatically rounded by the cast to int, while num(x,y) does not ... EGL must be converting hardcoded inline floating point numbers into num(x,y) rather than float

     

    The following results in i=16, j = 15

     

            function main()                i int = (0.97 as float) * 16;                j int = (0.97 as num(5,2)) * 16;                Syslib.writeStdout(i::","::j);        end 

    I guess i just need to be careful about using the float data type in my code.  Thanks for your help!

     

    Cheers,

    D.R.

    dras


  • 6.  Re: float to int conversion strangeness?

    Posted Wed November 01, 2017 01:17 PM

    Is this for cobolGen or javaGen?

    Jeff_Douglas


  • 7.  Re: float to int conversion strangeness?

    Posted Wed November 01, 2017 01:42 PM

    well I was just using the RBD debugger on my local Windows development machine, I assume this means its using javaGen?  Ultimately we are a COBOL shop, but I haven't generated and tested in our ZOS/COBOL environment.  I'm new to EGL, just trying to get an understanding of the language and how things work, so it is certainly possible that I'm just doing something wrong or just not understanding something fundamental about how EGL works

    dras