0

I have problems with Atof function. I am trying to convert string to float but it is not giving any error when I try in Coocox software in Debug section, Output is not showing anything. I tried two functions Atoi and Atof. When I use Atoi there is no output.When I use Atof The program starting restart. I put stdlib.h definition for atof in here.But it is atoff for float value in here.I tried same code in Dev C++ in C it is working very well. Other things I use without working Atof but this time again the program is restarting. This is working on Dev C. But not in Coocox. How can I solve the problem? There is only difference atoff! What can it be related? I used stdlib.h and there is no error in compilation!

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

int main ()
{
    float c;
    int b;

    char *array[1] = {"52.43525"};

    b=(atoi(array[0])/100);
    c=((atof(array[0]))/100.0)*100.0;
    c/=60;
    c+=b;

    printf ("%f\n", c);

    return 0;
}

-----stdlib.h----
double  _EXFUN(atof,(const char *__nptr));
#if __MISC_VISIBLE
float   _EXFUN(atoff,(const char *__nptr));
#endif
int _EXFUN(atoi,(const char *__nptr));
int _EXFUN(_atoi_r,(struct _reent *, const char *__nptr));
long    _EXFUN(atol,(const char *__nptr));
long    _EXFUN(_atol_r,(struct _reent *, const char *__nptr));
------------------------------------
Clifford
  • 88,407
  • 13
  • 85
  • 165
Nobody
  • 79
  • 13
  • If it is truly `atof()` that is not working, you should simplify the code to be sure the problem is specifically that. – Clifford Sep 17 '17 at 14:14
  • When I remove atof() everything is working very well. There is no problem! – Nobody Sep 17 '17 at 14:22
  • Curious, same problem if `float c` changed to `double c`? – chux - Reinstate Monica Sep 17 '17 at 14:27
  • 1
    Note that `(atoi(array[0]) / 100)` evaluates to 0. – chux - Reinstate Monica Sep 17 '17 at 14:29
  • I tried atof (double format) again same! – Nobody Sep 17 '17 at 14:35
  • Some compilers see all this code and convert it to `puts("0.873921");`, negating any need for FP libraries. Suggest making the code not so deterministic (e.g. add user input) and check that the compilation is including FP support. – chux - Reinstate Monica Sep 17 '17 at 14:35
  • That is not what I meant by "simplify". If you just atof a string literal and output it without the arithmetic expressions in order to exclude them. – Clifford Sep 17 '17 at 14:58
  • @Nobody Your code, as posted, works perfectly for me, and I don't see anything wrong with it (besides a whole school of red herrings). Suggest you try `"char *s = "52.43525"; c = atof(s)";`, or even `c = atof("52.43525");`, to try to narrow your problem down. – Steve Summit Sep 17 '17 at 14:59
  • @chux : the simplest way to force the compiler not to optimise away the expressions is to declare the variables volatile. – Clifford Sep 17 '17 at 14:59
  • @Clifford Good idea. – chux - Reinstate Monica Sep 17 '17 at 15:02
  • Iam using Cortex M3 and i added -mfloat-abi=softfb;-mfloat-abi=hard; Other thing,I read comment about hardware "There isn't any floating-point hardware in Cortex-M3, but support for C float and double are provided in software by most compilers/libraries." Can hardware effect? – Nobody Sep 17 '17 at 15:05
  • hard to believe optimalisation with function changing. What compiler make such optuimalisation? – Jacek Cz Sep 17 '17 at 15:06
  • "char *s = "52.43525"; c = atof(s)";, or even c = atof("52.43525"); They were not working because I tried before. – Nobody Sep 17 '17 at 15:08
  • Try `char s[] = "52.43525";`. All other forms result in a read-only literal and the library could choke on that. – Paul Ogilvie Sep 17 '17 at 15:11
  • @ Paul Ogilvie char s[] = "52.43525" I has just tried which is restarting again in Debug mode! – Nobody Sep 17 '17 at 15:18
  • #define RCC_CSR_SFTRSTF ((uint32_t)0x10000000) /*!< Software Reset flag */ --_RCC_CSR-- When I look reset reason ,these are – Nobody Sep 17 '17 at 15:21

1 Answers1

1

after correcting all the compiler warnings, this was the resulting code:

Note: since the array feature was not used, I changed it to a simple pointer. This made no difference int the output.

#include <stdio.h>   // printf()
//#include <string.h> -- contents not used
#include <stdlib.h>  // atoi(), atof()

int main ()
{
    float c;
    int b;

    char *array = {"52.43525"};

    b =  atoi(array);
    c =  ( (float)( atof(array) ) / 100.0f ) * 100.0f;
    c /= 60.0f;
    c += (float)b;

    printf ("%f\n", c);

    return 0;
}

running the program resulted in:

52.873920

So if your compiler is not finding atof() it is a problem with the compiler.

user3629249
  • 16,402
  • 1
  • 16
  • 17
  • it is restarting again.If the problem is compiler, What can I do?Which compiler link should I add? – Nobody Sep 17 '17 at 16:40