Skip to content

Floating Point Conversions #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sticilface opened this issue Apr 1, 2015 · 3 comments
Closed

Floating Point Conversions #27

sticilface opened this issue Apr 1, 2015 · 3 comments

Comments

@sticilface
Copy link
Contributor

I need to convert numbers entered in a browser (Strings) or sent by MQTT (char array) to a floating point number (double).

I've been trying and failing various ways, but I'm pretty new to C (only since i got hold of the beta for this) so i've failed.

Igrr has said that atof has not been implemented ... this is what is in core_esp8266_noniso.c

float atof_internal(const char* s)
{
  float result = 0;
  return result;
}

atol does work but I'd kinda like the decimals... ( i know i can just enter an integer and then divide...)

Does anyone have any ideas... or fancy giving me a quick patch for this.. I'd try.... infact i spent a few hours on it.. but I'm so new to arrays, pointers, and all this that i just fail....

Cheers

A

@sticilface sticilface changed the title Floating Point Converstions Floating Point Conversions Apr 1, 2015
@igrr
Copy link
Member

igrr commented Apr 1, 2015

You can try this:

double os_atof(const char* s)
{
    double rez = 0, fact = 1;
    while (*s && (*s == ' ' || *s == '\t' || *s == '\r' || *s == '\n'))
        s++;
    if (*s == 0)
        return 0; // can't read

    if (*s == '-')
    {
        s++;
        fact = -1;
    };
    for (int point_seen = 0; *s; s++)
    {
        if (*s == '.')
        {
            point_seen = 1;
            continue;
        };
        int d = *s - '0';
        if (d >= 0 && d <= 9)
        {
            if (point_seen)
                fact /= 10.0;
            rez = rez * 10.0f + (double) d;
        };
    };
    return rez * fact;
}

source

I'm not 100% positive this is LGPL though.

@igrr
Copy link
Member

igrr commented Apr 1, 2015

Yeah, and you obviously need to change the function name and change double to float.

@sticilface
Copy link
Contributor Author

Thanks for the speedy reply.. If I cut and paste that function straight into my sketch, it works perfectly.

I tried what you suggested, changed the name of the function to atof_internal, changed return type to float, -/+ changing internal types to float, and it does not compile...

/Users/amelvin/Downloads/Arduino-espfinal.app/Contents/Java/hardware/tools/esp8266/xtensa-lx106-elf/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/bin/ld: MQTT-Melvide_-_v1.0.2.cpp.elf section .text' will not fit in regioniram1_0_seg'
/Users/amelvin/Downloads/Arduino-espfinal.app/Contents/Java/hardware/tools/esp8266/xtensa-lx106-elf/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/lib/libc.a(lib_a-mallocr.o):(.literal+0x1c): undefined reference to _sbrk_r' /Users/amelvin/Downloads/Arduino-espfinal.app/Contents/Java/hardware/tools/esp8266/xtensa-lx106-elf/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/lib/libc.a(lib_a-mallocr.o): In functionmalloc_extend_top':
mallocr.c:(.text+0x5e): undefined reference to _sbrk_r' mallocr.c:(.text+0x127): undefined reference to_sbrk_r'
/Users/amelvin/Downloads/Arduino-espfinal.app/Contents/Java/hardware/tools/esp8266/xtensa-lx106-elf/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/lib/libc.a(lib_a-freer.o): In function _malloc_trim_r': mallocr.c:(.text+0x366): undefined reference to_sbrk_r'
mallocr.c:(.text+0x38f): undefined reference to _sbrk_r' /Users/amelvin/Downloads/Arduino-espfinal.app/Contents/Java/hardware/tools/esp8266/xtensa-lx106-elf/bin/../lib/gcc/xtensa-lx106-elf/4.8.2/../../../../xtensa-lx106-elf/lib/libc.a(lib_a-freer.o):mallocr.c:(.text+0x3a0): more undefined references to_sbrk_r' follow
collect2: error: ld returned 1 exit status
Error compiling.

its out old friend sbrk...

Im happy though as the function works just fine inserted into the sketch. cheers for your help

@igrr igrr closed this as completed in eeabbf4 Apr 2, 2015
igrr pushed a commit that referenced this issue May 19, 2015
igrr added a commit that referenced this issue Oct 29, 2015
igrr pushed a commit that referenced this issue Oct 29, 2015
madpilot pushed a commit to madpilot/Arduino that referenced this issue Jul 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants