[Convert from Double to Single]

99 views (last 30 days)
Hello Everyone,
Currently, during Simulation I have a issue, Please help me........
Issue : ( at the command windown)
value = 214748364.7 // Data type is default double.
value_single = single(value) // The result of this convertion is 214748368
my expectation value is value_single = 214748364.7
What is happened ?
Please help me.............!!!
Thank you in advance.
  2 Comments
Shubham Gupta
Shubham Gupta on 15 Nov 2019
I think What you are expecting is not possible for a single type value. It has something to do with the precision for single. Either reduce the value or if you want to use big numbers then I would suggest using double type.
Binh Huynh Thanh
Binh Huynh Thanh on 15 Nov 2019
Thank you Shubham,
Actually , the double type is good solution because it is 8 bytes and single is only 4 bytes. But this is a specical case, I would like to use the single data type. So, I had above issue.
Thank you.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 15 Nov 2019
>> flintmax('single')
ans =
single
16777216
your 214748368 exceeds that, so you have exceeded the point at which single precision floating point numbers start being spaced 1.0 or more further apart.
>> log2(214748364.7)
27.6780719044408
So you would need 28 bits at least just to represent the integer portion. https://en.wikipedia.org/wiki/Single-precision_floating-point_format says that for IEEE 754 single precision, there are only 23 explicit bits available for the fraction; there is also one "hidden" bit, so you can only get 24 bits worth but you need more than 28 bits for the representation.
>> eps(single(214748364.7))
ans =
single
16
  3 Comments
Steven Lord
Steven Lord on 15 Nov 2019
As a metaphor, unless he's talking to his accountants or the IRS Bill Gates probably doesn't describe his fortune down to the last cent. In casual conversation he rounds to a "nice" number. If you were to give him a $1 bill, it doesn't change how he reports his wealth. If you were to give him a million dollars, that might cause him to change his statement of his fortune (or that might require multiple millions.)
The "nice" number to which Gates rounds his wealth is the closest "representable" number to his actual wealth. How much money he would have to gain to change that nice number to the next larger or smaller representable number is eps, and if we operate on the assumption from the first paragraph it's somewhere between 1 and a million.
Since neither of us has as much money as Bill Gates does, our eps values are smaller ;)
Binh Huynh Thanh
Binh Huynh Thanh on 16 Nov 2019
Your thinking is out of my mind

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings 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!