Topic: conversion
Author: logigon@aol.com (Logigon)
Date: 1997/10/08 Raw View
there is atof and the like that suppose to conver a string to a floating point
number stored into a float variable..
however, a lot of times its not precise, for instance the string 5.556 will
turn into something like 5.555999999
or 1.23 will turn into 1.230000675.
i know its insignificant but it looks bad when i write a program that displays
the number.
is there a better way to convert a string to a float or double???
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1997/10/08 Raw View
Logigon wrote:
>
> there is atof and the like that suppose to conver a string to a floating point
> number stored into a float variable..
> however, a lot of times its not precise, for instance the string 5.556 will
> turn into something like 5.555999999
> or 1.23 will turn into 1.230000675.
> i know its insignificant but it looks bad when i write a program that displays
> the number.
> is there a better way to convert a string to a float or double???
No, there is nothing wrong with atof(), the problem is inherent with
floating point representations of numbers. The number 5.556 cannot be
stored exactly in a floating point number; the only numbers that can be
stored exactly can be written as an integer times a power of 2. There
are maximum and minimum values for that integer, and maximum and minimum
values for that power of 2.
float f;
f = atof("5.556")
calculates the double precision number that is closest to 5.556,
converts that number to the closest floating point number, and stores
it in f.
printf("%f", f)
converts f to double precision, a conversion that is usually exact.
Then, it prints out the decimal number that is closest to that number.
In general, that number may not be the one originally passed to atof().
You can significantly reduce this problem by using double precision
numbers, rather than floating point. However, even then there are
floating point roundoffs that can sometime make the printed string be
different from the input string.
The main thing you should do to minimize the problem is to never print
with more precision than is actually justified by your application.
Check numeric_limits<float>.epsilon() and
numeric_limits<double>.epsilon() to find out the maximum relative
precision with which you can store floating point numbers.
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]