0

When I execute it in C#:

float.TryParse("51778365".ToString(), out x)

it return true and "x" value is 51778364. ¿Why? I don't understund it.

Is it number, why one down?

If use double parse work ok.

Any idea?

My question is why, not how can I resolve it.

I use framework 4.8

Crainar
  • 23
  • 4
  • Use double or even better decimal instead of float. – NineBerry Sep 08 '20 at 17:57
  • because floating point numbers have ***[limited precision](https://learn.microsoft.com/en-gb/dotnet/csharp/language-reference/builtin-types/floating-point-numeric-types)*** - see also https://floating-point-gui.de/ - if you need more precision, use double (which is double the size of a regular float); if you don't ever have fractional values, use int or long. – Franz Gleichmann Sep 08 '20 at 18:01
  • Is not the same, this value is down of max value float. And don't have decimals. Not round. Is not the same. – Crainar Sep 08 '20 at 18:16
  • 4
    It is the same issue, though -- 51 778 365 cannot be represented exactly by a `float`. The largest integer that can be represented exactly is 2^24, 16 777 216. See [here](https://en.wikipedia.org/wiki/Single-precision_floating-point_format#Precision_limitations_on_decimal_values_in_[1,_16777216]). Floating-point numbers start to lose precision long before the maximum value is reached, and you don't need any fractional part before this precision loss starts to happen. – Jeroen Mostert Sep 08 '20 at 18:23
  • 1
    Read the linked information. Floating point values are stored with a mantissa and exponent. – NineBerry Sep 08 '20 at 18:24
  • @NineBerry it's not about how to solve it, it's about why it's happening. I'm sure they know that's wrong and found a way around it. – Hannish Sep 08 '20 at 20:01
  • Is not correct, single float max value is 2^31 = 2 147 483 647 – Crainar Sep 08 '20 at 21:41
  • https://en.wikipedia.org/wiki/Single-precision_floating-point_format#Precision_limitations_on_integer_values – Hans Passant Sep 08 '20 at 22:10
  • @Crainar: no, that's `int`. A `float` can't possibly have the same range as `int` as it's the same size (32 bits) but supports more scaling. You may be confused with `double`, which *is* capable of representing all `int` values exactly (which is why `double.TryParse` does give the exact value). – Jeroen Mostert Sep 09 '20 at 05:54
  • @Jeroen Mostert I understand you perfectly, but that would make sense if when performing the conversion you had to round or perform some complex operation, the number to convert is simple, it only involves converting a simple number to float, there is no reason, not even by buffer, nor by length so that this number does not convert well. At least in the microsoft documentation this is not appreciated. – Crainar Sep 09 '20 at 08:14
  • 1
    The thing is, the engine *does* have to round when parsing this number. It has to, because the number simply does not fit in a `float`. The nearest `float` value to 51 778 365 is indeed 51 778 364 (try [this site](https://www.h-schmidt.net/FloatConverter/IEEE754.html)), so the rounding is correct. You are misled into thinking no rounding is necessary because there are no fractional digits, but it's no different from (say) `0.3`. The Microsoft docs do not intend to explain how IEEE-754 works -- there are plenty of other sources for that. And yes, floating-point is confusing, no way around that. – Jeroen Mostert Sep 09 '20 at 08:23
  • I understund you, on your [link](https://www.h-schmidt.net/FloatConverter/IEEE754.html) site I see it, I understand that the float value is not exact, and there are errors in values when converting, as can be seen clearly in the site you showed us. Thanks @Jeroen Mostert your explanation was great. ;-) A greeting!!! – Crainar Sep 09 '20 at 08:42
  • @JeroenMostert post your last comments as the answer to get it accepted. +1! – Hannish Sep 09 '20 at 12:53
  • @Hannish: the question is properly closed as a duplicate, and no answers can be added to closed questions. – Jeroen Mostert Sep 09 '20 at 13:59

0 Answers0