MATLAB: Bin2gray function’s input is decimal.

barneyberbin2grayCommunications Toolboxsersmall furry creatures

if dec2bin converts decimal to binary. and hex2dec converts hexadecimal to decimal. then how come bin2gray converts DECIMAL to gray?

Best Answer

>> why
The terrified engineer wanted it that way.
>> why
Barney suggested it.
>> why
To please a hamster.
It is for the same reason that you can get set to set a set of plates on a table. Some words have multiple meanings, and the meaning depends on context.
dec2bin() and hex2dec() deal with matters of presentation of values to and from characters. bin2gray() deals with error coding theory.
When practical, MATLAB is written to use terms that would be familiar and easily recognizable to the people who work in the field that the function applies to. The point is to be useful to people, not to force them to learn a terminology in which each phrase ("dec") has only a single meaning.
The fault, if there is one, is in the naming of "dec2bin". Perhaps it should have been named something like bindouble2binint322binstr, and then people would learn that dec2bin(42) was too sloppy as a name because it doesn't specifically point out that the 42 was internally represented as the double-precision-interpreted binary 100000001000101000000000000000000000000000000000000000000000000 and needs to be converted to the integer-interpreted 32 bit binary 0000000000000000000000000101010 which in turn needs to be converted (as the main functionality of the routine) to the string '101010'. Those things are important to think about every time one wants to convert a number, right?!
Related Question