Conversion to single is out of range

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.

 Risposta accettata

Stephen23
Stephen23 il 15 Apr 2020
Modificato: Stephen23 il 15 Apr 2020

2 voti

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 Commenti

Ronald Smith
Ronald Smith il 15 Apr 2020
Modificato: Ronald Smith il 15 Apr 2020
Yes, thank you. It really is an issue with how subnormal (or denormalized) numbers are handled. I realized this after I posted, but thanks for responding.
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.

Accedi per commentare.

Più risposte (0)

Prodotti

Release

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by