ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Floating-point variable

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Floating-point variable

    Hello,

    I have a variable which is named number, my variable is of type integer. And I have a second variable which is named total. This variable is of type "floating".

    I have to multiply by 0.10, the variable 'number' and store the result in the variable 'total'.
    Code:
     total = number * 0.10;
    So, the resultat is 5 * 0.10 = 0.50

    On AS400 I get only the value .50 instead of 0.50

    52qt.png (see image)


    I saw the doc below, but I don't understand... There are several ways to manipulate a floating?

    https://www.rpgpgm.com/2018/06/diffe...rs-in-rpg.html

    Do you have an idea please?

    Code:
    **FREE
    
    dcl-s number int(3) inz(5);
    dcl-s total packed(5:2);
    
    
    total = number * 0.10;
    
    dsply  %Char(total) ;
    
    *inlr = *on;

    Thank you for your help.

  • #2
    Hi Juliette, I am extremely new to RPG also. I think you using %char is why you are getting the result .50.

    In my helpful RPG book, it says that %char will always remove all leading zeros from the result. Maybe try it with %DEC?

    Comment


    • #3
      RPGNoob is right in that DSPLY is what is making the zero "disappear". It is there. They are wrong however in thinking that %Dec will help - it won't.

      %EditC( total : 'X' ) will retain the leading zero.

      P.S. Using floats in RPG is (to put it mildly) unusual - but you are not using a float - you are using a packed decimal with 2 decimal places.

      Comment


      • #4
        Originally posted by RPGNoob View Post
        Hi Juliette, I am extremely new to RPG also. I think you using %char is why you are getting the result .50.

        In my helpful RPG book, it says that %char will always remove all leading zeros from the result. Maybe try it with %DEC?
        Hello RPGNoob,

        Thank you for your help. I tried with %DEC, the problem is my resultat is 50 instead of 0.50

        Comment


        • #5
          Originally posted by JonBoy View Post
          RPGNoob is right in that DSPLY is what is making the zero "disappear". It is there. They are wrong however in thinking that %Dec will help - it won't.

          %EditC( total : 'X' ) will retain the leading zero.

          P.S. Using floats in RPG is (to put it mildly) unusual - but you are not using a float - you are using a packed decimal with 2 decimal places.
          Hello JonBoy,

          I don't know `editC,` except that my display show 0050 instead of 0.50 but how I could add a point for example? Is it possible ?

          Code:
          **FREE
          
          dcl-s number int(3) inz(5);
          dcl-s total packed(4:2);
          
          
          total = number * 0.10;
          
          dsply %EditC( total : 'X' );
          
          *inlr = *on;

          Comment


          • #6
            Regarding %EDITC, check the help. Either put your cursor on the BIF and press F1, or download a PDF of IBM's "ILE RPG Reference".

            I second JonBoy's statement about floats. Their usage is very rare in RPG.

            Finally, rather than the DSPLY opcode, why not just use a debugger to view values?


            HTH

            Comment


            • #7
              Hey Juliette

              You could try adding a H spec to the top of your program as:
              PHP Code:
              ctl-opt DECEDIT('0.'); 
              This is a decimal edit and means that all decimal numbers in the program will have the leading zero. Use this with your standard %char and it should keep the leading zero. It's great if you need to apply this format multiple times in the program.

              Another alternative is the BIF (built in function), %editw which allows you to format the output in place of %char. You could use this if you knew the number of decimals you're working with and only wanted this particular instance to have a leading zero.
              PHP Code:
              dsply %editw(total '   0.  '); 
              EDITC is great for formatting numbers however as you've found, the decimals go away with the X type. I'm having trouble loading the IBM page for some reason (anyone else??) but it will be very useful to familiarize yourself with the available codes as no doubt you will need them in the future.

              Comment


            • #8
              Hello,

              Ok, I understood... I have to use this line below:

              Code:
              dsply %editw(total : '   0.  ');
              PS: Thank you for the link, it's interessant.

              Comment


              • jtaylor___
                jtaylor___ commented
                Editing a comment
                The %editc() BIF allows 1-4, A-D, and J-Q for the 2nd parameter, each giving a slightly different result. Check out the "Edit Codes" section of the manual if you want to know more.

            • #9
              Regarding the %EDITC() and the value of the editcode I would recommend you to take a look at the DDS manual.
              There the editcodes are described much better than in the RPG manual. And they are identical.
              You can easily find out what to use.
              Take a look at it:



              Just be aware that in RPG it is called %EDITC() and in DDS it is called EDTCDE.
              It is a common mistake for me to write %EDTCDE() when coding RPG -- even after 30+ years of experience.
              One of the stupid things that IBM hasn't been consistent in their naming.

              Comment


              • #10
                Juliette,
                I sense some confusion about the different types of numeric fields in RPG.
                If you are used to C++, Java or other languages like these then you are familiar with
                the types integer and float. The types packed and zoned are not known very much outside
                the RPG or COBOL world but are very usefull when coding ERP systems where the precision
                of the decimals are important.


                You already have referenced the link to
                RPG has different types of numbers formats. This is a brief explanation of how to use them and how to swap data between them.

                There Simon Hutchinson describes the different formats very well.

                Normally you don't have to bother about if a field is packed or zoned.
                When using them in calculations or comparing them there is no difference.
                The difference is how they are stored in the memory and this you normally shouldn't bother
                about until you get a little more experience. But if you are curious about it take a look in the RPG manual.

                So if you define a field as zoned(5:2) or packed(5:2) you are defining a field that can contain 5 digits of which
                2 are decimals. This means that it can contain a value between -999.99 and +999.99

                In Simons example he shows how the values are converted
                PACKED 00123.45
                FLOAT4 1.234499969482E+002

                And you can see that the floating format value is not precise.
                Given you are accumulating different sums then the difference in float is accumulating and in the end you risk
                having a hell with accounting
                The packed and zoned fields keeps the precision in the decimal parts.

                This is one of the reasons that float is not used very much in RPG.


                Comment


                • #11
                  Hello Peder Udesen,

                  Thank you for your explanations. It's different compared to java.

                  Comment


                  • #12
                    Hello Peder Udesen,

                    Thank you for your explanations. It's different compared to java.

                    Comment

                    Working...
                    X