Converting a fraction into a floating number

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?

5 Comments

"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?
float(object)
We convert some numbers and numerical expressions to floats:
float(17), float(PI/7 + I/4), float(4^(1/3) + sin(7))
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.
John: float() is a legacy Simulink Fixed Point function. https://www.mathworks.com/help/simulink/slref/float.html
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:
float(17), float(PI/7 + I/4), float(4^(1/3) + sin(7))
As copied directly out of this link:

Sign in to comment.

Answers (3)

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.
If I have an equation like (256753367864*t)/(46897532469) How can I convert that to float?

6 Comments

Could you confirm that you want to work at the MuPAD level and convert the expression to a mix of symbolic variables and MuPAD float() numbers?
I suspect that is not what you want. I suspect you want
expr = str2sym('(256753367864*t)/(46897532469)')
expr = 
vpa(expr)
ans = 
Thank you so much for your answer. Str wasn't my mean. I have another question: when I use vpa(equation,4) the accuracy of my calculation will be reduced? (Compare by vpa(equation,32))
format long g
x = rand()
x =
0.645658816961466
xsr = sym(x), xsd = sym(x, 'd')
xsr = 
xsd = 
0.64565881696146565360550084733404
xs4r = vpa(xsr, 2), xs4d = vpa(xsd, 2)
xs4r = 
0.65
xs4d = 
0.65
xs32r = vpa(xsr, 32), xs32d = vpa(xsd, 32)
xs32r = 
0.64565881696146565360550084733404
xs32d = 
0.64565881696146565360550084733404
d4r = double(xs4r), d4d = double(xs4d)
d4r =
0.645658816909418
d4d =
0.645658816961466
d32r = double(xs32r), d32d = double(xs32d)
d32r =
0.645658816961466
d32d =
0.645658816961466
x - d4r, x - d4d
ans =
5.2048143572847e-11
ans =
0
x - d32r, x - d32d
ans =
0
ans =
0
x2 = round(x,2), x32 = round(x, 32)
x2 =
0.65
x32 =
0.645658816961466
x2 - xs4r, x2 - xs4d
ans = 
0.00434118309058248996734619140625
ans = 
0.00434118303853434639449915266596
x32 - xs32r, x32 - xs32d
ans = 
0.0
ans = 
2.1915053423066843181180459459077e-33
There is more than one way that a floating point number can be converted to symbolic. The default, sym(number) is the same as sym(number, 'r') which (mostly) converts to rational-ish numbers; the major alternative is sym(number,'d') which constructs a software decimal number.
If you vpa() one of the rational numbers to a lower number of decimal places, then the result will not be as accurate as the original. If you vpa() one of the 'd' numbers, to a lower number of decimal places, then the result is as accurate as the original.
But vpa() of a rational includes an internal change in form, and the number of decimal places specified tells it when it is okay to give up on the conversion.
We can tell from the above tests that when you vpa() a number that is already in software decimal form, either through sym('d') or by a previous vpa(), then although it changes the result of displaying the value, that the value is retained in full precision internally. I do not know at the moment if the same thing holds if you are working with a large enough number of decimal places.

Thanks again. Indeed because of my expression is more than 25000 character I have to use "vpa" to make them small to observe all of them for copy. In Matlab the the coefficient of variables wrote in rational form and it made my expression very large. Is there any other way to make my expressions smaller than 25000 character?

This is an approach to get some readable representation of the result, not to get the result in MATLAB form. The result will be in the internal MuPAD language

Thanks a lot. I'll test that

Sign in to comment.

Asked:

on 30 Jan 2018

Edited:

on 15 Dec 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!