Forum: Mikrocontroller und Digitale Elektronik Array --> float


von Sunset (Gast)


Lesenswert?

Hallo zusammen,

ich habe ein rieeeesen Problem.

Die Arrayvariable:
char a[] = {'-', '0', '.', '1', '2', '3',} // -0.123

muss ich in einen float-Wert convertieren.

Welche Möglichkeiten gibt es da? Hat jemand einen Ansatz für mich?

Vieln Dank   Sunset

von Martin Jangowski (Gast)


Lesenswert?

Ich würde das ganze in einen String verwandeln und dann mit scanf (o.ä)
zurückkonvertieren.

Martin

von Unbekannter (Gast)


Lesenswert?

NAME
       atof - convert a string to a double.

SYNOPSIS
       #include <stdlib.h>

       double atof(const char *nptr);

DESCRIPTION
       The  atof()  function  converts the initial portion of the
       string pointed to by nptr to double.  The behaviour is the
       same as

              strtod(nptr, (char **)NULL);

       except that atof() does not detect errors.

RETURN VALUE
       The converted value.

CONFORMING TO
       SVID 3, POSIX, BSD 4.3, ISO 9899

SEE ALSO
       atoi(3), atol(3), strtod(3), strtol(3), strtoul(3)

von Unbekannter (Gast)


Lesenswert?

NAME
       strtod, strtof, strtold - convert ASCII string to floating
       point number

SYNOPSIS
       #include <stdlib.h>

       double strtod(const char *nptr, char **endptr);
       float strtof(const char *nptr, char **endptr);
       long double strtold(const char *nptr, char **endptr);

DESCRIPTION
       The strtod, strtof, and strtold functions convert the ini­
       tial  portion  of the string pointed to by nptr to double,
       float, and long double representation, respectively.

       The expected form of the (initial portion of  the)  string
       is  optional  leading  white  space  as  recognized by is­
       space(3), an optional plus (``+'') or minus  sign  (``-'')
       and  then either (i) a decimal number, or (ii) a hexadeci­
       mal number, or (iii) an infinity, or (iv)  a  NAN  (not-a-
       number).

       A  decimal number consists of a nonempty sequence of deci­
       mal digits possibly containing a radix character  (decimal
       point,  locale  dependent, usually ``.''), optionally fol­
       lowed by a decimal exponent.  A decimal exponent  consists
       of  an  ``E''  or  ``e'',  followed by an optional plus or
       minus sign, followed by a non-empty  sequence  of  decimal
       digits, and indicates multiplication by a power of 10.

       A  hexadecimal  number consists of a ``0x'' or ``0X'' fol­
       lowed by a nonempty sequence of hexadecimal digits  possi­
       bly containing a radix character, optionally followed by a
       binary exponent. A binary exponent consists of a ``P''  or
       ``p'',  followed  by  an optional plus or minus sign, fol­
       lowed by a non-empty sequence of decimal digits, and indi­
       cates  multiplication  by  a  power of 2.  At least one of
       radix character and binary exponent must be present.

       An infinity is either ``INF'' or ``INFINITY'',  disregard­
       ing case.

       A  NAN  is ``NAN'' (disregarding case) optionally followed
       by `(', a sequence of characters, followed  by  ')'.   The
       character  string specifies in an implementation-dependent
       way the type of NAN.
RETURN VALUE
       These functions return the converted value, if any.

       If endptr is not NULL, a pointer to  the  character  after
       the last character used in the conversion is stored in the
       location referenced by endptr.

       If no conversion is performed, zero is  returned  and  the
       value  of  nptr  is  stored  in the location referenced by
       endptr.

       If the correct value would cause overflow, plus  or  minus
       HUGE_VAL  (HUGE_VALF, HUGE_VALL) is returned (according to
       the sign of the value), and ERANGE is stored in errno.  If
       the  correct value would cause underflow, zero is returned
       and ERANGE is stored in errno.

ERRORS
       ERANGE Overflow or underflow occurred.

CONFORMING TO
       ANSI C describes strtod, C99 describes the other two func­
       tions.

SEE ALSO
       atof(3), atoi(3), atol(3), strtol(3), strtoul(3)

von Rolf Magnus (Gast)


Lesenswert?

>       The behaviour is the same as
>
>              strtod(nptr, (char **)NULL);
>
>       except that atof() does not detect errors.

Deshalb sollte man besser gleich strtod nehmen.

von Andreas (Gast)


Lesenswert?

oder einfach den Punkt im Array suchen und dann über die 10-er Potenzen
ausrechnen, Vorzeichen prüfen und gegebenenfalls mit -1 multiplizieren.

von Sunset (Gast)


Lesenswert?

Erstmal großen Dank für Eure Beiträge.

Zur Zeit 'funktioniert' es mit der atof-Funktion. Da bekomme ich aber
Rundungsfehler rein. Z.B. im Array steht(als ascii): '-0.1'
atof macht daraus(als float) '-0.09999899'.
Bei Ganzzahlen funktionierts richtig.

Naja, das Wochenende muss auch genutzt werden. Andreas' Vorschlag ist
auch eine Überlegung wert.

Sunset

von Thomas (Gast)


Lesenswert?

Die Rundungsfehler sind nun halt mal Systembedingt, da 0,1 eine
unendliche Binärzahl ergibt. Ich nehme an, daß das eine 32 Bit Floating
Bibliothek ist. Wenn du es genau haben willst, die Zahl als
Festkommazahl nutzen (z.B. 1000 fach als integer).

Gruß Thomas

von Rolf Magnus (Gast)


Lesenswert?

> Zur Zeit 'funktioniert' es mit der atof-Funktion. Da bekomme ich
> aber Rundungsfehler rein. Z.B. im Array steht(als ascii): '-0.1'
> atof macht daraus(als float) '-0.09999899'.

Das ist kein Fehler von atof, sondern eine immanente Eigenschaft von
Fließkommazahlen. 0,1 ist im Dualsystem eine periodische Zahl.
So wie man im Dezimalsystem den Wert 1/3 nicht exakt darstellen kann,
so kann man im Dualsystem den Wert 1/5 und damit auch 1/10 nicht exakt
darstellen.

von Sunset (Gast)


Lesenswert?

Ich vergaß zu erwähnen, das ich mit Codevision füe den mega128 das
Programm schreibe.

Ich habe zum Vergleich mal die atof-Sequenz mit dem
Keil-C166-Compiler(für 16bit) getestet, der löst perfekt in float auf.

von Rolf Magnus (Gast)


Lesenswert?

> der löst perfekt in float auf.

Ich glaube eher, daß dessen atof den angezeigten Wert halt rundet, so
daß du es nicht siehst.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.