Hot!STRTOF why the result is not correct?

Page: 12 > Showing page 1 of 2 - Powered by APG vNext Trial
Author
giannic
New Member
  • Total Posts : 14
  • Reward points : 0
  • Joined: 2018/01/08 04:41:54
  • Location: Venice Italy
  • Status: offline
2018/05/17 03:48:50 (permalink)
0

STRTOF why the result is not correct?

float x;
uint8_t buffer[]={"123.456"};
x=strtof(buffer,NULL);
result is 123.45600
 
float x;
uint8_t buffer[]={"123.456f2"};   //add "f2"
x=strtof(buffer,NULL);
result is 12345.6000
 
the conversion should stop after the last numeric character....
the letter "f" seems to multiply / divide the result
#1

30 Replies Related Threads

    qɥb
    Monolothic Member
    • Total Posts : 2498
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: offline
    Re: STRTOF why the result is not correct? 2018/05/17 04:01:57 (permalink)
    5 (1)
    It would appear that "f" is being treated the same as "e".
    i.e. 123.456e2 = 123.456 * 10^2 = 12345.6
     

    PicForum "it just works"
    #2
    giannic
    New Member
    • Total Posts : 14
    • Reward points : 0
    • Joined: 2018/01/08 04:41:54
    • Location: Venice Italy
    • Status: offline
    Re: STRTOF why the result is not correct? 2018/05/17 04:16:41 (permalink)
    0
    why this?
    so it's an unreliable function
    there must be a logical explanation ....
    #3
    rodims
    Super Member
    • Total Posts : 1262
    • Reward points : 0
    • Joined: 2009/02/10 11:08:59
    • Location: 51.9627, 7.6262
    • Status: online
    Re: STRTOF why the result is not correct? 2018/05/17 04:35:08 (permalink)
    0
    You didn't even specify your compiler and version or which library you are using.
    This is not a general problem, it's currently your problem.
    E.g. GNU GCC v7.1.1 ignores the 'f'
     
     
    #4
    giannic
    New Member
    • Total Posts : 14
    • Reward points : 0
    • Joined: 2018/01/08 04:41:54
    • Location: Venice Italy
    • Status: offline
    Re: STRTOF why the result is not correct? 2018/05/17 04:39:13 (permalink)
    0
    Sorry......
    I'm using mplab X v4.15
    xc32 v 1.42
    Harmony 2.01
    and pic32mz chip
     
    #5
    rodims
    Super Member
    • Total Posts : 1262
    • Reward points : 0
    • Joined: 2009/02/10 11:08:59
    • Location: 51.9627, 7.6262
    • Status: online
    Re: STRTOF why the result is not correct? 2018/05/17 04:56:06 (permalink)
    0
    Tested with XC32 v 1.44 and MpLabX simulator (choosing some MZ  PIC32MZ2048ECG100)
    The result is 123.456 for
    float x;
    char buffer[]={"123.456f2"};   //add "f2"
    x=strtof(buffer,NULL);

    Simulator is not always the same, but currently I would suspect the problem somewhere else.
    Anyway, does it make sense to dive into that ? If you cannot exclude that your string contains an f character, you probably also cannot exclude that it contains an e character. Apparently you do not expect that to be an exponent. If not, then you cannot use strtof. What is the source of your data ?
    #6
    giannic
    New Member
    • Total Posts : 14
    • Reward points : 0
    • Joined: 2018/01/08 04:41:54
    • Location: Venice Italy
    • Status: offline
    Re: STRTOF why the result is not correct? 2018/05/17 05:04:23 (permalink)
    0
    I use a file txt for an robot position and I use  string with only x,y,z,s,f,n,m and numbers.
    I don't use the "e" or "E"  but only "f" and "F"
     
    for the rest it works perfectly and the problem remains that if strtof does not find a value to be converted it returns 0.0
     
    #7
    rodims
    Super Member
    • Total Posts : 1262
    • Reward points : 0
    • Joined: 2009/02/10 11:08:59
    • Location: 51.9627, 7.6262
    • Status: online
    Re: STRTOF why the result is not correct? 2018/05/17 05:20:12 (permalink)
    0
    So either
    a) use a different character in your files for f
    b) use a different function than strtof
    c) investigate, why you get a different behaviour than me (or other GNU compilers)
       The result may be that you cannot change that, so it depends how much time you want to invest.
     
    I can't tell how to find the source for the library, that would show the implementation.
    You could try to reproduce my result.  Note that I used
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    And for me it does not make a difference whether I choose Legacy Lib or not.
     
    Next create a fresh project with your real PIC but same simple code.
    If you then still get the "correct" result 123.456 then you need to find the difference to your project.
     
    edit:
    ...and the problem remains that if strtof does not find a value to be converted it returns 0.0

    That also could mean that you do not want to use strtof.
    post edited by rodims - 2018/05/17 05:29:02
    #8
    giannic
    New Member
    • Total Posts : 14
    • Reward points : 0
    • Joined: 2018/01/08 04:41:54
    • Location: Venice Italy
    • Status: offline
    Re: STRTOF why the result is not correct? 2018/05/17 05:24:46 (permalink)
    0
    this situation worries me because the program must be reliable and I can not afford this kind of problem ...
    Meanwhile, I try to do some counter-tests to find differences between the various compilers
    #9
    giannic
    New Member
    • Total Posts : 14
    • Reward points : 0
    • Joined: 2018/01/08 04:41:54
    • Location: Venice Italy
    • Status: offline
    Re: STRTOF why the result is not correct? 2018/05/17 05:47:08 (permalink)
    0
    same problem with mplab v4.15
    xc32 v1.44 (x86) and v2.05 (x86)
    #10
    JorgeF
    Super Member
    • Total Posts : 2969
    • Reward points : 0
    • Joined: 2011/07/09 11:56:58
    • Location: PT/EU @ Third rock from the Sun
    • Status: offline
    Re: STRTOF why the result is not correct? 2018/05/17 05:55:10 (permalink)
    0
    Hi
     
    If you are worried about reliability, do it yourself.
    It looks like you are trying to feed a string of tokens with numeric arguments to some system.
    A better way to do it is to set up a parser to find the tokens and isolate the numeric values.
    Then you can the feed clean strings to the convertion to numeric.
     
    In general I never trust "unknown code" (as in generic libraries) to take care of critical stuff.
    Particularly to take care of situations that can be both correct or error, a single character in the middle of a numeric string, depending on a very specific context.
     
    Best regards
    Jorge
     
    EDIT: Typo
    post edited by JorgeF - 2018/05/17 05:59:48
    #11
    giannic
    New Member
    • Total Posts : 14
    • Reward points : 0
    • Joined: 2018/01/08 04:41:54
    • Location: Venice Italy
    • Status: offline
    Re: STRTOF why the result is not correct? 2018/05/17 05:58:07 (permalink)
    0
    you are right
    #12
    rodims
    Super Member
    • Total Posts : 1262
    • Reward points : 0
    • Joined: 2009/02/10 11:08:59
    • Location: 51.9627, 7.6262
    • Status: online
    Re: STRTOF why the result is not correct? 2018/05/17 06:11:46 (permalink)
    0
    Sounds that you use 1.44 and 2.05 instead of 1.42,  but still use your current project.
    It's not likely that the difference in version would explain your problem.
     
     
    #13
    giannic
    New Member
    • Total Posts : 14
    • Reward points : 0
    • Joined: 2018/01/08 04:41:54
    • Location: Venice Italy
    • Status: offline
    Re: STRTOF why the result is not correct? 2018/05/17 06:34:48 (permalink)
    0
    I tried in every way and creating new projects for each test.
    I also tried to change between mac and w10
    I did the tests using the program simulator and displaying the variable with watch
    the problem is always the same....
    with other compilers xcode ,code blocks no problem
    #14
    1and0
    Access is Denied
    • Total Posts : 7855
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: STRTOF why the result is not correct? 2018/05/17 07:56:11 (permalink)
    0
    There is the probability that the simulator is displaying the value wrong.
    #15
    giannic
    New Member
    • Total Posts : 14
    • Reward points : 0
    • Joined: 2018/01/08 04:41:54
    • Location: Venice Italy
    • Status: offline
    Re: STRTOF why the result is not correct? 2018/05/17 09:02:29 (permalink)
    0
    I think the most convincing answer is that "f" is interpreted as "e"....
    can I try to use sscanf as an alternative to strtof?
    #16
    JorgeF
    Super Member
    • Total Posts : 2969
    • Reward points : 0
    • Joined: 2011/07/09 11:56:58
    • Location: PT/EU @ Third rock from the Sun
    • Status: offline
    Re: STRTOF why the result is not correct? 2018/05/17 09:25:17 (permalink)
    0
    Hi
    giannic
    I think the most convincing answer is that "f" is interpreted as "e"....

    I guess its correct, I've been scratching my head trying to remember in what obscure context, besides the ususal "printf"/"scanf" format string, "f" might be understood as a number formater. But I seem to have a faint memory of something but can't pinpoint it.
    giannic
    can I try to use sscanf as an alternative to strtof?

    In theory thats the tool for the job but.
    In scanf, yes, "f" has a meaning. And also a lot of other letters, you would have to review your tokens to avoid any clash with the scanf format string.
     
    As I said before, a simple string parser looks a lot simpler than working around all the peculiarities of "scanf", "strtof" and others.
     
     
    Best regards
    Jorge
     
    post edited by JorgeF - 2018/05/17 09:29:48
    #17
    giannic
    New Member
    • Total Posts : 14
    • Reward points : 0
    • Joined: 2018/01/08 04:41:54
    • Location: Venice Italy
    • Status: offline
    Re: STRTOF why the result is not correct? 2018/05/17 09:50:57 (permalink)
    0
    "123.456" =123.456
    "123.456f2" =12345.6000
    "123.456f-2" =1.23456000
    "123.456f-5" = 0.00123456
    same results as "e"....
    the strtof command instructions are clear and seemingly easy to use.
    Are developers aware of these situations?
    #18
    JorgeF
    Super Member
    • Total Posts : 2969
    • Reward points : 0
    • Joined: 2011/07/09 11:56:58
    • Location: PT/EU @ Third rock from the Sun
    • Status: offline
    Re: STRTOF why the result is not correct? 2018/05/17 11:25:07 (permalink)
    4 (1)
    Hi
    giannic
    Are developers aware of these situations?

    I don't know if all are aware of this specific case, I wasn't.
    But I'm aware that generic lib functions, frameworks and alike, are, in general, full of these small obscure details.
    As they intended to be a "one size fits all" they don't perfectly fit none.
     
    Anyhow, after being bitten once, and with the suspect that this behaviour can change with compiler versions.
    I'm would go straight to my own version.
    After all it takes less time to write and test something like this than to find the solution to the puzzle.

    #include <xc.h>
    #include <string.h>
     
    #define ARGUMENT_STR_MAXLEN 15
     
    char TestStr[] = "x123.450y456.780z789.120f147.250n258.360m369.010";
    char TokensStr[] = "xXyYzZsSfFnNmM"; // accept uppercase and lowercase
     
    char GetNextCoord(char *InputStr, char* Tokens, float* Value)
    {
    static char* InputPtr;
    char Token;
    char ValStr[ARGUMENT_STR_MAXLEN+1];
    char *ValStrPtr = ValStr;

    if(NULL != InputStr)
    {
    // New string to be parsed - Reset static variables
    InputPtr = InputStr;
    }
     
    if('\0' == *InputPtr)
    {
    // End of string
    return '\0';
    }

    // Fetch next valid token and its argument (value))
    if(NULL != strchr(Tokens, *InputPtr))
    {
    // Found a valid token
    Token = *InputPtr++;
    // Fetch the associated value
    while(('0' <= *InputPtr && '9' >= *InputPtr) || ('.' == *InputPtr))
    {
    // Get digit or "."
    *ValStrPtr++ = *InputPtr++;
    }
    // Found non number character leave the pointer there and return the result
    ValStrPtr = '\0';
    *Value = strtof(ValStr, NULL);
    return Token;
    }
    // Not a valid token
    return '\0';
    }
     
     
    void main(void)
    {
    float CoordArg;
    char CurrentCoord = GetNextCoord(TestStr, TokensStr, &CoordArg);
    _nop(); // breakpoint to inspect first token
    while(CurrentCoord != '\0')
    {
    CurrentCoord = GetNextCoord(NULL, TokensStr, &CoordArg);
    _nop(); // Breakpoint to inspect second and following tokens
    }
    _nop(); // breakpoint end of it
    while(1);
    }

    This is the result of half an hour coding and testing.
    Of course, reporting it and the exchange of experience like in this thread is good, so we are all on alert.
    But don't put your project on hold for it. Anyhow, things can change at the next compiler version....
     
    Just my 2 cents of it.
     
    Best regards
    Jorge
     
    post edited by JorgeF - 2018/05/17 11:36:42
    #19
    Jim Nickerson
    User 452 _
    • Total Posts : 4650
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: STRTOF why the result is not correct? 2018/05/17 11:42:39 (permalink)
    0
    Maybe you could open a support case and ensure the developers are aware of what you found so others in the future might benefit.
    #20
    Page: 12 > Showing page 1 of 2 - Powered by APG vNext Trial
    Jump to:
    © 2018 APG vNext Trial Version 4.5