string2float

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pontiy.pilat@gmail.com

    #1

    string2float

    How i can convert *char to float?

  • Chris McDonald

    #2
    Re: string2float

    pontiy.pilat@gm ail.com writes:
    >How i can convert *char to float?
    atof();

    --
    Chris,

    Comment

    • Eric Sosman

      #3
      Re: string2float

      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)

      --
      Eric Sosman
      esosman@acm-dot-org.invalid

      Comment

      • Robert Gamble

        #4
        Re: string2float


        Eric Sosman wrote:
        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)
        C99 has strtof() which will convert directly to float representation.

        Robert Gamble

        Comment

        • Flash Gordon

          #5
          Re: string2float

          Chris McDonald wrote:
          pontiy.pilat@gm ail.com writes:
          >
          >How i can convert *char to float?
          >
          atof();
          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

            #6
            Re: string2float

            "Robert Gamble" <rgamble99@gmai l.comwrites:
            Eric Sosman wrote:
            >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)
            >
            C99 has strtof() which will convert directly to float representation.
            And if your implementation doesn't have strtof(), it's easy enough to
            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

              #7
              Re: string2float

              Keith Thompson wrote:
              "Robert Gamble" <rgamble99@gmai l.comwrites:
              Eric Sosman wrote:
              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)
              C99 has strtof() which will convert directly to float representation.
              >
              And if your implementation doesn't have strtof(), it's easy enough to
              call strtod() and assign the result to a float.
              Except that if the result can be represented as a double but is outside
              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.
              It's even easier to use double in the first place.
              Agreed.
              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.)
              Robert Gamble

              Comment

              Working...