What is a FLOAT? Floating Point Variables — Programming Theory

Definition of FLOAT

The name FLOAT comes from the "float­ing point" numer­i­cal rep­re­sen­ta­tion sys­tem and refers to a type of prim­i­tive vari­able that is char­ac­ter­ized by being a numer­i­cal vari­able that admits a dec­i­mal part, it can be used to rep­re­sent pos­i­tive and neg­a­tive real num­bers with­in a cer­tain range and with a cer­tain pre­ci­sion that depends on the type of float­ing point vari­able chosen.

Here is a more detailed arti­cle about what are vari­ables in pro­gram­ming and the dif­fer­ent types of vari­ables.

CHECK OUT THIS CRAZY EXPERIMENT

Types of FLOATING POINT variables and ranges

There are dif­fer­ent types of float­ing point vari­ables that we can use in pro­gram­ming, the best known of these vari­ables is the FLOAT itself, in which 32 bits are used to rep­re­sent the float­ing point val­ue, but there are oth­er types of float­ing point vari­ables as well, only with more or less bits for their rep­re­sen­ta­tion. In the fol­low­ing table you can see some of these variables.

Type of variableNum­ber of BitsPre­ci­sionSmall­est rep­re­sentable valueLargest rep­re­sentable value
float32~6 a 9 digits±1.5 x 10−45±3.4 x 1038
dou­ble64~15 a 17 digits±5.0 × 10−324±1.7 × 10308
dec­i­mal128~28 a 29 digits±1.0 x 10-28±7.9 x 1028
Com­par­a­tive table of the dif­fer­ent types of float­ing point vari­ables in C# with num­ber of bits and approx­i­mate range of representation

Syntax of FLOATING POINT variables in Unity

To define a float­ing point vari­able, we use the key­word of the first col­umn fol­lowed by the name of the vari­able, for exam­ple as follows:

float aFloat;
dou­ble aDou­ble;
dec­i­mal aDecimal;

Cuan­do ingre­samos val­ores con dec­i­males se uti­liza el pun­to como sep­a­rador y en el caso par­tic­u­lar de las vari­ables tipo float, cuan­do se le asigna un val­or hay que hac­er­lo se suele incluir la letra "f", para explic­i­tar que el val­or que indicamos es de tipo float, por ejemplo:

When we enter val­ues with dec­i­mals we use the dot as a sep­a­ra­tor and in the par­tic­u­lar case of float type vari­ables, when we assign a val­ue we usu­al­ly include the let­ter "f", to make it explic­it that the val­ue we indi­cate is a float val­ue, for example:

aFloat = 3.51f

In the pre­vi­ous case, as we are assign­ing a dec­i­mal val­ue in a float vari­able, there would be no prob­lem in not includ­ing the "f", how­ev­er in oth­er oper­a­tions such as method exe­cu­tions pass­ing float val­ues as para­me­ters, it may be manda­to­ry to spec­i­fy that such val­ue is a float.

Applications of a FLOATING POINT variable in Unity

The float­ing point vari­ables allow us to rep­re­sent data that belong to the numer­i­cal set of real num­bers, so we can use it to rep­re­sent num­bers that have that nature, for exam­ple weight, tem­per­a­ture, vol­ume, height. They are also used to build more com­plex data struc­tures such as arrays and arrays of float data, pro­gram­ming class­es such as Vector2 and Vector3 or we can also define our own pro­gram­ming class­es and use floats in their inter­nal structure.

YouTube
Scroll to Top
Secured By miniOrange