Am reading a Wrox book about Javascript
Inset highlighted "point" in the book:
Even though integers can be represented as octal and hexadecimal literals, all mathematical operations return decimal results.
P.S: Please feel free to embarass me if that statement makes any sense at all
May 13 2006, 19:34:28 UTC 6 years ago
Just my $0.02
I guess it just means that whatever octal and hex operations you perform on decimal numbers, you can convert the answer back to base 10 to get a decimal result.May 15 2006, 19:19:17 UTC 6 years ago
Re: Just my $0.02
hex and octal operations? :-?May 15 2006, 19:22:30 UTC 6 years ago
Re: Just my $0.02
Take two decimal numbers. Convert them to hex. Perform some hex operations like addition etc., Reconvert the answer back to decimal. You see that the decimal and hex results agree.That's what I think :-D
May 15 2006, 19:33:20 UTC 6 years ago
Re: Just my $0.02
But.......but....WHY DO YOU THINK THAT? :D It is nowhere near that statement!
May 15 2006, 19:34:15 UTC 6 years ago
Re: Just my $0.02
What is your interpretation of the statement?May 15 2006, 19:39:02 UTC 6 years ago
Re: Just my $0.02
Because of this line all mathematical operations return decimal results.I was thinking in the context of elementary mathematical operations. Since all hex, octal operations enjoy a one-to-one correspondence with an equivalent decimal operation, that's what led me to make that statement.
May 15 2006, 19:57:12 UTC 6 years ago
Re: Just my $0.02
Hex, oct, dec, these are just ways the programmer can represent numbers in code, display it to the user, or take input from the user.Mathematical operations just work on numbers. If you insist on knowing what the base is in which these operations are done for a typical javascript implementation, it's binary.
0xa + 0xa
10 + 10
in the source code is the same thing.
Once the source code is parsed, these numbers are not stored handled by the interpreter with any representation but binary. Only when we choose to display the result to the user again, do we get to choose between decimal, hex or oct.
Mathematical operations return results. It's how we choose to display that result to the user where hex-dec-oct comes in. For example, the alert() box displays numbers in decimal. The mathematical operations themselves just return numbers with no representation. If you insist on knowing how they are held internally, it's binary.
May 15 2006, 20:00:52 UTC 6 years ago
Re: Just my $0.02
If you insist on knowing how they are held internally, it's binary.Nobody's disputing that. But how else will you interpret his statement?
May 15 2006, 20:06:46 UTC 6 years ago
Re: Just my $0.02
I'm guessing he didalert(Ox2D - Ox19);
saw that the alert-box displayed 20 and decided that the minus operation returns the number in the decimal format (20 instead of 0x14), when actually it is the alert() function that has chosen to display the number in the decimal format.
That's what I'm guessing happened.
May 15 2006, 20:21:24 UTC 6 years ago
Re: Just my $0.02
Your explanation seems likely. Correct me if I'm wrong, what's happening is that Javascript realizes that Ox2D and Ox19 are numbers, albeit non-decimal (that would explain why it's not performing a string concatenation for +) and since there is an arithmetical operator '-', it converts the answer into decimal.May 15 2006, 20:29:24 UTC 6 years ago
Re: Just my $0.02
Nooooo, the operation doesn't convert the answer to decimal. There is no question of a conversion there. Mathematical operations just return numbers, not the print representations of numbers.What I'm claiming is that the alert() box is the function that is formatting the number in decimal, like it is giving us a string of the decimal representation of the number.
Decimal - Hex - Oct are just used to represent numbers for input and output. The operations themselves are base-agnostic. Actually, they work in a particular base, but the results they give are just numbers (represented internally in binary), they are not in any particular format. Only a string/print representation of a number can be in a particular format.
P.S: I'm repeating myself a lot. Maybe because I'm feeling drowsy and need to get some sleep :D
May 15 2006, 20:31:27 UTC 6 years ago
Re: Just my $0.02
What you call formatting I'm calling converting. That's the source of confusion here ;-)May 15 2006, 20:33:07 UTC 6 years ago
Re: Just my $0.02
But still, the mathematical operation is not doing it. It's the alert() box that did the job.May 15 2006, 20:42:56 UTC 6 years ago
Re: Just my $0.02
Uh.. ok. I dunno nothing about Javascript so just curious. But what result is returned if you type in "Ox2D - Ox19;" without passing it to alert() ?May 15 2006, 20:53:12 UTC 6 years ago
Re: Just my $0.02
And I'm just realising the stuff I don't know in JS even after working with it for a few months now.
There is no way to know without some kind of output. Some output functions let us specify in which base we want to see the output, defaulting to decimal.
Actually, I think alert() calls the toString() method of whichever object is passed to it.
And now the fact that floats (or numbers in general) are represented sometimes (or always?) internally as strings is confusing me. Must read this thread http://groups.google.co.in/group/comp.l
Going to sleep. Don't expect any more replies today :D
May 15 2006, 20:55:05 UTC 6 years ago
Re: Just my $0.02
Going to sleep. Don't expect any more replies today :DNo No! Just one more! :-D
I meant what's the result like you have 1+3 returning 4. So what would "Ox2D - Ox19;" return?
May 15 2006, 21:00:55 UTC 6 years ago
Re: Just my $0.02
1 + 3 does not return 4. It returns a number that can be printed out in decimal format like this -> 4I said it this way to make a point. To make you see what I'm trying to say. In the future, I will just say 1 + 3 returns 4. So please don't argue with me then :)
P.S: I'm REEAAAALY off to sleep now.
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
May 15 2006, 20:31:37 UTC 6 years ago
Re: Just my $0.02
"Actually they are in a particular base"That base is binary.
May 15 2006, 20:23:18 UTC 6 years ago
Re: Just my $0.02
Forgot to mention that the book mentions that floats are represented using strings, even internally (I have no idea why). So that statement could (maybe) make sense if it didn't say 'integers'. Even assuming that there's only one data type in javascript called 'number' it's obvious from the rest of what's mentioned in that section of the book that integers are not represented as strings internally.The statement is almost definitely wrong. But really, shouldn't be surprising. Programming book authors are not usually very good :D
May 14 2006, 16:18:01 UTC 6 years ago
Ergo, (octal string) (numerical plus operator) (octal string) = (decimal string)
I think.
May 15 2006, 19:29:23 UTC 6 years ago
"0xa" - "0xb" gives an integer, not a string.
typeof("0xa" - "0xb") == "number"
The statement still doesn't make sense. A number is not decimal, hex or oct. It's just a number.
May 15 2006, 06:34:12 UTC 6 years ago
Or is this part of a larger plan? (It is, for me, but no book, just reading one online article at a time...) Maybe we can write a book together, "Dr. Rajkumar teaches JavaScript".
May 15 2006, 19:32:20 UTC 6 years ago
I do this too. But only because I'm addicted to the computer. The few times I pick up a book, I learn a lot more.
August 22 2006, 02:29:36 UTC 5 years ago
ALL YOUR BASE ARE BELONG TO 10
Ha ha, you're right. I don't know how they came to include that in the book. I could understand including something like, "numeric values are converted to string in decimal by default".This kind of thing just confuses people and makes them feel dumb when it doesn't make any sense. Bad book! Bad!
September 6 2006, 12:38:36 UTC 5 years ago
Re: ALL YOUR BASE ARE BELONG TO 10
This kind of thing just confuses people and makes them feel dumb when it doesn't make any sense.What's worse is that it could make someone who doesn't really know his/her stuff "understand" things the wrong way thinking it all makes sense.