Is it possible that the "m. file" code is converted from the double precision to the single precision??
3 views (last 30 days)
Show older comments
I have "sqp.m" file which is attached here.
Is there anyway to convert this "m. file" from the double precision to the single precision??
(I found the below document and tried it, but it doesn't work.)
I wish that a result is "m.file" with 'single precision variables'
Thank you very much.
6 Comments
dpb
on 29 Nov 2021
Edited: dpb
on 29 Nov 2021
A geezer story on single vs double precision -- way back in the dark ages in the mid 70s I attended a conference of American Nuclear Society at which a MS candidate presented a point kernel model of neutron flux distribution in a power reactor to estimate flux at excore detector locations. It just so happened I was in the process of building a model for the same problem at the time so I requested and received a copy of his code for comparison/help in completing the work I was doing.
When I compiled and ran the code on the CDC Cyber76, I got results signficantly different from those he had produced and, unfortunately, on which his MS thesis conclusions were based(*). On exploration for the cause, it turned out he had relied on single precision and run his code on a 32-bit IBM mainframe. The CDC machines were 60-bit so single precision floating point on them was nearly as precise as double precision on the IBM.
The problem was that he accumulated summations from the the various nodes within the reactor at the external locations in a geometrical pattern (logically), but owing to the limited precision, many of the contributions to the total response from the inner assemblies were sufficiently small as to underflow the summation although the aggregation was sufficient overall to modify the response enough as to be in substantial error.
"There be dragons!" that you can't predict without extensive testing and quite likely major rewrites to solve. In the above case the solution could be obtained relatively easily without DP, but it was still much easier to just recompile, of course.
(*) Fortunately, his research thesis topic corporate sponsor and the university were flexible enough as to allow him to go ahead and receive his diploma and resubmit the work results and a revision/errata section to his thesis. It was a little dicey for a while for him, however...
dpb
on 29 Nov 2021
I don't have the coder so can't test/experiment, but looks to me by the error messages it spit out like there are further input parameters to the conversion routine you could specify to let it know the data types of the input arguments and other refinements that could be tried before completely giving up on the conversion process.
Accepted Answer
Jan
on 29 Nov 2021
This function has been developped an tested since 1995. You cannot be sure, if it works correctly with singles as inputs.
converttosingle is an interesting idea. Unfortunately you explain only, that "it doesn't work" without mentioning the details.
With using the debugger, you should be able to find all commands, which create double arrays or rely on the inputs to be doubles. Most of all these are a lot of eps(), ones() and zeros() calls. In addition I find eye() and inf. Some constants as 0, 1 and 2 are occuring also, but they might be used as integers, e.g. in indices.
I would not rely on an automatic conversion, because for such a complicated code this is too fragile.
2 Comments
dpb
on 29 Nov 2021
Agreed wholeheartedly.
http://klaus-schittkowski.de/nlpqlp.htm has the original Fortran source code.
Many historic Fortran math libraries do have both single and double precision implementations so I searched for NLPQLP online. It seems not to have any SP implementations.
More Answers (1)
Andy Bartlett
on 6 Dec 2021
Edited: Andy Bartlett
on 6 Dec 2021
Hi,
The tools for converting double precision floating-point to single precision for MATLAB code and Simulink models work best at converting designs that (aside from numeric efficiency needs like data types) are close to ready for a "specific" embedded usage.
General purpose MATLAB code based on default doubles and designed to handle a very wide set of situations, such as your SQP function, is much less likely to convert well as compared to a design that has been narrowed down to a specific embedded real-time need.
Since the example you provided, sqp, is doing Quadratic Programming, my answer to your other question might be helpful. That answer refers to the Model Predictive Controller that I believe uses Quadratic Programming on the inside and supports single precision sim and code generation.
Andy
0 Comments
See Also
Categories
Find more on Function Creation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!