Questions tagged [atof]

atof() is the C runtime library function for converting the ASCII representation of a number to a floating point double.

Use this tag on for all questions about the use of atof() or where it does not seem to be working correctly.

Closely related are:

  • for functions converting text to int, long, or long long
  • for converting to long from text in any base from 2 through 36. Or choose a base automatically as the C compiler does depending on how the number is written. strtoul() converts to an unsigned long.
  • for double
  • for converting one or more values at a time directed by a format specification

SYNPOSIS

#include <stdlib.h>

double atof(const char *nptr);

atof() returns a value of zero if there is any problem during the conversion, though the documentation says that it does not detect errors. Since converting a zero also returns zero, there is no easy way of distinguishing an error from a correct conversion of zero. Use either scanf() or strtod() if error checking is needed.

atof() accepts integers, fixed decimal notation, and scientific notation. Some implementations also recognize the strings INF or INFINITY (disregarding case), NAN (disregarding case) optionally followed by (, a sequence of characters, followed by ). The character string specifies an implementation-dependent type of NAN. (Documentation of these strings are scant.)

Some implementations support hexadecimal formatted floats. These begin with 0x or 0X, followed by at least one hex digit, an optional decimal point (locale-specific), more digits, an optional p or P followed by the binary exponent in hex.

129 questions
3
votes
3 answers

Conversion problem in ansi c

Possible Duplicates: Floating point inaccuracy examples Is JavaScript's Math broken? I need to convert some data from txt file into double value and I'm using this function for this : atof . The problem is that the value which must be convert is…
Jordan Borisov
  • 1,603
  • 6
  • 34
  • 69
3
votes
1 answer

How do I read in command line arguments into a double array/vector in C?

I need to be able to input several things using the command line when I run my program in C. I would run the program using a command like the line below for example: ./programName 1 2.5 A.txt B.txt 0.0,0.5,1.0,1.5 Then I would ideally have the…
user3716193
  • 476
  • 5
  • 21
3
votes
4 answers

Explanation of atof code from K&R

I understand what we are doing, before we converted a string to an int, now we are converting a string to a double. I don't understand the logic behind this code though. Could someone clarify this a little for me? Best regards. #include…
kits
  • 609
  • 6
  • 20
3
votes
1 answer

Understanding Error "error: cannot convert ‘char**’ to ‘const char*’ for argument ‘1’ to ‘double atof(const char*)’"

So I'm working on this assignment for my class and book is REALLY unclear about how to use the argc, argv, and atof() items (which are all new to me), so I am trying to use them but I am getting this error, and I'm not quite sure how to correct…
scbeacham
  • 279
  • 2
  • 4
  • 12
3
votes
8 answers

atof and non-null terminated character array

using namespace std; int main(int argc, char *argv[]) { char c[] = {'0','.','5'}; //char c[] = "0.5"; float f = atof(c); cout << f*10; if(c[3] != '\0') { cout << "YES"; } } OUTPUT: 5YES Does atof work with…
tez
  • 4,990
  • 12
  • 47
  • 67
2
votes
2 answers

Atof not working?

So I'm writing a program for a school project, and part of it requires having a user put in a random number at the command line. The program then uses atof to convert the number to a float so I can do some math with it. That part of the program…
Dee
  • 21
  • 1
  • 2
  • 4
2
votes
1 answer

Is `std::atof` guaranteed to produce identical output when given identical string input?

I'm reading double values from file as strings and parsing them with std::atof. Afterwards, I'm using the values as keys in a unordered map. It's seems to be working correctly, but is it guaranteed to work in 100% of the cases? I'm asking the…
Arsen Zahray
  • 24,367
  • 48
  • 131
  • 224
2
votes
2 answers

Invalid conversion of char to float, different codes and no good results

I want to code a program which receives a number between zero and ten and shows a message saying it's valid or not. If not, it should keep asking for a valid number. I could code everything and it seems pretty okay for me, but it's not working…
HansMiku
  • 23
  • 3
2
votes
5 answers

c++ atof. How to check wrong input?

So I use atof to convert my string to double. But I need to know if I get wrong input (like y564 etc.). How can I check it? I need correct number for further actions with it. double x = atof(s.c_str());
Paul R.
  • 23
  • 1
  • 3
2
votes
2 answers

Strange behavior with atof

I have an Arduino which receives a message from my smartphone (over bluetooth) containing the unix timestamp. Now, i am trying to sync my DS1307 with that timestamp. However, it did not work, hence, i begun searching and found some strange behavior…
Kevin
  • 2,813
  • 3
  • 20
  • 30
2
votes
1 answer

Turning a group of characters into a float using atof()

How can I convert all 4 characters into a float ? I can only convert the first character into an integer. Can you also give me some examples in your explanation. thanks This is what I have tried so far. void use_atof() { char c[200]; …
user5771881
  • 97
  • 1
  • 9
2
votes
1 answer

using atof function in x86 NASM

I am having some trouble getting the c function atof() to work in my asm program. I'm trying to read in 4 numbers from the keyboard, and ultimately print their average. Before i can do that, however, i need to convert the numbers to floats. I'm…
pfNbigblue
  • 25
  • 5
2
votes
3 answers

Alternative to atof() function to differenciate input 0 with failure

I created a function to validate the input of real numbers . It is working well , but as the atof() function returns 0 upon failure, it is not possible to enter 0 as an input value. Anyone can give help to solve this? float validate_float() { …
nosperov
  • 31
  • 1
  • 4
2
votes
2 answers

Is boost::lexical_cast thread-safe?

I am actually failing to find an answer to this question in boost documentation. I am being a bit paranoid about using atof in a multi-threaded environment, so one suggestion was to replace the call with lexical_cast. Is lexical_cast thread-safe?
sneg
  • 2,088
  • 4
  • 19
  • 23
2
votes
1 answer

atof truncates fractional parts after QApplication

I created a qt application with qt creator, the code is like, #include "mainwindow.h" #include #include #include int main(int argc, char *argv[]) { double before = atof("3.1"); double x; sscanf("3.1", "%lf",…
user2621037
  • 326
  • 1
  • 3
  • 13
1
2
3
8 9