Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,262 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,502 people online right now. Registration is fast and FREE... Join Now!




Floating point numbers

 
Reply to this topicStart new topic

Floating point numbers, Read floating point from file

sissy0584
4 Nov, 2006 - 07:34 AM
Post #1

New D.I.C Head
*

Joined: 2 Nov, 2006
Posts: 9


My Contributions
Hi guys,

i have to read a series of coefficients from a file which are in the format -0.00000000000000 (14 digits after the coma) and then to write them in another file.

Actually i'm using a cycle

CODE

for(i=0;i<=n;i++)
{
fscanf_s(file_pointer, "%f", &vector[i]);
fprintf(file_output, "%1.14f \n", vector[i]);
}


and the problem is that in the printed number, the last 6 digits are differents from the original ones... but i cannot use the same %1.14f in the fscanf_s because it raises a syntax error.

So is this problem due to the precision of floating point numbers? If it is so, do you have any clue on how i can solve it?

Thank you so much.

Silvia
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Floating Point Numbers
4 Nov, 2006 - 08:11 AM
Post #2

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,914



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
Interesting...What Syntax Error do you get?
User is offlineProfile CardPM
+Quote Post

horace
RE: Floating Point Numbers
4 Nov, 2006 - 09:00 AM
Post #3

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 4 times
Dream Kudos: 50
My Contributions
The accuracy to which real number calculations can be carried out using a particular real type is determined by the precision, i.e. the maximum number of decimal places that the floating value carries. The standard header file <float.h> provides details on the range and precision of real numbers enabling programs to determine if the specific implementation can support the application.

The following program determines the precision in decimal digits for type float, double and long double.
CODE

#include <stdio.h>
#include <float.h>

int main()
{
    printf("float precision %d \n",  FLT_DIG);
    printf("double precision %d \n",  DBL_DIG);
    printf("long double precision %d \n",  LDBL_DIG);
    getchar();
}

Run using DEV-C++ I get:
float precision 6
double precision 15
long double precision 18

Try running the program and see what the precision of your float variables is. If insufficent use double or long double. However, the greater the precision the larger the storage requirements and longer it takes to execute instructions.


User is offlineProfile CardPM
+Quote Post

sissy0584
RE: Floating Point Numbers
4 Nov, 2006 - 10:39 AM
Post #4

New D.I.C Head
*

Joined: 2 Nov, 2006
Posts: 9


My Contributions
actually it is a debugging problem not a syntax error sorry i was wrong.

it says debug assertion failed

any clue?
User is offlineProfile CardPM
+Quote Post

sissy0584
RE: Floating Point Numbers
4 Nov, 2006 - 11:18 AM
Post #5

New D.I.C Head
*

Joined: 2 Nov, 2006
Posts: 9


My Contributions
i also get the same results about float and double precision... but i need to use float... so how could i do?

any clue?

Thx a lot

Silvia
User is offlineProfile CardPM
+Quote Post

horace
RE: Floating Point Numbers
4 Nov, 2006 - 11:36 AM
Post #6

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 4 times
Dream Kudos: 50
My Contributions
QUOTE(sissy0584 @ 4 Nov, 2006 - 07:18 PM) *

i also get the same results about float and double precision... but i need to use float... so how could i do?

any clue?

Thx a lot

Silvia

if the precision of float is 6 significant digits you can read data with 14 significant digits but the less significant digits will be lost. I don't see any alternative but to use double - is the use of float part of the specification you have been given?

I usually see the opposite problem with engineers reading in real world data accurate to 2 or 3 significant digits, doing calculations and then printing results with 10 or 12 digits.
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Floating Point Numbers
4 Nov, 2006 - 11:54 PM
Post #7

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,914



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
QUOTE
float precision 6 double precision 15 long double precision 18

Darn...How can I ever forget that?
Thanks for reminding wink2.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 07:03PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month