473,794 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

atof approximates the String, I need exact value of the string passed

Hi talents,

I have noticed that atof() function approximates the string I pass to
it.

when I use atof() , as atof(" 184.64")
and it returns 184.63999999999

But I would like to have the exact value that is passed.

Could anyone help me?

Thanks
Suresh Ooty

Apr 12 '06 #1
21 8366
ok******@gmail. com wrote:
Hi talents,

I have noticed that atof() function approximates the string I pass to
it.

when I use atof() , as atof(" 184.64")
and it returns 184.63999999999

But I would like to have the exact value that is passed.

Could anyone help me?


No. Floating point types are not exact types. They cannot represent every
possible value. So whenever you convert a number representation of a string
into a floating point value, or take the string representation of a
floating point value, some approxiamtion must be done.

Apr 12 '06 #2
> when I use atof() , as atof(" 184.64")
and it returns 184.63999999999

But I would like to have the exact value that is passed.


For floating point operations you should specify some toleration like
EPSILON.
By the way, even the same number, can be represented in two different
representation, that it: if you have: double a = 10.5, b = 10.5; it's
very probable that a != b;
Alaways use: if( fabs ( a - b ) < EPSILON ) then they are equals.

Apr 12 '06 #3
ok******@gmail. com wrote:
I have noticed that atof() function approximates the string I pass to
it.

when I use atof() , as atof(" 184.64")
and it returns 184.63999999999


Really? Try this:

#include <stdio.h>
#include <stdlib.h>
int main() {
double d = atof(" 184.64");
printf("%f\n", d);
}

Works for me...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 12 '06 #4
Really? Try this:

#include <stdio.h>
#include <stdlib.h>
int main() {
double d = atof(" 184.64");
printf("%f\n", d);
}

Works for me...


You are right, however looking at the value of d while debugging will
show that d has a value of 184.639999999. But as you can see this makes
no difference as a whole as the calculation would be affected by a very
small amount, if any, and all the printing function would consider
this...

Abdo Haji-Ali
Programmer
In|Framez

Apr 12 '06 #5
asterisc wrote:
when I use atof() , as atof(" 184.64")
and it returns 184.63999999999

But I would like to have the exact value that is passed.
For floating point operations you should specify some toleration like
EPSILON.


If I try using "EPSILON" in my program, it says "undefined identifier".
Do I need to include some header for that?
By the way, even the same number, can be represented in two different
representation, that it: if you have: double a = 10.5, b = 10.5; it's
very probable that a != b;
Really? How "very probable" is that? 0.8? 0.9?
Alaways use: if( fabs ( a - b ) < EPSILON ) then they are equals.


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 12 '06 #6
<ok******@gmail .com> wrote:
I have noticed that atof() function approximates the string I pass to
it.

when I use atof() , as atof(" 184.64")
and it returns 184.63999999999

But I would like to have the exact value that is passed.


That *is* the exact value that was passed. It is the best approximation for
that number that the compiler could come up with and it doesn't look like
adding bits to the representation would help. It is analogous to the
problem of representing 1/3 exactly in the decimal number system. Compilers
commonly round the number to a more pleasing number when doing output. If
you can't get the compiler to do that you could round it yourself.

I would guess there are some helpful links or information on this problem in
the FAQ, or try Wikipedia.
Apr 12 '06 #7
Abdo Haji-Ali wrote:
Really? Try this:

#include <stdio.h>
#include <stdlib.h>
int main() {
double d = atof(" 184.64");
printf("%f\n", d);
}

Works for me...
You are right, however looking at the value of d while debugging will
show that d has a value of 184.639999999.


I don't think it's irrelevant. Debugging is not defined in C++ language
Standard. Yes, I know, the world is a cruel place. But can't it be that
your debugger is wrong?

The point I was trying to make was simple: you cannot compare two different
representations . You need to compare two representations of the same kind.
External with external, for example. If you read five significant digits,
you need to compare it with the output that has only five significant digits
otherwise it's bogus.

OK, if you do atof("184.64000 000000000000") and then output 20 digits, it's
still possible that you will not get the same sequence of characters. Only
then we can talk about precision of the internal representation because the
number of digits actually exceeds the precision.
But as you can see this
makes no difference as a whole as the calculation would be affected
by a very small amount, if any, and all the printing function would
consider this...


I don't understand that statement, sorry.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 12 '06 #8
asterisc wrote:
when I use atof() , as atof(" 184.64")
and it returns 184.63999999999

But I would like to have the exact value that is passed.

For floating point operations you should specify some toleration like
EPSILON.
By the way, even the same number, can be represented in two different
representation, that it: if you have: double a = 10.5, b = 10.5; it's
very probable that a != b;


On the contrary: they're exactly the same. If you compute the values
differently you may get different results, but that's true of
fixed-width arithmetic in general. You can write examples of integer
arithmetic where computing what looks to us like the same thing produces
different results.
Alaways use: if( fabs ( a - b ) < EPSILON ) then they are equals.


Only do that if you know how the values were computed and that the
result you're getting with that rule makes sense. But keep in mind that
it's not transitive: there are many triples of values a, b, and c such
that a equals b and b equals c, but a does not equal c.

--

Pete Becker
Roundhouse Consulting, Ltd.
Apr 12 '06 #9
osmium wrote:
<ok******@gmail .com> wrote:
I have noticed that atof() function approximates the string I pass to
it.

when I use atof() , as atof(" 184.64")
and it returns 184.63999999999

But I would like to have the exact value that is passed.
That *is* the exact value that was passed. It is the best
approximation for that number that the compiler could come up with
and it doesn't look like adding bits to the representation would
help.


Actually, it's the debugger's best approximation of the best approximation
that the run-time could come up with. The compiler doesn't get its hands
on the actual double value. It's a sequence of chars in the source.
[...]


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 12 '06 #10

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

Similar topics

4
8828
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where a * occurs in the string). This split function should allocate a 2D array of chars and put the split results in different rows. The listing below shows how I started to work on this. To keep the program simple and help focus the program the...
6
4494
by: Sreekanth | last post by:
Hello, Am trying to convert a string to float. Am using atof() for that purpose. But the return value for atof is same for the string "0.0" and for some invalid input "Invalid". Can any body suggest me the way to differentiate an invalid float number string and a 0.0. TIA, Sreekanth.
15
13084
by: XZ | last post by:
Hi everyone, this is really confusing to me: #include <stdio.h> main(int argc, char **argv) { printf("argv = %f\n",(double)atof(argv)); printf("argv = %d\n\n",atoi(argv)); } $ a.out a argv = 97.000000
32
14903
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
5
3203
by: tjay | last post by:
Hi. I wrote some code using sprintf and atof to store a double as a string of fixed length and to convert it back to a double variable. The string is stored in a char buffer global variable. I'm afraid it might contain bugs though. If I serialize a double, I get a string of the format "-1.0000000000000000e+212". This string gets stored in the buffer. Then, to convert it back into a double, I pass it to the atof function. The problem...
14
8918
by: sharmaharish | last post by:
I need a conversion function that converts values from string to a particular type. For this I have a template function that looks like this ... template<class T> T value(const string& s) { istringstream(s); T val; is >val;
2
8898
by: allexander | last post by:
Hello ! I need to convert a string like string number = "1235646.5678" to any floating point number x (double x or float x) I have used earlier the function atof()
0
9518
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10212
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10161
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6777
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4112
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2919
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.