• AVR Freaks

Hot!long double variables not printing properly after subroutine pass

Author
fsonnichsen
Super Member
  • Total Posts : 330
  • Reward points : 0
  • Joined: 2009/08/13 11:00:11
  • Location: 0
  • Status: offline
2021/01/19 10:37:51 (permalink)
0

long double variables not printing properly after subroutine pass

Does anyone want to venture what I may be doing wrong here? (see attached)
I set a "long double" in a subroutine and pass it up to the calling program.
In the routine it prints fine but when passed up it appears as zero.
Thanks
Fritz

Product Version: MPLAB X IDE v5.30
System: Windows 10 version 10.0 running on amd64; Cp1252; en_US (mplab)
XC16: 1.60
PIC24FJ128GA010
 
#1

10 Replies Related Threads

    DuaneH
    New Member
    • Total Posts : 15
    • Reward points : 0
    • Joined: 2009/10/15 11:49:22
    • Location: Sandy, UT
    • Status: offline
    Re: long double variables not printing properly after subroutine pass 2021/01/19 10:49:15 (permalink)
    0
    Based on what I have seen before, I would suggest that you make your variable global and then pass it's address the your routine.  My guess is that the compiler doesn't return long doubles properly.  You can verify this by breaking at the return point in your current subroutine and then single-stepping through the disassembly listing to see (1) where the variable is put - (which registers), and (2) where the calling program looks for the return value.  I'll bet there's a 'disconnect' between the two.
    Duane

    duaneh
    #2
    Jerry Messina
    Super Member
    • Total Posts : 666
    • Reward points : 0
    • Joined: 2003/11/07 12:35:12
    • Status: offline
    Re: long double variables not printing properly after subroutine pass 2021/01/19 11:13:12 (permalink)
    0
    fsonnichsen
    I set a "long double" in a subroutine and pass it up to the calling program.

    What do you mean "pass it up"?
     
    You're not expecting

            getvar(vr);

    to modify the contents of your global variable 'vr', are you?
    #3
    1and0
    Access is Denied
    • Total Posts : 12086
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: long double variables not printing properly after subroutine pass 2021/01/19 11:54:13 (permalink)
    0
    fsonnichsen
    Does anyone want to venture what I may be doing wrong here? (see attached)
    I set a "long double" in a subroutine and pass it up to the calling program.
    In the routine it prints fine but when passed up it appears as zero.

    Here is your main():
    char buf[100];
    long double vr;
    int main(void)
    {
        while(1)
        {
            SYSTEM_Initialize();
            
            getvar(vr);
            sprintf(buf,"var= %Lf \r\n",vr);     
            put_string(buf);
         __delay_ms(1500);
        } //end while(1)
    } //end main

    As Jerry said, are you expecting the variable "vr" to have a value of 12345.678 from the getvar() function?
     
     
    #4
    fsonnichsen
    Super Member
    • Total Posts : 330
    • Reward points : 0
    • Joined: 2009/08/13 11:00:11
    • Location: 0
    • Status: offline
    Re: long double variables not printing properly after subroutine pass 2021/01/19 12:21:52 (permalink)
    0
    Crikey--
      I needed to pass pointers not the variables. 
      Works now.
    Thanks all
    Fritz
    #5
    ric
    Super Member
    • Total Posts : 29870
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: long double variables not printing properly after subroutine pass 2021/01/19 12:53:05 (permalink)
    0
    Or change the function to actually return a value, rather than accept a parameter, and call it as
    vr = getvar();

     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #6
    dan1138
    Super Member
    • Total Posts : 4242
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: long double variables not printing properly after subroutine pass 2021/01/19 13:28:07 (permalink)
    +1 (1)
    fsonnichsen
    Works now.

    Are you sure about that?
     
    Remember to decorate long double literal constants correctly to get what you need.
     
     

    Attached Image(s)

    #7
    fsonnichsen
    Super Member
    • Total Posts : 330
    • Reward points : 0
    • Joined: 2009/08/13 11:00:11
    • Location: 0
    • Status: offline
    Re: long double variables not printing properly after subroutine pass 2021/01/19 14:27:30 (permalink)
    0
    Not sure what you are referring to Dan. What you posted was similar to the old code.
    The problem was that C does not pass a variable copy "up" to the caller--so I had to pass the pointer (*vr).  I should have remembered that.
     
     I always get all these languages mixed up but I think C is more standard in that way. I think MATLAB and R for example does not do this but  always have to look them up.
     
    Thanks
    fritz
    #8
    ric
    Super Member
    • Total Posts : 29870
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: long double variables not printing properly after subroutine pass 2021/01/19 14:29:19 (permalink)
    0
    Dan was referring to a different issue, the difference between
    12345.678
    and
    12345.678L
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #9
    1and0
    Access is Denied
    • Total Posts : 12086
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: long double variables not printing properly after subroutine pass 2021/01/19 18:15:26 (permalink)
    +1 (1)
    Floating point numbers are not always accurate and generally do not have an exact binary representation. In other words, floating point values are finite precision approximations of real numbers.

    The closest floating point representation for the 32-bit float 12345.678 is
    12345.677734375 (0x4640E6B6),
     
    and the closest floating point representation for the 64-bit long double 12345.678L is
    12345.677999999999883584678173065185546875 (0x40C81CD6C8B43958).
    #10
    fsonnichsen
    Super Member
    • Total Posts : 330
    • Reward points : 0
    • Joined: 2009/08/13 11:00:11
    • Location: 0
    • Status: offline
    Re: long double variables not printing properly after subroutine pass 2021/01/20 06:28:49 (permalink)
    0
    Thank you for the comments. The use--or not-  of "L" in my example was strictly by way of example and not in the real code. I believe that most compilers will implicitly down-convert the long in such cases. That did not matter for the example.
        In reality I am dealing with a 24 bit ADC based RTD sensor that delivers its data as binary from the ADC. Doing some manipulations on the output can lead to some large numbers and converting to long and not worrying about it is a quick convenience since the final result is reduced to only 2 decimal places. All this is of course calibrated against a resistor that is somewhat inaccurate. So the round-off and error from manipulations is not important here. If precision to several decimals is a concern a lot can be handled a-priori by choosing the order of arithmetic operations.
      As you point out it must be addressed in some cases. When I was looping millions of times for crystal lattice calculations I had to put the lowest value terms first in the loop and hand calculate some terms to understand what was going on. In those cases your comments about the true meaning of float is very important. I recall 45 years ago having to branch out of Fortran and do arithmetic in assembler to be sure that round-off error was not a concern. Yuk. With the modern languages like PERL and PYTHON and am never confident in what conversions are going on in their "type-free" variables--probably partly my laziness. But there is comfort in knowing what C variables are in situations like you cite. 
     
    Thanks!
    Fritz
    #11
    Jump to:
    © 2021 APG vNext Commercial Version 4.5