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
21 8369
> 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?
Maybe, but I was building my point view on float representation of
numbers and the debugger seemed to prove it...
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.
I couldn't agree more... 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.
For the first I meant: It's a fact that float can't represent all
numbers accuratley (that's why we need that Epsilon test), however the
Inaccuracy is negligible and won't affect the calculation.
For the second statement I meant : if atoi() would return 184.64 as
184.63999999 then printf() would print 184.63999999 as 184.64
Abdo Haji-Ali
Programmer
In|Framez
"Victor Bazarov" writes: osmium wrote: 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.
If there is information there, as opposed to words, it eludes me.
osmium <r1********@com cast.net> wrote: I would guess there are some helpful links or information on this problem in the FAQ, or try Wikipedia.
I would recommend reading "What Every Computer Scientist Should Know
About Floating-Point Arithmetic" by David Goldberg: http://docs.sun.com/source/806-3568/ncg_goldberg.html
--
Marcus Kwok
Replace 'invalid' with 'net' to reply
osmium wrote: "Victor Bazarov" writes:
osmium wrote:
That *is* the exact value that was passed. It is the best approximatio n 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.
If there is information there, as opposed to words, it eludes me.
The debugger translates the internal representation into a text
representation, just like printf does. It typically shows higher
precision, but you can do the same with printf or with iostreams.
--
Pete Becker
Roundhouse Consulting, Ltd.
osmium wrote: "Victor Bazarov" writes:
osmium wrote:
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.
If there is information there, as opposed to words, it eludes me.
Too bad.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Victor Bazarov wrote: 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?
AFAIK the mapping of "very probable" to a numeric function is up to the
individual application: http://www.doc.ic.ac.uk/~nd/surprise.../article2.html
(C++ being a useful language in which fuzzy applications can be
written in usefully ... an' all.)
regards
Andy Little
Victor Bazarov wrote: If I try using "EPSILON" in my program, it says "undefined identifier". Do I need to include some header for that?
There isn't any, EPSILON depends on your accuracy demands, it could be
0.001, 1 or even 100 so you should define it yourself.
Abdo Haji-Ali
Programmer
In|Framez
asterisc wrote: 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;
Actually, it's extremely improbable that two objects of exactly the
same basic type, initialized using exactly the same expression, within
the same region of program text (same declaration even, sharing the
same set of declaration specifiers), will compare unequal.
> Actually, it's extremely improbable that two objects of exactly the same basic type, initialized using exactly the same expression, within the same region of program text (same declaration even, sharing the same set of declaration specifiers), will compare unequal.
Indeed, very improbable, but not impossible. Anyway, i just wanted to
express that if you need to check if two floating numbers are equal,
then don't rely on the '==';
If I try using "EPSILON" in my program, it says "undefined identifier". Do I need to include some header for that?
There is no *standard* EPSILON. You have to define your own, depending
on your needs.
For example, i work in CAD industry, so i have my own EPSILON like
this:
#define EPSILON 0.00001
It's better to use a function that return that (instead of a macro),
or, better a singleton object.
asterisc wrote: #define EPSILON 0.00001
It's better to use a function that return that (instead of a macro), or, better a singleton object.
A macro is certainly not the best choice, but, if the epsilon approach
with that value is appropriate for your application, why is a function
or a singleton better than
const double epsilon = 0.00001;
Gavin Deane This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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.
|
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
|
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) ||
|
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...
| |
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;
|
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()
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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,...
| |
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...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| | |