[Convert from Double to Single]

107 ビュー (過去 30 日間)
Binh Huynh Thanh
Binh Huynh Thanh 2019 年 11 月 15 日
コメント済み: Binh Huynh Thanh 2019 年 11 月 16 日
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 件のコメント
Shubham Gupta
Shubham Gupta 2019 年 11 月 15 日
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 2019 年 11 月 15 日
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.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2019 年 11 月 15 日
>> 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 件のコメント
Steven Lord
Steven Lord 2019 年 11 月 15 日
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 2019 年 11 月 16 日
Your thinking is out of my mind

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by