Home > Net >  C# DataTypes Confusion From Documentation
C# DataTypes Confusion From Documentation

Time:11-20

I was going through the documentation for "Floating-point numeric types (C# reference)" at MSDN, https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/floating-point-numeric-types.

It has a table, "Characteristics of the floating-point types," describing the approximate ranges for the different floating datatypes that C# deals with. What I do not understand is why both the MIN and MAX in "Approximate range" column are both positive and negative. Skipping a link click, here is the table,

C# type/keyword Approximate range Precision Size .NET type
float ±1.5 x 10−45 to ±3.4 x 1038 ~6-9 digits 4 bytes System.Single
double ±5.0 × 10−324 to ±1.7 × 10308 ~15-17 digits 8 bytes System.Double
decimal ±1.0 x 10-28 to ±7.9228 x 1028 28-29 digits 16 bytes System.Decimal

Why does the approximate range on both the MIN and MAX have a ±? Should it not be a - for the MIN, and for the MAX, as it does for the Integer type here https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/integral-numeric-types? Maybe I misunderstood something about floating points.

Thank you.

CodePudding user response:

Perhaps it could be made clearer, but this expresses the smallest absolute value that can be expressed as well as the largest absolute value that can be expressed by the given data type. To take an example, if we consider double, it is impossible to represent 3e-324 - it would become approximately 5.0e-324, which is double.Epsilon (https://learn.microsoft.com/en-us/dotnet/api/system.double.epsilon?view=net-7.0).

These values work for both positive and negative values, hence the use of ±.

CodePudding user response:

The important thing here is the sign of the exponent.

The double and float types are used for approximations and should be avoided for exact values. Some programmers make the mistake of using them because there is a performance gain in the execution of complex calculations in relation to the decimal type.

Explaining what I think is confusing you

I'll lower the exponent to make it easier to understand

± is a replecement for "more or less", since float and double types are recommended for approximations.

The first part is for values that are less than one (fractions).

  • ±1.5 x 10^−5 = "more or less" -0,000015

The seconde part is for integers.

  • ±3.4 x 10^5 = "more or less" 340.000
  • Related