How does '3' differ to 3?

조회 수: 1 (최근 30일)
Jon
Jon 2013년 10월 13일
답변: Walter Roberson 2013년 10월 14일
'3' is a string right? Whereas 3 is a number. Now, the absolute value of '3' is 51. How is that?

채택된 답변

Walter Roberson
Walter Roberson 2013년 10월 14일
A character such as '3' is just a drawing that has no inherent meaning. If everyone agreed that 'Q' was the proper drawing for the third non-negative integer, then using 1, 2, Q, 4, 5 would be just as meaningful as 1, 2, 3, 4, 5. It is a symbol, nothing more, and it is what the symbol represents is what is important. When you use '3' what that represents is just that that particular shape should be displayed (or printed or whatever). There does not need to be any connection between '3' and the third shape in any agreed list of shapes: the shapes can be ordered arbitrarily as long as everyone agrees on what idea each shape designates.
Once an arbitrary ordering of shapes and associated ideas is constructed, it can be convenient to refer to the position within the arbitrary ordering. So if the shape '3' happens to be in the 52nd position and you happen to start counting the positions from 0, then you can refer to "shape 51" to designate the shape '3'. And then you can start building tables of properties that tie position 51 to some meaning. For example, you might have a table that designates whether each shape position is associated an upper case character, a lower case character, a punctuation mark, some form of what spacing, and every once in a while, you might even encounter a shape position that is associated with a digit.
It so happened that in the history of computing, it was mechanically easier to not use the third possible location in the table to designate the shape '3'. And I do mean mechanically easier. If the person who built the first model had been left-handed instead of right-handed the arbitrary order of the shapes might have ended up differently than it did.
The association between the shape '3' and the third positive integer is done through software. Some of the routines that handle that conversion are fscanf() and str2double(). But abs() is not defined as converting between shapes and integers they might designate: abs() just deals with internal representations such as the ones that happened to put the shape '3' into the 52nd position (position 51 when you start counting from 0).

추가 답변 (1개)

sixwwwwww
sixwwwwww 2013년 10월 13일
편집: sixwwwwww 2013년 10월 13일
Dear Jonagorn, 3 is a number however '3' is a character. So when you try to get numeric value of a character then MATLAB converts the character to its equal ASCII value and display that numeric value. So numeric value of character '3' is 51 which MATLAB shows. You can see complete set of ASCII values and equivalent characters here: http://en.wikipedia.org/wiki/ASCII
  댓글 수: 1
Walter Roberson
Walter Roberson 2013년 10월 14일
To be more correct: MATLAB uses the first 65536 entries of UNICODE, rather than using ASCII.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by