Asked by Samuel Généreux
on 30 Jan 2018 at 0:34

Hi, I want to simply convert a fraction into a float. When I checked with the help, it seems really simple. To convert 2/5 into a float, it seems like I just need to type float(2/5). But, when I do that, I have for response :Error using float (line 46) The input argument to float was not a supported type. The only recognized input character vectors are 'single' and 'double'. The input type was 'double'. Can someone explain me that?

Answer by John D'Errico
on 30 Jan 2018 at 0:38

No. There is no need to use float.

x = 2/5 x = 0.4

whos x Name Size Bytes Class Attributes

x 1x1 8 double

x is automatically a floating point number, of class double.

Opportunities for recent engineering grads.

Apply Today
## 6 Comments

## Stephen Cobeldick (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/379666-converting-a-fraction-into-a-floating-number#comment_529903

"When I checked with the help, it seems really simple. To convert 2/5 into a float, it seems like I just need to type float(2/5)"Can you please show a reference to the MATLAB help, where that conversion is stated?

## Samuel Généreux (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/379666-converting-a-fraction-into-a-floating-number#comment_530015

We convert some numbers and numerical expressions to floats:

## John D'Errico (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/379666-converting-a-fraction-into-a-floating-number#comment_530031

I assume you mean the MuPAD function float. float itself is NOT part of MATLAB proper. There is no MATLAB function called float, nor do you use float to create a floating point number in MATLAB, as I said in my answer.

Numbers are automatically assumed to be cast as floating point numbers by default in MATLAB.

## Walter Roberson (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/379666-converting-a-fraction-into-a-floating-number#comment_530037

John: float() is a legacy Simulink Fixed Point function. https://www.mathworks.com/help/simulink/slref/float.html

## John D'Errico (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/379666-converting-a-fraction-into-a-floating-number#comment_530065

No. The examples given by Samuel are NOT consistent with the use of float as it is seen in the Simulink docs. However, they ARE consistent with the examples shown in the MuPAD documentation. That is why I talked about MuPAD. In fact, if I read the examples shown for the MuPAD float, we find very coincidentally these examples:

As copied directly out of this link:

https://www.mathworks.com/help/symbolic/mupad_ref/float.html?searchHighlight=float&s_tid=doc_srchtitle

## Walter Roberson (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/379666-converting-a-fraction-into-a-floating-number#comment_530076

John, you said that there is no MATLAB function called float, but there is a function by that name. It does not happen to be the function that the user wants, but

somefunction with that name exists.Log in to comment.