[Convert from Double to Single]

102 vues (au cours des 30 derniers jours)
Binh Huynh Thanh
Binh Huynh Thanh le 15 Nov 2019
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 commentaires
Shubham Gupta
Shubham Gupta le 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 le 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.

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 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 commentaires
Steven Lord
Steven Lord le 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 le 16 Nov 2019
Your thinking is out of my mind

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by