[Convert from Double to Single]

조회 수: 99 (최근 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 CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by