Thread: atof() question

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    84

    atof() question

    Hi all,

    I am getting the following warning using the following code:
    Code:
    float Convert(char *str)
    {
        float num;
        num=atof(str);
        return num;
    }
    warning C4244: '=' : conversion from 'double ' to 'float ', possible loss of data

    I thought that atof() is ascii to float..... not asci to double........
    What am I doing wrong?

    Thank you

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I thought that atof() is ascii to float.....
    "float" being "floating-point", not the float type specifically. atof returns double.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    84
    Any suggestions to convert string (chars) to float type?
    Last edited by salvadoravi; 01-10-2008 at 12:53 PM.

  4. #4
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Code:
    float Convert(char *str)
    {
        float num;
        num = (float) atof(str);
        return num;
    }
    or

    Code:
    float Convert(char *str)
    {
        float num;
        sscanf(str, "%f", &num);
        return num;
    }

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Any suggestions to convert string (chars) to float type?
    Why not just use double?
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    84
    thank you for the code it's great!

    I want to use float for study purpses.

    Except the fact the float is 6 digit percision and double is 10,
    I don't really understand the difference.

    for example if I enter the following number 1234.56 in a float type the output will be 1234.560059 , I don't understand why.

    Another thing if I use
    Code:
    num=(float)atof(string,"%f",&num);
    In what point will I my data lost it's precision.

    Many thanks again

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by salvadoravi View Post
    thank you for the code it's great!

    I want to use float for study purpses.

    Except the fact the float is 6 digit percision and double is 10,
    I don't really understand the difference.

    for example if I enter the following number 1234.56 in a float type the output will be 1234.560059 , I don't understand why.

    Another thing if I use
    Code:
    num=(float)atof(string,"%f",&num);
    In what point will I my data lost it's precision.

    Many thanks again
    http://docs.sun.com/source/806-3568/ncg_goldberg.html
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    for example if I enter the following number 1234.56 in a float type the output will be 1234.560059 , I don't understand why.
    A simple answer is "because computers work in base 2 and we (humans) work in base 10". Some numbers who are representable with a finite numbers of digits in base 10 aren't necessarily representable with a finite numbers of digits in base 2.

    Example. "2/3" can't be represented with a finite number of digit in base 10. If we can take only 4 digits to represent that number, we'll write "0.667". But in base 3, you can write it as "0.2".

    Another thing if I use
    Code:
    num=(float)atof(string,"%f",&num);
    In what point will I my data lost it's precision.
    Doubles have more significative digits than floats. By the way, that call to atof is incorrect (mix between sscanf and atof).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. atof()
    By Luigi in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2003, 10:08 AM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM