atof(“0”) returns 2 in float variable












-2















I write c embedded on STM32F437VI. At some point, I need to parse some strings that contain numbers, as float numbers. I use atof and it works always with the correct result, except for this one weird case. If the string is 0 or 0.0 it gives 2.



I have included stdlib.h and even tried (float)atof() to typecast, but for some weird reason, the float variable has always the value 2 from the atof("0") operation and the double value has the correct 0.
Why is this happening? Thank you all in advance.



#include "stdio.h"
#include "stdlib.h"

int main(void)
{
char test = "0";
float val1;
double val2;

val1 = atof(test);
val2 = atof(test);

return 0;
}


I expect the obvious result of 0 to the float variable as well, but it keeps getting the fixed 2 value.



UPDATE:
This code section is part of a much bigger project and there is no point in elaborating on the project. I have custom Makefile with LDFLAG option



"-mfloat-abi=hard -mfpu=fpv4-sp-d16 -u _printf_float".


Could this affect this issue?



As the MCV example is concerned, in main.c I have the above code section and I got the results mentioned.
Can anybody think of any reason atof() behaves in this way?
Of course I have used online c compiler as well with the exact same code and the result is of course 0. I guess if something was very very wrong with the stdlib library, then atof() would not work for all the other cases as well. But it fails only for "0" and only with the result 2 assigned to the float variable.



I watch the variables realtime with Ozone debugger software.
Could the reason be the floating point implementation on STM32F4 mcu used? A missing parameter in the custom Makefile or something like that?










share|improve this question




















  • 3





    With the code you show, it's going to be impossible for us to replicate your problem. Please try to create a Minimal, Complete, and Verifiable example to show us.

    – Some programmer dude
    2 days ago













  • Does your toolchain have configurable floating point support? Also, some toolchains allow disabling more complex features of scanf library functions, and may also affect ato* and strto* functions.

    – user694733
    2 days ago








  • 2





    The answer to "my code is to big to show here" is MCVE. The answer to "too secret" is MCVE. If you show only the code lines in which you guess the problem is, but where you have not found it yourself, then in alll likelyhood, the error is somewhere else. Make an MCVE to make sure that you and we are looking at the same thing. If we cannot reproduce it, then it is time to talk about compiler versions and other attributes of your environment. Read the MCVE link provided by Some and apply the concept to your code. Asking about perceived misbehaviour without an MCVE is off-topic.

    – Yunnosch
    2 days ago






  • 1





    If what you show is enough for you to replicate the problem for you, then put it into a main function, add header files, and make it complete. Then tell us that it's the minimal code to replicate the problem (and of course make sure that you really can replicate it using the program), and add all details you can about build and run settings and environments.

    – Some programmer dude
    2 days ago








  • 1





    Your new code does not use the value returned by atof. The compiler could optimize variable allocation. It might happen that the debugger does not show real value. You should try to print the results and see what you get.

    – Gerhardh
    2 days ago
















-2















I write c embedded on STM32F437VI. At some point, I need to parse some strings that contain numbers, as float numbers. I use atof and it works always with the correct result, except for this one weird case. If the string is 0 or 0.0 it gives 2.



I have included stdlib.h and even tried (float)atof() to typecast, but for some weird reason, the float variable has always the value 2 from the atof("0") operation and the double value has the correct 0.
Why is this happening? Thank you all in advance.



#include "stdio.h"
#include "stdlib.h"

int main(void)
{
char test = "0";
float val1;
double val2;

val1 = atof(test);
val2 = atof(test);

return 0;
}


I expect the obvious result of 0 to the float variable as well, but it keeps getting the fixed 2 value.



UPDATE:
This code section is part of a much bigger project and there is no point in elaborating on the project. I have custom Makefile with LDFLAG option



"-mfloat-abi=hard -mfpu=fpv4-sp-d16 -u _printf_float".


Could this affect this issue?



As the MCV example is concerned, in main.c I have the above code section and I got the results mentioned.
Can anybody think of any reason atof() behaves in this way?
Of course I have used online c compiler as well with the exact same code and the result is of course 0. I guess if something was very very wrong with the stdlib library, then atof() would not work for all the other cases as well. But it fails only for "0" and only with the result 2 assigned to the float variable.



I watch the variables realtime with Ozone debugger software.
Could the reason be the floating point implementation on STM32F4 mcu used? A missing parameter in the custom Makefile or something like that?










share|improve this question




















  • 3





    With the code you show, it's going to be impossible for us to replicate your problem. Please try to create a Minimal, Complete, and Verifiable example to show us.

    – Some programmer dude
    2 days ago













  • Does your toolchain have configurable floating point support? Also, some toolchains allow disabling more complex features of scanf library functions, and may also affect ato* and strto* functions.

    – user694733
    2 days ago








  • 2





    The answer to "my code is to big to show here" is MCVE. The answer to "too secret" is MCVE. If you show only the code lines in which you guess the problem is, but where you have not found it yourself, then in alll likelyhood, the error is somewhere else. Make an MCVE to make sure that you and we are looking at the same thing. If we cannot reproduce it, then it is time to talk about compiler versions and other attributes of your environment. Read the MCVE link provided by Some and apply the concept to your code. Asking about perceived misbehaviour without an MCVE is off-topic.

    – Yunnosch
    2 days ago






  • 1





    If what you show is enough for you to replicate the problem for you, then put it into a main function, add header files, and make it complete. Then tell us that it's the minimal code to replicate the problem (and of course make sure that you really can replicate it using the program), and add all details you can about build and run settings and environments.

    – Some programmer dude
    2 days ago








  • 1





    Your new code does not use the value returned by atof. The compiler could optimize variable allocation. It might happen that the debugger does not show real value. You should try to print the results and see what you get.

    – Gerhardh
    2 days ago














-2












-2








-2








I write c embedded on STM32F437VI. At some point, I need to parse some strings that contain numbers, as float numbers. I use atof and it works always with the correct result, except for this one weird case. If the string is 0 or 0.0 it gives 2.



I have included stdlib.h and even tried (float)atof() to typecast, but for some weird reason, the float variable has always the value 2 from the atof("0") operation and the double value has the correct 0.
Why is this happening? Thank you all in advance.



#include "stdio.h"
#include "stdlib.h"

int main(void)
{
char test = "0";
float val1;
double val2;

val1 = atof(test);
val2 = atof(test);

return 0;
}


I expect the obvious result of 0 to the float variable as well, but it keeps getting the fixed 2 value.



UPDATE:
This code section is part of a much bigger project and there is no point in elaborating on the project. I have custom Makefile with LDFLAG option



"-mfloat-abi=hard -mfpu=fpv4-sp-d16 -u _printf_float".


Could this affect this issue?



As the MCV example is concerned, in main.c I have the above code section and I got the results mentioned.
Can anybody think of any reason atof() behaves in this way?
Of course I have used online c compiler as well with the exact same code and the result is of course 0. I guess if something was very very wrong with the stdlib library, then atof() would not work for all the other cases as well. But it fails only for "0" and only with the result 2 assigned to the float variable.



I watch the variables realtime with Ozone debugger software.
Could the reason be the floating point implementation on STM32F4 mcu used? A missing parameter in the custom Makefile or something like that?










share|improve this question
















I write c embedded on STM32F437VI. At some point, I need to parse some strings that contain numbers, as float numbers. I use atof and it works always with the correct result, except for this one weird case. If the string is 0 or 0.0 it gives 2.



I have included stdlib.h and even tried (float)atof() to typecast, but for some weird reason, the float variable has always the value 2 from the atof("0") operation and the double value has the correct 0.
Why is this happening? Thank you all in advance.



#include "stdio.h"
#include "stdlib.h"

int main(void)
{
char test = "0";
float val1;
double val2;

val1 = atof(test);
val2 = atof(test);

return 0;
}


I expect the obvious result of 0 to the float variable as well, but it keeps getting the fixed 2 value.



UPDATE:
This code section is part of a much bigger project and there is no point in elaborating on the project. I have custom Makefile with LDFLAG option



"-mfloat-abi=hard -mfpu=fpv4-sp-d16 -u _printf_float".


Could this affect this issue?



As the MCV example is concerned, in main.c I have the above code section and I got the results mentioned.
Can anybody think of any reason atof() behaves in this way?
Of course I have used online c compiler as well with the exact same code and the result is of course 0. I guess if something was very very wrong with the stdlib library, then atof() would not work for all the other cases as well. But it fails only for "0" and only with the result 2 assigned to the float variable.



I watch the variables realtime with Ozone debugger software.
Could the reason be the floating point implementation on STM32F4 mcu used? A missing parameter in the custom Makefile or something like that?







c embedded stm32f4 atof






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









Clifford

58.6k858125




58.6k858125










asked 2 days ago









BabisIBabisI

517




517








  • 3





    With the code you show, it's going to be impossible for us to replicate your problem. Please try to create a Minimal, Complete, and Verifiable example to show us.

    – Some programmer dude
    2 days ago













  • Does your toolchain have configurable floating point support? Also, some toolchains allow disabling more complex features of scanf library functions, and may also affect ato* and strto* functions.

    – user694733
    2 days ago








  • 2





    The answer to "my code is to big to show here" is MCVE. The answer to "too secret" is MCVE. If you show only the code lines in which you guess the problem is, but where you have not found it yourself, then in alll likelyhood, the error is somewhere else. Make an MCVE to make sure that you and we are looking at the same thing. If we cannot reproduce it, then it is time to talk about compiler versions and other attributes of your environment. Read the MCVE link provided by Some and apply the concept to your code. Asking about perceived misbehaviour without an MCVE is off-topic.

    – Yunnosch
    2 days ago






  • 1





    If what you show is enough for you to replicate the problem for you, then put it into a main function, add header files, and make it complete. Then tell us that it's the minimal code to replicate the problem (and of course make sure that you really can replicate it using the program), and add all details you can about build and run settings and environments.

    – Some programmer dude
    2 days ago








  • 1





    Your new code does not use the value returned by atof. The compiler could optimize variable allocation. It might happen that the debugger does not show real value. You should try to print the results and see what you get.

    – Gerhardh
    2 days ago














  • 3





    With the code you show, it's going to be impossible for us to replicate your problem. Please try to create a Minimal, Complete, and Verifiable example to show us.

    – Some programmer dude
    2 days ago













  • Does your toolchain have configurable floating point support? Also, some toolchains allow disabling more complex features of scanf library functions, and may also affect ato* and strto* functions.

    – user694733
    2 days ago








  • 2





    The answer to "my code is to big to show here" is MCVE. The answer to "too secret" is MCVE. If you show only the code lines in which you guess the problem is, but where you have not found it yourself, then in alll likelyhood, the error is somewhere else. Make an MCVE to make sure that you and we are looking at the same thing. If we cannot reproduce it, then it is time to talk about compiler versions and other attributes of your environment. Read the MCVE link provided by Some and apply the concept to your code. Asking about perceived misbehaviour without an MCVE is off-topic.

    – Yunnosch
    2 days ago






  • 1





    If what you show is enough for you to replicate the problem for you, then put it into a main function, add header files, and make it complete. Then tell us that it's the minimal code to replicate the problem (and of course make sure that you really can replicate it using the program), and add all details you can about build and run settings and environments.

    – Some programmer dude
    2 days ago








  • 1





    Your new code does not use the value returned by atof. The compiler could optimize variable allocation. It might happen that the debugger does not show real value. You should try to print the results and see what you get.

    – Gerhardh
    2 days ago








3




3





With the code you show, it's going to be impossible for us to replicate your problem. Please try to create a Minimal, Complete, and Verifiable example to show us.

– Some programmer dude
2 days ago







With the code you show, it's going to be impossible for us to replicate your problem. Please try to create a Minimal, Complete, and Verifiable example to show us.

– Some programmer dude
2 days ago















Does your toolchain have configurable floating point support? Also, some toolchains allow disabling more complex features of scanf library functions, and may also affect ato* and strto* functions.

– user694733
2 days ago







Does your toolchain have configurable floating point support? Also, some toolchains allow disabling more complex features of scanf library functions, and may also affect ato* and strto* functions.

– user694733
2 days ago






2




2





The answer to "my code is to big to show here" is MCVE. The answer to "too secret" is MCVE. If you show only the code lines in which you guess the problem is, but where you have not found it yourself, then in alll likelyhood, the error is somewhere else. Make an MCVE to make sure that you and we are looking at the same thing. If we cannot reproduce it, then it is time to talk about compiler versions and other attributes of your environment. Read the MCVE link provided by Some and apply the concept to your code. Asking about perceived misbehaviour without an MCVE is off-topic.

– Yunnosch
2 days ago





The answer to "my code is to big to show here" is MCVE. The answer to "too secret" is MCVE. If you show only the code lines in which you guess the problem is, but where you have not found it yourself, then in alll likelyhood, the error is somewhere else. Make an MCVE to make sure that you and we are looking at the same thing. If we cannot reproduce it, then it is time to talk about compiler versions and other attributes of your environment. Read the MCVE link provided by Some and apply the concept to your code. Asking about perceived misbehaviour without an MCVE is off-topic.

– Yunnosch
2 days ago




1




1





If what you show is enough for you to replicate the problem for you, then put it into a main function, add header files, and make it complete. Then tell us that it's the minimal code to replicate the problem (and of course make sure that you really can replicate it using the program), and add all details you can about build and run settings and environments.

– Some programmer dude
2 days ago







If what you show is enough for you to replicate the problem for you, then put it into a main function, add header files, and make it complete. Then tell us that it's the minimal code to replicate the problem (and of course make sure that you really can replicate it using the program), and add all details you can about build and run settings and environments.

– Some programmer dude
2 days ago






1




1





Your new code does not use the value returned by atof. The compiler could optimize variable allocation. It might happen that the debugger does not show real value. You should try to print the results and see what you get.

– Gerhardh
2 days ago





Your new code does not use the value returned by atof. The compiler could optimize variable allocation. It might happen that the debugger does not show real value. You should try to print the results and see what you get.

– Gerhardh
2 days ago












2 Answers
2






active

oldest

votes


















3














First, your question lacks a Minimal, Complete, and Verifiable example.



#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char test = "0";
float val1;
double val2;

val1 = atof(test);
printf("%fn", val1);

val2 = atof(test);
printf("%fn", val2);
}


Output:



0.000000
0.000000


<°)))<()



Or your standard library implementation is fubar.






share|improve this answer

































    1














    It looks like the cause of your problem is in the updated question text:




    UPDATE: This code section is part of a much bigger project and there is no point in elaborating on the project. I have custom Makefile with LDFLAG option



    "-mfloat-abi=hard -mfpu=fpv4-sp-d16 -u _printf_float".



    Assuming these options are changes vs the defaults, what you're doing is telling the compiler to generate code for a different ABI from your toolchain's library ecosystem. The code generated for main expects the result of atof in a floating point register, but atof is using the standard ABI, which passes floating point arguments and return values in general-purpose registers. Thus, main is just reading whatever junk happens to be left in the floating point registers used for return values.



    See if your problem goes away if you remove -mfloat-abi=hard. If so, you've probably found your problem. You need to either build a toolchain and libraries for the hardfloat ABI, or stick with the default non-hardfloat calling convention.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54252773%2fatof0-returns-2-in-float-variable%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      First, your question lacks a Minimal, Complete, and Verifiable example.



      #include <stdio.h>
      #include <stdlib.h>

      int main(void)
      {
      char test = "0";
      float val1;
      double val2;

      val1 = atof(test);
      printf("%fn", val1);

      val2 = atof(test);
      printf("%fn", val2);
      }


      Output:



      0.000000
      0.000000


      <°)))<()



      Or your standard library implementation is fubar.






      share|improve this answer






























        3














        First, your question lacks a Minimal, Complete, and Verifiable example.



        #include <stdio.h>
        #include <stdlib.h>

        int main(void)
        {
        char test = "0";
        float val1;
        double val2;

        val1 = atof(test);
        printf("%fn", val1);

        val2 = atof(test);
        printf("%fn", val2);
        }


        Output:



        0.000000
        0.000000


        <°)))<()



        Or your standard library implementation is fubar.






        share|improve this answer




























          3












          3








          3







          First, your question lacks a Minimal, Complete, and Verifiable example.



          #include <stdio.h>
          #include <stdlib.h>

          int main(void)
          {
          char test = "0";
          float val1;
          double val2;

          val1 = atof(test);
          printf("%fn", val1);

          val2 = atof(test);
          printf("%fn", val2);
          }


          Output:



          0.000000
          0.000000


          <°)))<()



          Or your standard library implementation is fubar.






          share|improve this answer















          First, your question lacks a Minimal, Complete, and Verifiable example.



          #include <stdio.h>
          #include <stdlib.h>

          int main(void)
          {
          char test = "0";
          float val1;
          double val2;

          val1 = atof(test);
          printf("%fn", val1);

          val2 = atof(test);
          printf("%fn", val2);
          }


          Output:



          0.000000
          0.000000


          <°)))<()



          Or your standard library implementation is fubar.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 days ago

























          answered 2 days ago









          SwordfishSwordfish

          9,19511335




          9,19511335

























              1














              It looks like the cause of your problem is in the updated question text:




              UPDATE: This code section is part of a much bigger project and there is no point in elaborating on the project. I have custom Makefile with LDFLAG option



              "-mfloat-abi=hard -mfpu=fpv4-sp-d16 -u _printf_float".



              Assuming these options are changes vs the defaults, what you're doing is telling the compiler to generate code for a different ABI from your toolchain's library ecosystem. The code generated for main expects the result of atof in a floating point register, but atof is using the standard ABI, which passes floating point arguments and return values in general-purpose registers. Thus, main is just reading whatever junk happens to be left in the floating point registers used for return values.



              See if your problem goes away if you remove -mfloat-abi=hard. If so, you've probably found your problem. You need to either build a toolchain and libraries for the hardfloat ABI, or stick with the default non-hardfloat calling convention.






              share|improve this answer




























                1














                It looks like the cause of your problem is in the updated question text:




                UPDATE: This code section is part of a much bigger project and there is no point in elaborating on the project. I have custom Makefile with LDFLAG option



                "-mfloat-abi=hard -mfpu=fpv4-sp-d16 -u _printf_float".



                Assuming these options are changes vs the defaults, what you're doing is telling the compiler to generate code for a different ABI from your toolchain's library ecosystem. The code generated for main expects the result of atof in a floating point register, but atof is using the standard ABI, which passes floating point arguments and return values in general-purpose registers. Thus, main is just reading whatever junk happens to be left in the floating point registers used for return values.



                See if your problem goes away if you remove -mfloat-abi=hard. If so, you've probably found your problem. You need to either build a toolchain and libraries for the hardfloat ABI, or stick with the default non-hardfloat calling convention.






                share|improve this answer


























                  1












                  1








                  1







                  It looks like the cause of your problem is in the updated question text:




                  UPDATE: This code section is part of a much bigger project and there is no point in elaborating on the project. I have custom Makefile with LDFLAG option



                  "-mfloat-abi=hard -mfpu=fpv4-sp-d16 -u _printf_float".



                  Assuming these options are changes vs the defaults, what you're doing is telling the compiler to generate code for a different ABI from your toolchain's library ecosystem. The code generated for main expects the result of atof in a floating point register, but atof is using the standard ABI, which passes floating point arguments and return values in general-purpose registers. Thus, main is just reading whatever junk happens to be left in the floating point registers used for return values.



                  See if your problem goes away if you remove -mfloat-abi=hard. If so, you've probably found your problem. You need to either build a toolchain and libraries for the hardfloat ABI, or stick with the default non-hardfloat calling convention.






                  share|improve this answer













                  It looks like the cause of your problem is in the updated question text:




                  UPDATE: This code section is part of a much bigger project and there is no point in elaborating on the project. I have custom Makefile with LDFLAG option



                  "-mfloat-abi=hard -mfpu=fpv4-sp-d16 -u _printf_float".



                  Assuming these options are changes vs the defaults, what you're doing is telling the compiler to generate code for a different ABI from your toolchain's library ecosystem. The code generated for main expects the result of atof in a floating point register, but atof is using the standard ABI, which passes floating point arguments and return values in general-purpose registers. Thus, main is just reading whatever junk happens to be left in the floating point registers used for return values.



                  See if your problem goes away if you remove -mfloat-abi=hard. If so, you've probably found your problem. You need to either build a toolchain and libraries for the hardfloat ABI, or stick with the default non-hardfloat calling convention.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 days ago









                  R..R..

                  155k26258562




                  155k26258562






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54252773%2fatof0-returns-2-in-float-variable%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Callistus III

                      Plistias Cous

                      Index Sanctorum