473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Precision Issue with STRTOF

Dear Group

I am encountering a precision issue while converting a text string to
a float using STRTOF. For example, at times a text string "736.00000"
(quotation marks added) is converted to "736.00001". I have null
terminated the strings. Does anyone know why does this happen and how
specifically to avoid this. This is quite critical to an applicaiton
that I am writing.
Thanks

Marcus D. Jacobs
Nov 13 '05 #1
11 6277
ma*******@hotmail.com (Marcus Jacobs) writes:
I am encountering a precision issue while converting a text string to
a float using STRTOF. For example, at times a text string "736.00000"
(quotation marks added) is converted to "736.00001". I have null
terminated the strings. Does anyone know why does this happen and how
specifically to avoid this. This is quite critical to an applicaiton
that I am writing.


You should read the FAQ.
--
"Some programming practices beg for errors;
this one is like calling an 800 number
and having errors delivered to your door."
--Steve McConnell
Nov 13 '05 #2
Marcus Jacobs wrote:
I am encountering a precision issue while converting a text string to
a float using STRTOF. For example, at times a text string "736.00000"
(quotation marks added) is converted to "736.00001". I have null
terminated the strings. Does anyone know why does this happen and how
specifically to avoid this. This is quite critical to an applicaiton
that I am writing.


Did you try writing a little test program like this (not tested)?

#include <stdio.h>
main() {
float x = 736.0;
printf("%f\n", (double) x);
}

You'll probably get similar results. Float is the low-precision
type. Double is the high-precision type. Use double.

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #3
Marcus Jacobs wrote:
Dear Group

I am encountering a precision issue while converting a text string to
a float using STRTOF. For example, at times a text string "736.00000"
(quotation marks added) is converted to "736.00001". I have null
terminated the strings. Does anyone know why does this happen and how
specifically to avoid this. This is quite critical to an applicaiton
that I am writing.


Note that C is case-sensitive. The function's name is 'strtof', not 'STRTOF'.
strtof() returns a float. A float need not have more than 6 digits of
precision. The value you report "736.00001" has some 27 bits of information
apart from scaling ("mantissa"). How do you know that the value has those
8 decimal digits of precision? If you would restrict yourself to printing
only the information in the data, you would do better.

#include <stdio.h>
#include <math.h>
void showfloat(x)
{
printf("%.*g", FLT_DIG, x);
}


--
Martin Ambuhl

Nov 13 '05 #4

"Marcus Jacobs" <ma*******@hotmail.com> wrote in message

I am encountering a precision issue while converting a text string to
a float using STRTOF. For example, at times a text string "736.00000"
(quotation marks added) is converted to "736.00001". I have null
terminated the strings. Does anyone know why does this happen and
how specifically to avoid this. This is quite critical to an applicaiton
that I am writing.

Regard floating point variables as having a small random error. It's not
really random, of course, but there is no exact representation of 0.1 in a
typical floating-point format.

If the the problem is critical to your application, there are several things
you can do. The best and probably easiest is to rewrite your algorithms so
that they can tolerate small errors.

The next thing to do is to use a fixed-point representation. If you only
need three digits of decimal places, you could store all results in longs as
value *1000 (this method is very useful if the values you are dealing with
are currency).

As a last resort, resign yourself to operating in decimal and write your own
floating point routines, maybe taking their operands as ASCII strings. This
will be slow and messy but should be perfectly accurate.
Nov 13 '05 #5
Tom Zych wrote:

Marcus Jacobs wrote:
I am encountering a precision issue while converting a text string to
a float using STRTOF. For example, at times a text string "736.00000"
(quotation marks added) is converted to "736.00001". I have null
terminated the strings. Does anyone know why does this happen and how
specifically to avoid this. This is quite critical to an applicaiton
that I am writing.


Did you try writing a little test program like this (not tested)?

#include <stdio.h>
main() {
float x = 736.0;
printf("%f\n", (double) x);
}

You'll probably get similar results. Float is the low-precision
type. Double is the high-precision type. Use double.

double, is what you should use most of the time
for foating point variables.
float, is for when memory is tight.
long double, is for when double isn't precise enough.

--
pete
Nov 13 '05 #6
Malcolm wrote:
As a last resort, resign yourself to operating in decimal and write your own
floating point routines, maybe taking their operands as ASCII strings. This
will be slow and messy but should be perfectly accurate.


If someone really needs more precision than double (including exact
fractions), I'd recommend gmp.

http://www.swox.com/gmp/

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #7
pete wrote:
double, is what you should use most of the time
for foating point variables.
float, is for when memory is tight.
long double, is for when double isn't precise enough.


Some C programmer I am...I forgot all about long double :)

--
Tom Zych
This email address will expire at some point to thwart spammers.
Permanent address: echo 'g******@cbobk.pbz' | rot13
Nov 13 '05 #8
On Tue, 09 Sep 2003 01:24:57 GMT, pete <pf*****@mindspring.com> wrote
in comp.lang.c:
Tom Zych wrote:

Marcus Jacobs wrote:
I am encountering a precision issue while converting a text string to
a float using STRTOF. For example, at times a text string "736.00000"
(quotation marks added) is converted to "736.00001". I have null
terminated the strings. Does anyone know why does this happen and how
specifically to avoid this. This is quite critical to an applicaiton
that I am writing.


Did you try writing a little test program like this (not tested)?

#include <stdio.h>
main() {
float x = 736.0;
printf("%f\n", (double) x);
}

You'll probably get similar results. Float is the low-precision
type. Double is the high-precision type. Use double.

double, is what you should use most of the time
for foating point variables.
float, is for when memory is tight.
long double, is for when double isn't precise enough.


....assuming that there are actually three different underlying
representations, which there most certainly not on a lot of platforms.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #9
Marcus Jacobs wrote:

Dear Group

I am encountering a precision issue while converting a text string to
a float using STRTOF. For example, at times a text string "736.00000"
(quotation marks added) is converted to "736.00001". I have null
terminated the strings. Does anyone know why does this happen and how
specifically to avoid this. This is quite critical to an applicaiton
that I am writing.

Thanks

Marcus D. Jacobs


You've gotten quite a few interesting responses by now but none of them
see the 'error' in your post. A float or a double can represent integers
precisely (without error) up to 2**24 for float and 2**53 for double.
The case for "736.00000" converting to "736.00001" would indicate an
error in strtof() perhaps. The floating point representation of 736 is
exact, not an approximation.
--
Joe Wright mailto:jo********@earthlink.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #10
Joe Wright wrote:

You've gotten quite a few interesting responses by now but none of them
see the 'error' in your post. A float or a double can represent integers
precisely (without error) up to 2**24 for float and 2**53 for double.
uh-uh. A float must be accurate to 6 decimal digits (about 20 bits) and a
double to 10 decimal digits (about 33 bits). Your implementation does not
define the language.
The case for "736.00000" converting to "736.00001" would indicate an
error in strtof() perhaps.


uh-uh. It shows that he was trying to get 8 digits printed from a variable
for which only 6 are required. This is a *programmer* error in misusing
?printf().

--
Martin Ambuhl

Nov 13 '05 #11
Ben Pfaff wrote:
ma*******@hotmail.com (Marcus Jacobs) writes:
I am encountering a precision issue while converting a text
string to a float using STRTOF. For example, at times a text
string "736.00000" (quotation marks added) is converted to
"736.00001". I have null terminated the strings. Does anyone
know why does this happen and how specifically to avoid this.
This is quite critical to an applicaiton that I am writing.


You should read the FAQ.


However, assuming the OP really means strtof(), it seems to be an
extremely poor implementation that cannot maintain exact values
for a range of integers that fit in the significand. Maybe
Marcus should identify it, so that we can avoid it.

In general float (or double, etc.) values should never be tested
for exact equality. They are always the 'best' approximation to
the value that the implementation can provide. They should
always meet the criteria specified by FLT_EPSILON.

--
Replies should be to the newsgroup
Chuck Falconer, on vacation.
Nov 13 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Ladvánszky Károly | last post by:
Entering 3.4 in Python yields 3.3999999999999999. I know it is due to the fact that 3.4 can not be precisely expressed by the powers of 2. Can the float handling rules of the underlying layers be...
11
by: Marcus Jacobs | last post by:
Dear Group I have written a file conversion program that uses strtof to convert text strings to floats. It works as I intended except for my error messages. It is my understanding that strtof...
10
by: Jonathan Lamothe | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I've written a program that requires the user to input a decimal number which is then stored as a float. First it's read into a variable buf...
3
by: katrinaVictim | last post by:
Please copy and paste the full code source into any .asp page and pull the page from the browser. Make sure all components are up to date. Explain why number1 is treated differently than number2...
5
by: rocknbil | last post by:
Hello everyone! I'm new here but have been programming for the web in various languages for 15 years or so. I'm certainly no "expert" but can keep myself out of trouble (or in it?) most of the time....
3
by: Boot2TheHead | last post by:
This one cost me a solid half hour yesterday. I'm wondering why on earth the default precision for a decimal type is 18,0. Maybe I'm mistaken. A decimal datatype sort of implies that you'd want...
10
by: Artemio | last post by:
Hello all! I just stumbled across a weird problem with precision of a division operation. I am on Mac OS X, GCC 4.0.1. Say I have two float or double numbers, and I want to divide one by...
137
by: mathieu.dutour | last post by:
Dear all, I want to do multiprecision floating point, i.e. I want to go beyond single precision, double precision and have quadruple precision, octuple precision and the like, and possibly with...
0
by: Charles Coldwell | last post by:
James Kanze <james.kanze@gmail.comwrites: True, with some additional considerations. The commonly used IEEE 754 floating point formats are single precision: 32 bits including 1 sign bit, 23...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.