473,394 Members | 1,709 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,394 software developers and data experts.

Looking for some way to get correct value with long precision

Dew
Hi All
The problem is related to strtod()/atof()'s well known behaviour.
strtod()/atof() add some extra digits at the end of precision while
converting from string to double. Becasue floating-point variables
cannot represent all numbers precisely, I'm looking some way around to
solve it explicitly in the code itself so that I could get the value
property = 999999999998.90000000 for precison=8 instead of
property=999999999998.90002441

Size of float and double both is 8 bytes in my machine. and I'm using
xlC compiler on AIX plateform.
Any clue will help me immensely. Thanks.
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<wchar.h>
#include<math.h>
#include<float.h>

int main(int argc, char* argv[])
{
char property[1024];
int precison=0;
memset(property, '\0', sizeof(property));
strcpy(property, argv[1]);
precison = atoi(argv[2]);
cout <<"property: \"" << property << "\"" << endl;
//sscanf ( property, "%Lf", &dl);
//sprintf(PrpValue1, "%.*lf", precison,property);

double doubleVal=0.0;

if (strstr(property,"E+") != NULL || strstr(property,"E-") != NULL)
{
doubleVal = ( long double)strtod(property, (char **)NULL);
//doubleVal = atof(property);
printf("doubleVal:%.*lf\n",precison,doubleVal);
sprintf(property, "%.*lf", precison,doubleVal);
cout <<"E+/- :property: " << property << endl;
}

cout <<"final property: " << property << endl;
free(property);
return 0;
}

Here's the o/p.
/a.out 9.999999999989E+11 8

property: "9.999999999989E+11"
doubleVal:999999999998.90002441
E+/- :property: 999999999998.90002441
final property: 999999999998.90002441

May 8 '06 #1
7 4844
On 8 May 2006 03:02:27 -0700, "Dew" <mr*********@gmail.com> wrote:
Hi All
The problem is related to strtod()/atof()'s well known behaviour.
strtod()/atof() add some extra digits at the end of precision while
converting from string to double. Becasue floating-point variables
It's not behaviour of strtod or atof but behaviour of float/double.
cannot represent all numbers precisely, I'm looking some way around to
solve it explicitly in the code itself so that I could get the value
property = 999999999998.90000000 for precison=8 instead of


Just use rounding before outputting values.
May 8 '06 #2
Dew
Hi Olaf,
Thanks for reply.
doubleVal = 999999999998.90002441 , which is returned from strtod call
have

Now I am doing rounding as well till the length precison .. but it cant
help me.
sprintf(property, "%.*lf", precison,doubleVal);

I also have tried flor().

Are you aware of any rounding techniques that allow control of decimal
point length or do I need to create my own rounding function ?

Thanks.

May 8 '06 #3
Dew
Hi,
Infact I need to replace the extra digit (2441) added by
strtod()/atof() in the last of precission, by 0000. will round tech
will help here ?
any clue ?

May 8 '06 #4
Dew
I am trying something like
1.
doubleVal = ( long double)strtod(property, (char **)NULL); // give me
999999999998.90002441
double dl = doubleVal * 10000000;
double res = floor(dl)/10000000;] // give me 1e+12

2.
printf("doubleVal:%.*lf\n",precison,doubleVal); //give me
999999999998.90002441

3. printf("doubleVal:%.8g\n",doubleVal) // give me 1e+12

any way around to get 999999999998.90000000 ?

May 8 '06 #5
On 8 May 2006 04:55:00 -0700, "Dew" <mr*********@gmail.com> wrote:
any way around to get 999999999998.90000000 ?


Ah.

A 64-bit double has only 15 significant digits. So no, you it doesn't
have enough precision for what you wish to do.
May 8 '06 #6
Dew wrote:
property = 999999999998.90000000 for precison=8 instead of
property=999999999998.90002441


Olaf gave the correct answer, but it may be good to expand
on it.

A double only has about 15 significant digits. So, in your
values there, that takes you to 999999999998.900 roughly.
Exactly where it ends is dependent on the specific value,
as it isn't storing things as decimal digits, but as a
binary representation of a float.

Anyway, what that means is anything past the .900
is noise. It is not significant, and there is no correct
nor incorrect value for it. So, a desire to get .90000000
(or any other specific value) is misplaced. No matter
what it tells you, the value is no better, nor worse,
than any other value. Anything from .90000000 to
..90099999 is as good as any other value in the range.
If you ask the prog to produce values there, it makes
it up somehow, but the values don't mean anything.

If you *need* more digits than is held by a double, there
are a few choices for you.

One choice is, obtain a library of math operations that
gives you the number of digits you require. There are
several such on the net. You could start with the
FAQ link posted frequently here. If you go this route,
you need to be prepared to accept the extra run time
it is likely to consume to run a math heavy app.
On many hardwares, doubles are done through a math
co-processor and are quite fast as long as you live
in the range they hold. Going outside this range is
likely to mean overhead. Though this is not a hard
and fast rule, and in many cases the change may
be acceptable or even very small.

Another choice is to refactor your calculation. Suppose
you were able to change the form of your calculation
so that it was something like so: The answer you
want is 999999999998.900 exactly plus some
extra value that is less (in magnitude) than .001.
For example, you might be able to convert it to
a perturbation calculation where you know the main
answer and only need to calculate the difference
due to some (hopefully small) influence. This is a
common method in physics and engineering.
This method probably means a lot of design and
theory effort, and may also increase the complexity
of your app.
Socks

May 8 '06 #7
Dew
Hi Socks,
Good explanation. Certainly it'll help me to work around.

Thanks.

May 10 '06 #8

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

Similar topics

14
by: calan | last post by:
Does anyone have a function that will round a number to 0 or .5? I have a form where I'm entering a number in inches. I need to round it to the nearest 1/2 inch (onChange). The split will be...
193
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I...
2
by: Miguel Morales | last post by:
Hello: Currently, I am trying to solve a problem that requires the calculation of Airy functions for big arguments. Because Airy functions diverge rapidly for positive values, the results of the...
22
by: bq | last post by:
Hello, Two questions related to floating point support: What C compilers for the wintel (MS Windows + x86) platform are C99 compliant as far as <math.h> and <tgmath.h> are concerned? What...
1
by: Reza Nabi | last post by:
Dear All: I have been developing ASP.NET application on MS Access database using ODBC. When I was trying to save more than 255 chars in a Memo field I got the following error. ERROR Invalid...
0
by: AMDRIT | last post by:
I am looking for better concrete examples, as I am a bit dense, on design patterns that facilitate my goals. I have been out to the code project, planet source code, and microsoft's patterns and...
1
by: Charles Vejnar | last post by:
Hi, I have a C library using "long double" numbers. I would like to be able to keep this precision in Python (even if it's not portable) : for the moment I have to cast the "long double" numbers...
4
by: banansol | last post by:
I read the thread Floating point rounding error and I saw Richard Heathfield's description with several lines like: 0.1 = 1/2 = 0.5 - too large 0.01 = 1/4 = 0.25 - too large 0.001 = 1/8 = 0.125...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
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,...
0
Oralloy
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,...
0
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...
0
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...

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.