Converts the argument to a 32-bit floating point number. The syntax for its use is
y = float(x)
where x
is an n
-dimensional numerical array. Conversion follows the general C rules. Note that both NaN
and Inf
are both preserved under type conversion.
The following piece of code demonstrates several uses of float
. First, we convert from an integer (the argument is an integer because no decimal is present):
--> float(200) ans = <float> - size: [1 1] 200.00000
In the next example, a double precision argument is passed in (the presence of a decimal without the f
suffix implies double precision).
--> float(400.0) ans = <float> - size: [1 1] 400.00000
In the next example, a dcomplex argument is passed in. The result is the real part of the argument, and in this context, float
is equivalent to the function real
.
--> float(3.0+4.0*i) ans = <float> - size: [1 1] 3.0000000
In the next example, a string argument is passed in. The string argument is converted into an integer array corresponding to the ASCII values of each character.
--> float('helo') ans = <float> - size: [1 4] Columns 1 to 3 104.00000 101.00000 108.00000 Columns 4 to 4 111.00000
In the last example, a cell-array is passed in. For cell-arrays and structure arrays, the result is an error.
--> float({4}) Error: Cannot convert cell-arrays to any other type. at built-in function float