avr-chat
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-chat] Bug in atof shipped with avr-libc?


From: Joerg Wunsch
Subject: Re: [avr-chat] Bug in atof shipped with avr-libc?
Date: Fri, 25 Feb 2011 23:56:21 +0100 (MET)

<address@hidden> wrote:

> An example of code that demonstrates the behaviour is below:
> 
> float atof_test(void){
>       static char buf[10];
>       char* pbuf = buf;
> 
>       buf[0] = '-';
>       buf[1] = '0';
>       buf[2] = '.';
>       buf[3] = '3';
>       buf[4] = '3';
>       buf[5] = '\0';
> 
>       return atof(pbuf);
> }

This is not an example of code (that can be used to reproduce the
problem) but just a function -- and even written very complicated.
Just:

float atof_test(void) {
  return atof("-0.33");
}

would do the same ...  Anyway, I put the following lines into a
compilable file:

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

float atof_test(void){
        static char buf[10];
        char* pbuf = buf;
        buf[0] = '-';
        buf[1] = '0';
        buf[2] = '.';
        buf[3] = '3';
        buf[4] = '3';
        buf[5] = '\0';
        return atof(pbuf);
}


char buffer[20];
int
main(void)
{
        sprintf(buffer, "atof result: %f\n", (double)atof_test());

        return 42;
}

compiled it using:

avr-gcc -ffreestanding -Os -mmcu=atmega128 atoftest.c -Wl,-u,vfprintf 
-lprintf_flt -lm -o atoftest.elf

(The -ffreestanding is only there to force the compiler into using the
actual library functions for everything, rather than implementing
"shortcuts".)

and ran it through the simulator.  It yields the expected result:

Internal SRAM Memory Dump:
0100 : 61 74 6f 66 20 72 65 73 75 6c 74 3a 20 25 66 0a 
0110 : 00 00 61 74 6f 66 20 72 65 73 75 6c 74 3a 20 2d 
0120 : 30 2e 33 33 30 30 30 30 0a 00 33 00 00 00 00 00 
0130 : 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 

The first string is the copy of the format string, the
second one is the result of the sprintf(), namely:

"atof result: -0.330000\n"

Btw., atof() is a reduced version of strtod(), which is regularly
being regression tested as part of the avr-libc testsuite.
-- 
cheers, J"org               .-.-.   --... ...--   -.. .  DL8DTL

http://www.sax.de/~joerg/                        NIC: JW11-RIPE
Never trust an operating system you don't have sources for. ;-)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]