The single function is not behaving as expected

122 views (last 30 days)
Leon
Leon on 20 Nov 2025 at 23:42
Edited: Stephen23 on 23 Nov 2025 at 6:08
I'm trying to use single to save disk space because my files are very large. I know it could affect the precision if my numbers have many places after the decimal point. However, it is altering my integers as well.
>> single(340580097)
ans =
340580096.00
Is this a bug?

Accepted Answer

Matt J
Matt J on 20 Nov 2025 at 23:46
Edited: Matt J on 20 Nov 2025 at 23:53
No, it is not a bug. The precision of a number isn't something that is measured only by the digits to the right of the decimal point. It is measured by the total number of digits present. Single float precision only has about 7 digits of precision, and your integer has more digits than that.
  10 Comments
Sam Chak
Sam Chak on 22 Nov 2025 at 6:06
You know that single-precision numbers use exactly half the storage of double-precision numbers, reducing memory by 50%. What you might not know is that the conversion is really about how many significant digits a type can represent, which applies to the whole number (integer and fractional parts), not just the decimal part.
We don't know the context for converting the large dataset to single precision. If the goal is just to save space, consider storing values that only need about 7 significant digits in single precision and keeping higher-accuracy values in double precision. That way you can cut overall memory use compared with storing everything as double.
Because MATLAB doesn't automatically change a variable's storage size, you'd need to implement a class (see classdef) that stores values based on their "valid significant digits." I don't know whether mixed-precision storage would affect the accuracy of later calculations that mix single and double. You'll need test cases to verify!
If the goal is deep-learning training, check whether the MATLAB Deep Learning Toolbox supports automatic mixed-precision training. User @xingxingcui asked about this in 2021 but, as of now, there have been no technical replies.
xingxingcui
xingxingcui on 22 Nov 2025 at 7:33

It's been almost five years, and from what I understand now, the Deep Learning Toolbox still doesn't support mixed-precision training. Has TMW not noticed this issue?

Sign in to comment.

More Answers (1)

Stephen23
Stephen23 on 22 Nov 2025 at 6:16
Moved: Matt J on 22 Nov 2025 at 13:46
"However, it is altering my integers as well."
Then use an integer type of sufficient size to store those values; this would use the least possible memory.
  8 Comments
Steven Lord
Steven Lord on 22 Nov 2025 at 16:51
Is there a reason Matlab can not program the single() function so that it can automatically identify large integers and use int32 instead?
If you order a hamburger at a restaurant, would you be surprised / angry / insulted if the waiter looked at you and decided to serve you a salad instead?
To answer your question directly, there's no technical reason preventing MATLAB from doing that.
But if you explicitly ask MATLAB for a single precision value, I think most of our users would be very surprised to get something returned from that call to single that wasn't a single precision value. Users would report that as a silent wrong answer bug (the most severe type of bug, worse in many ways than MATLAB crashing; at least if MATLAB crashes it's obvious something went wrong) the second it went out the door in the Prerelease.
If a developer proposed making that change, most of the people reviewing that proposed change (and I would almost certainly be asked to review it, and this would be my review) would give it an OMDB (aka "You're shipping this Over My Dead Body.")
Stephen23
Stephen23 on 23 Nov 2025 at 5:31
Edited: Stephen23 on 23 Nov 2025 at 6:08
"Is there a reason Matlab can not program the single() function so that it can automatically identify large integers and use int32 instead?"
Yes, there are very good reasons:
1) you would force single() to be sloooooow. Instead of simply converting to one homogenous array (which can be preallocated by the MATLAB engine and then filled out with the values) you would force the MATLAB engine to either a) use a heterogenous array (i.e. container array) which is much slower to access OR b) parse all values into one array until it gets to a value which does not "fit" at which point it needs to create a new array and copy all of the existing homogenous array into a new memory location and then continue OR c) perform some other so-far-undefined-behavior. All of these will be significantly slower than using one homogenous array.
2) you would break an enormous amount of existing code. If single() could return int32, uint32, single, or any other type depending on input values, then every single line of code that calls single() would need defensive type checking. Consider:
data = single(userInput);
result = data * 0.5; % This breaks if data is int32!
Your proposal would break billions of lines of existing MATLAB code that reasonably expects single() to return single. The entire language's type system would become unreliable, calling external libraries and compiled code would be extremely fragile. Functions must have predictable return types, otherwise composition of operations becomes impossible.
3) Dispatch/overloading nightmare: MATLAB uses dynamic dispatch based on argument types. If single() returns different types based on values rather than input types, you break fundamental language semantics. Consider:
function process(x)
y = single(x);
% What methods are available on y?
% What operations are valid?
% This becomes undecidable without runtime value inspection
end
Method resolution, operator overloading, and function dispatch all assume type stability within a given code path. Value-dependent type returns would require the JIT compiler to give up all optimizations, making ALL MATLAB code significantly slower, not just single().
4) The documentation would be an opaque nightmare: "Returns single, unless the value is an integer that doesn't fit in single precision, in which case it returns int32, unless it's negative and too large, then int64, unless it is complex in which case return a unicorn..."
As Steven Lord described, explicitly requesting one thing and being given something completely different is a guarantee that the function is broken, not improved. Every function call site would need type guards. Generic algorithms would be impossible. Code review would require analyzing every possible value path to determine runtime types. This isn't "helpful automation," it's the destruction of language predictability - the very foundation that makes programming possible.

Sign in to comment.

Tags

Products


Release

R2025b

Community Treasure Hunt

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

Start Hunting!