Announcement

Collapse
No announcement yet.

Functions which return floating point values.

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

  • Functions which return floating point values.

    I want to return floating point values from functions similar to the following, but...

    This is returning bizarre results:

    Code:
     SEED (sys_T());
        var myfloat[2];
        repeat
        flt_ADD (myfloat,NUM1(),NUM2());
        flt_PRINT (myfloat,"TOTAL:%f \n");
        pause (1000);
        forever
    endfunc
    
    func NUM1()
    var fl1[2],fl2[2];
    flt_ITOF(fl1,RAND());
    flt_LOG (fl2,fl1);
    flt_PRINT (fl2,"NUM1: %f \n");
    return (fl2);
    endfunc
    
    func NUM2()
    var fl1[2],fl2[2];
    flt_ITOF(fl1,RAND());
    flt_LOG (fl2,fl1);
    flt_PRINT (fl2,"NUM2: %f \n");
    return (fl2);
    
    endfunc
    Am I doing something incorrectly? Or should I not attempt to return floats?


    J.Dyess

  • #2


    Well, as you've worked out a float is really a two dimensional array of integers.

    So when you say
    return (fl2);

    You are 'simply' returning the address of fl2, which a) isn't what you want and b) will be invalid immediately after the statement is executed as fl2 is part of the stack that will be freed during the return.

    Passing the address to the routine is probably a better way to go about it.
    Mark

    Comment


    • #3


      Neither C or 4DGL is native to me, and since
      Passing the address to the routine is probably a better way to go about it.
      seems ambiguous, I've worked around it on my own. But thank you for the prompt response.

      Cheers
      J.Dyess

      Comment


      • #4


        Fully understand, that behaviour comes from C, for sure.

        C tends to be more 'grass roots', although it's not always apparent what it's really doing.

        Often other languages may pass addresses behind the scenes, but I guess that doesn't help you.

        What language is native to you?
        Mark

        Comment


        • #5


          I'm sorry. English would be my only native language. I'm just trying to figure out what address should be passed to what routine. There are plenty to choose from. I'm pretty sure you don't mean to return the pointer to the local float in my functions because that wouldn't work, right? They get destroyed too when the function terminates. Then there would be the literal interpretation to pass the address of the routine itself in program memory, but pass it to what? So that one's out too. Of course there's the obvious (and always last on the list) of passing the pointer of a float declared in main to the sub functions. I'm thinking that was what you meant?
          J.Dyess

          Comment


          • #6


            As a work around I attempted to create global floats and set them from the sub functions. That process, however, resulted in some sort of address error that I didn't have time this evening to delve into.
            J.Dyess

            Comment


            • #7


              I have to say though, I am really impressed with this product. I can't get over how versatile it is at such a modest cost.
              J.Dyess

              Comment


              • #8


                Here's a simplistic example


                func fltadd(var fr, var fv1, var fv2)
                flt_ADD(fr, fv1, fv2) ;
                endfunc
                func main()
                var f1[2], f2[2], f3[2] ;
                flt_VAL(f1, "-0.345") ;
                flt_VAL(f2, "23.4") ;
                fltadd(f3,f1,f2) ;
                flt_PRINT(f3, "%g ");
                repeat
                forever
                endfunc
                Mark

                Comment


                • #9


                  Thank you very much for the sample code it does clarify things a bit for me. It's all a matter of aesthetics. I want the code to be modular and easy to read for other techs who might work on it later, or god forbid, engineers. That is the only reason I wanted values returned from the function calls themselves. Ie ...add (...,NUM1(),NUM2()); but if that's a faux pas then I will restructure it. No big. Thank you. :-)
                  J.Dyess

                  Comment

                  Working...
                  X