2

I have a text file of values:

133.25 129.40 41.69 2.915

when I read it:

fscanf(File, "%f", &floatNumber[i]);

I get these values:

1.3325000000000000e+002, 1.2939999389648437e+002, 4.1689998626708984e+001 2.9149999618530273e+000

the first value is okay but the other three values why they are different?

Mat
  • 202,337
  • 40
  • 393
  • 406
RBM
  • 73
  • 3
  • 7
  • Is your question about accuracy or about formatting? Your results are in *scientific notation*. Your text file has a different formatting. The computer will do its best to accurately represent the values. – Thomas Matthews Feb 16 '13 at 18:13

4 Answers4

2

The values are the same, you need to change the format specificier in your printf.

Also, floating point numbers have discrete precision, it is therefore not possible to reprenent any arbitrary floating point numbers to infinite accuracy.

This is well-known problem with IEEE spec.

Srikant Krishna
  • 881
  • 6
  • 11
1

They're not different. Floating-point is only accurate to a point [sic]. These are the closest representations of those values. Floating-point is a special beast.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

The reason the values are different is that all numbers except the first one cannot be represented exactly as a binary float value. If you need exact representation of decimals, you need to use a non-standard library.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • No non-standard library can give you exact representation floating-point, right? Either _super-high precision_ or _fixed-point_. Right? Or do they work around imprecisions with cleverness? – Lightness Races in Orbit Feb 16 '13 at 19:07
  • @LightnessRacesinOrbit: "exact representation floating-point" is an oxymoron. There can be implementation using classes representing a wider number of digits, but they will never be exact, except for specific cases. It' not a language or library issue, but a mathematical one. – Emilio Garavaglia Feb 16 '13 at 20:20
  • @EmilioGaravaglia: That is what I thought (and have said elsewhere on this question). – Lightness Races in Orbit Feb 16 '13 at 20:59
  • @LightnessRacesinOrbit Right, I missed "decimal" from my answer. I meant a library for decimal FP, something along the lines of [this proposed addition to the C++ standard](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3407.html). – Sergey Kalinichenko Feb 17 '13 at 12:20
  • @dasblinkenlight: Aha there we go :) – Lightness Races in Orbit Feb 17 '13 at 13:14
0

Although most of your inputs cannot be represented exactly in either format, you would have got a lot more matching digits using double rather than float.

I regard float as a very specialized type. If you have a very large array of low precision floating point data, and are doing only very well behaved calculations on it, you may be able to gain some performance by using float. You get twice as many floats in e.g. a cache line. For anything else, prefer double to float.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75