What is the difference between 5 and '5'

11 Ansichten (letzte 30 Tage)
Jonathan Mendoza
Jonathan Mendoza am 27 Aug. 2019
Kommentiert: Bruno Luong am 30 Aug. 2019
5 + 1 = 6
'5' + 1 = 54
Why does the seceond line equal 54???
What does '' around numbers???
  2 Kommentare
Star Strider
Star Strider am 27 Aug. 2019
To make things even more interesting:
"5" + 1 = "51"
Bruno Luong
Bruno Luong am 30 Aug. 2019
Associativity warning
>> "5"+1+2
ans =
"512"
>> 1+2+"5"
ans =
"35"

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Bruno Luong
Bruno Luong am 27 Aug. 2019
Bearbeitet: Bruno Luong am 27 Aug. 2019
The '5' is the char array. Char array contain a text including number, such as 'Schrodinger has 2 dogs and 1/2 cat'. Char array uses ASCII code internally (actually UNICODE) and the ascii code of '5' is 53, therefore '5'+1 = 53+1 = 54
5 (without quote) is number 5, where the arithmetics operations has standard meaning.
  2 Kommentare
Jonathan Mendoza
Jonathan Mendoza am 27 Aug. 2019
*claps*
Walter Roberson
Walter Roberson am 28 Aug. 2019
There are deep historical reasons why the digits are not the first 10 positions in the character codes. It goes back to Morse code and the fact that Morse originally had no digits, and it goes back to mechanical selector lines...
For a while it looked like BCD or later EBCDIC would become the standard. EBCDIC coded '0' as the character after '9'.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Text Data Preparation finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by