Conversion to single is out of range
2 views (last 30 days)
Show older comments
I have an algorithm prototyped in MATLAB that needs to match with the prodcution single precision C-code. I understand the limitations of single vs double and that MATLAB uses double precision internally. What I am struggling with is that the single() type in MATLAB apparently produces values that are out of range for IEEE 754, 32-bit single precision. This causes problems when comparing the output of MATLAB against the output of the C-code.
Check range of single in MATLAB
>> realmax('single')
ans =
single
3.4028e+38
>> realmin('single')
ans =
single
1.1755e-38
>> single(2.0e+39)
ans =
single
Inf
>> single(2.0e-39)
ans =
single
2.0000e-39
As you can see, single() of a small number outside of the valid range returns an invalid value.
0 Comments
Accepted Answer
Stephen23
on 15 Apr 2020
Edited: Stephen23
on 15 Apr 2020
The keyword here is normalized.
Note that the realmin documentation clearly states "Smallest normalized floating-point number" (I added the bold), so what realmin returns is not the smallest possible single value... it is the smallest possible normalized single value... and clearly there are a few subnormal (or denormal) values that are smaller than that!
There are plently of resources on the web that will explain subnormal floating point numbers much better than I can, you might as well start here: https://en.wikipedia.org/wiki/Denormal_number
"single() of a small number outside of the valid range returns an invalid value."
No, that value is a perfectly valid subnormal single floating point number.
"MATLAB apparently produces values that are out of range for IEEE 754, 32-bit single precision"
In fact MATLAB correctly interpreted IEEE 754-2008, which allows for subnormal values. Apparently by default many C compilers do NOT handle denormal floating point numbers according to IEEE 754.
2 Comments
Steven Lord
on 15 Apr 2020
To reinforce what Stephen posted, let's look at four numbers.
% The smallest positive single precision value
% is the distance between 0 and the next number after 0
>> eps(single(0))
ans =
single
1.4013e-45
% Similar for double precision
>> eps(0)
ans =
4.9407e-324
% For comparison, the smallest normalized positive single precision value
>> realmin('single')
ans =
single
1.1755e-38
% And double
>> realmin
ans =
2.2251e-308
But getting back to the original question, whose first sentence was:
I have an algorithm prototyped in MATLAB that needs to match with the prodcution single precision C-code.
You may be interested in generating your single precision C code directly from your MATLAB code. Depending on the functions and functionality you're using, the product MATLAB Coder may be able to do that for you automatically.
More Answers (0)
See Also
Categories
Find more on Logical 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!