Home
C#
float Numbers
This page was last reviewed on Sep 7, 2023.
Dot Net Perls
Float. This type represents a floating-point number. The C# language provides this type as a single-precision floating point number representation.
Float details. Float is less precise than a double. It occupies four bytes and is no larger than an int. Float can help maintain compatibility with other programs.
double
Suffixes. You will need to use the character suffix "f" or "F" on constants you want to be treated as float values. If you don't specify "f" or "F", you will get a compile-time error.
Next This program shows how to specify floats with "f" or "F". It is often preferred to use the uppercase F to reduce confusion.
Numeric Suffix
using System; float val1 = 1.001f; // Lowercase f suffix. float val2 = 1.002F; // Uppercase F suffix. Console.WriteLine(val1); Console.WriteLine(val2);
1.001 1.002
Test float. You can convert floats to strings, either with ToString or through Console.WriteLine. You can use negative floating point numbers.
ToString
Console.WriteLine
Detail You can use the == equality operator on floats, and other arithmetic operators.
Info The expression typeof(float) in the program, as well as the GetType method, both return the System.Single type.
Detail The C# language aliases the float keyword to System.Single so they are precisely equivalent.
typeof
Single, Double
Detail The memory usage of floats in the C# language is four bytes per float, which is the same as the int type.
int
using System; // Use float type. float number = 1.5F; Console.WriteLine(number); // Set to negative value. number = -1.001F; Console.WriteLine(number); Console.WriteLine(number == -1.001F); // Use == operator Console.WriteLine(number + 200); // Use + operator Console.WriteLine(number.GetType()); Console.WriteLine(typeof(float)); Console.WriteLine(float.MinValue.ToString("0")); Console.WriteLine(float.MaxValue.ToString("0")); // Find the memory usage for a float value. long bytes1 = GC.GetTotalMemory(false); float[] array = new float[1000 * 1000]; array[0] = 1; long bytes2 = GC.GetTotalMemory(false); Console.WriteLine("{0} bytes per float", ((bytes2 - bytes1) / (1000000)));
1.5 -1.001 True 198.999 System.Single System.Single -340282300000000000000000000000000000000 340282300000000000000000000000000000000 4 bytes per float
Constant fields. This program shows the float.Epsilon constant, which is the smallest float value greater than zero. The NaN constant represents not a number.
And The NegativeInfinity and PositiveInfinity constants are tested with float.IsNegativeInfinity and float.IsPositiveInfinity.
using System; Console.WriteLine(float.Epsilon); Console.WriteLine(float.NaN); Console.WriteLine(float.NegativeInfinity); Console.WriteLine(float.PositiveInfinity);
1.401298E-45 NaN -Infinity Infinity
Epsilon. One major problem with comparing two float values is that they may be almost, but not exactly, equal. Your program probably wants to consider the two values equal.
And This can be solved with an epsilon test. The float.Epsilon constant is the smallest possible float value.
Detail We take the absolute value of the difference between the two values, and see if the difference is less than float.Epsilon.
Math.Abs
Discussion. Typically, if you need to use floating point numbers, using the double type instead is a better choice because it has more precision and is probably more common.
However The double type uses more memory than the float type. In a large array this difference becomes relevant.
Array
Detail Using a float instead of a double can be beneficial if you are sure the loss of precision is immaterial.
Also It is best to match the types closely to existing software to alleviate inconsistencies between different code bases.
Summary. Float is often used for compatibility issues when you need to represent floating-point numbers. Double is usually better for storing large numbers with decimals.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 7, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.