C# language uses float variable type to store real number values in floating point (negative and positive numbers that contain a fractional part). The C# compiler will allocate 32 bits (4 bytes) to store values of type float. A float variable type can store values with a precision of six or seven decimals in the interval of 3.4E-38 and 3.4E+38.
The C# language keeps the value on a mantissa of 23 bits (which contains the fractional part) and an exponent of 8 bits (which contains the power with which the number is multiplied) and a single bit of sign (which tells the compiler if the value is positive or negative). In other words, if a variable contains the value 3.4E+38, the sign bit will be 0, which indicates a positive number, the 23 bits mantissa will contain a binary representation of number 3.4, and the 8 bit exponent will contain a binary representation of 1038
Additional Information
If you want a numeric real literal to be treated as float, use the suffix f or F, for example:
1 |
float myFloatVariable = 157.39F; |
Without the suffix f, the number is treated as a double and generates a compiler error.
The concepts explained in this lesson are also shown visually as part of the following video:
EXERCISES
1. Write an expression that calculates the area of a trapezoid by given sides a, b and height h.
Solution
Guidelines: The formula for trapezoid surface is: S = (a + b) * h / 2.
Tags: float variable, variable types, variables