How i can convert *char to float?
string2float
Collapse
This topic is closed.
X
X
-
pontiy.pilat@gmail.comTags: None -
Chris McDonald
Re: string2float
pontiy.pilat@gm ail.com writes:
atof();>How i can convert *char to float?
--
Chris,
-
Eric Sosman
Re: string2float
Chris McDonald wrote:
strtod() is better, but neither actually converts a
string to float: both convert to double. The only way I
can think of to convert directly to float is
if (sscanf(my_stri ng, "%f", &float_variable ) == 1)
--
Eric Sosman
esosman@acm-dot-org.invalid
Comment
-
Robert Gamble
Re: string2float
Eric Sosman wrote:C99 has strtof() which will convert directly to float representation.Chris McDonald wrote:
>>
strtod() is better, but neither actually converts a
string to float: both convert to double. The only way I
can think of to convert directly to float is
>
if (sscanf(my_stri ng, "%f", &float_variable ) == 1)
Robert Gamble
Comment
-
Flash Gordon
Re: string2float
Chris McDonald wrote:Not a very good choice. You can't test to see if it worked and if the
value cannot be represented as a double it invokes undefined behaviour
(i.e. anything can happen). A far better function to use is strtod. This
allows you to test how much of the string was converted and if it is out
of range behave in a defined manner including setting errno, and thus
you can do error checking reliable and report problems such as the user
entering "fiftythree " of "12e456789" .
--
Flash Gordon
Still sigless on this computer.
Comment
-
Keith Thompson
Re: string2float
"Robert Gamble" <rgamble99@gmai l.comwrites:And if your implementation doesn't have strtof(), it's easy enough toEric Sosman wrote:>>Chris McDonald wrote:>>
> strtod() is better, but neither actually converts a
>string to float: both convert to double. The only way I
>can think of to convert directly to float is
>>
> if (sscanf(my_stri ng, "%f", &float_variable ) == 1)
C99 has strtof() which will convert directly to float representation.
call strtod() and assign the result to a float. It's even easier to
use double in the first place.
sscanf() has the disadvantage that, if the floating-point literal in
the string represents a value that can't be represented in the target
type, the behavior is undefined. strtod() has well-defined behavior
on overflow. (It would have been nice if sscanf() had been defined
this way as well.)
--
Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Comment
-
Robert Gamble
Re: string2float
Keith Thompson wrote:Except that if the result can be represented as a double but is outside"Robert Gamble" <rgamble99@gmai l.comwrites:>Eric Sosman wrote:C99 has strtof() which will convert directly to float representation.Chris McDonald wrote:
pontiy.pilat@gm ail.com writes:
>How i can convert *char to float?
atof();
>
strtod() is better, but neither actually converts a
string to float: both convert to double. The only way I
can think of to convert directly to float is
>
if (sscanf(my_stri ng, "%f", &float_variable ) == 1)
And if your implementation doesn't have strtof(), it's easy enough to
call strtod() and assign the result to a float.
the range of float then assigning the return value to a float will
result in undefined behavior. In this case one would need to check the
return value of strtod before assigning it to float, which is a pain
and probably why strtof() was added.
Agreed.It's even easier to use double in the first place.
Robert Gamblesscanf() has the disadvantage that, if the floating-point literal in
the string represents a value that can't be represented in the target
type, the behavior is undefined. strtod() has well-defined behavior
on overflow. (It would have been nice if sscanf() had been defined
this way as well.)
Comment
Comment