Floating point numbers

Read floating point from file

Page 1 of 1

6 Replies - 7468 Views - Last Post: 05 November 2006 - 12:54 AM Rate Topic: -----

#1 sissy0584  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 02-November 06

Floating point numbers

Post icon  Posted 04 November 2006 - 08:34 AM

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

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

Is This A Good Question/Topic? 0
  • +

Replies To: Floating point numbers

#2 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Floating point numbers

Posted 04 November 2006 - 09:11 AM

Interesting...What Syntax Error do you get?
Was This Post Helpful? 0
  • +
  • -

#3 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,898
  • Joined: 25-October 06

Re: Floating point numbers

Posted 04 November 2006 - 10:00 AM

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.
#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.
Was This Post Helpful? 1

#4 sissy0584  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 02-November 06

Re: Floating point numbers

Posted 04 November 2006 - 11:39 AM

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

it says debug assertion failed

any clue?
Was This Post Helpful? 0
  • +
  • -

#5 sissy0584  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 02-November 06

Re: Floating point numbers

Post icon  Posted 04 November 2006 - 12: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
Was This Post Helpful? 0
  • +
  • -

#6 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,898
  • Joined: 25-October 06

Re: Floating point numbers

Posted 04 November 2006 - 12:36 PM

View Postsissy0584, on 4 Nov, 2006 - 07:18 PM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#7 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Floating point numbers

Posted 05 November 2006 - 12:54 AM

Quote

float precision 6 double precision 15 long double precision 18

Darn...How can I ever forget that?
Thanks for reminding ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1