How does '3' differ to 3?

1 visualizzazione (ultimi 30 giorni)
Jon
Jon il 13 Ott 2013
Risposto: Walter Roberson il 14 Ott 2013
'3' is a string right? Whereas 3 is a number. Now, the absolute value of '3' is 51. How is that?

Risposta accettata

Walter Roberson
Walter Roberson il 14 Ott 2013
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).

Più risposte (1)

sixwwwwww
sixwwwwww il 13 Ott 2013
Modificato: sixwwwwww il 13 Ott 2013
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 Commento
Walter Roberson
Walter Roberson il 14 Ott 2013
To be more correct: MATLAB uses the first 65536 entries of UNICODE, rather than using ASCII.

Accedi per commentare.

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Tag

Non è stata ancora inserito alcun tag.

Community Treasure Hunt

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

Start Hunting!

Translated by