Gavri Fernandez ([info]ga_woo) wrote,
@ 2006-05-14 00:15:00
Previous Entry  Add to memories!  Tell a Friend!  Next Entry
Why I buy O'Reilly
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



(Post a new comment)

Just my $0.02
[info]poovanna
2006-05-13 07:34 pm UTC (link)
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.

(Reply to this) (Thread)

Re: Just my $0.02
[info]ga_woo
2006-05-15 07:19 pm UTC (link)
hex and octal operations? :-?

(Reply to this) (Parent)(Thread)

Re: Just my $0.02
[info]poovanna
2006-05-15 07:22 pm UTC (link)
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

(Reply to this) (Parent)(Thread)

Re: Just my $0.02
[info]ga_woo
2006-05-15 07:33 pm UTC (link)
But.......but....

WHY DO YOU THINK THAT? :D It is nowhere near that statement!

(Reply to this) (Parent)(Thread)

Re: Just my $0.02
[info]poovanna
2006-05-15 07:34 pm UTC (link)
What is your interpretation of the statement?

(Reply to this) (Parent)

Re: Just my $0.02
[info]poovanna
2006-05-15 07:39 pm UTC (link)
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.

(Reply to this) (Parent)(Thread)

Re: Just my $0.02
[info]ga_woo
2006-05-15 07:57 pm UTC (link)
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.

(Reply to this) (Parent)(Thread)

Re: Just my $0.02
[info]poovanna
2006-05-15 08:00 pm UTC (link)
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?

(Reply to this) (Parent)(Thread)

Re: Just my $0.02
[info]ga_woo
2006-05-15 08:06 pm UTC (link)
I'm guessing he did

alert(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.

(Reply to this) (Parent)(Thread)

Re: Just my $0.02
[info]poovanna
2006-05-15 08:21 pm UTC (link)
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.

(Reply to this) (Parent)(Thread)

Re: Just my $0.02
[info]ga_woo
2006-05-15 08:29 pm UTC (link)
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

(Reply to this) (Parent)(Thread)

Re: Just my $0.02
[info]poovanna
2006-05-15 08:31 pm UTC (link)
What you call formatting I'm calling converting. That's the source of confusion here ;-)

(Reply to this) (Parent)(Thread)

Re: Just my $0.02
[info]ga_woo
2006-05-15 08:33 pm UTC (link)
But still, the mathematical operation is not doing it. It's the alert() box that did the job.

(Reply to this) (Parent)(Thread)

Re: Just my $0.02
[info]poovanna
2006-05-15 08:42 pm UTC (link)
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() ?

(Reply to this) (Parent)(Thread)

Re: Just my $0.02
[info]ga_woo
2006-05-15 08:53 pm UTC (link)
Uh.. ok. I dunno nothing about Javascript so just curious.

And I'm just realising the stuff I don't know in JS even after working with it for a few months now.

But what result is returned if you type in "Ox2D - Ox19;" without passing it to alert()

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.lang.javascript/browse_thread/thread/38d21acb4d4509ce/ later.

Going to sleep. Don't expect any more replies today :D

(Reply to this) (Parent)(Thread)

Re: Just my $0.02
[info]poovanna
2006-05-15 08:55 pm UTC (link)
Going to sleep. Don't expect any more replies today :D

No No! Just one more! :-D

I meant what's the result like you have 1+3 returning 4. So what would "Ox2D - Ox19;" return?

(Reply to this) (Parent)(Thread)

Re: Just my $0.02
[info]ga_woo
2006-05-15 09:00 pm UTC (link)
1 + 3 does not return 4. It returns a number that can be printed out in decimal format like this -> 4



I 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.

(Reply to this) (Parent)

Re: Just my $0.02
[info]ga_woo
2006-05-16 04:22 pm UTC (link)
To make things clearer:

10 + 3 returns a number that can be printed out
in decimal form like this -> 13,
in hexadecimal form like this -> 0xD
and in octal form like this -> 015

The number that is returned is not tied to any particular representation.

(Reply to this) (Parent)(Thread)

Re: Just my $0.02
[info]poovanna
2006-05-16 04:37 pm UTC (link)
C'mon dude! I get that. All I'm asking is what gets printed out when you input "Ox2D - Ox19;" I want to know if it defaults to decimal, binary, hex.

(Reply to this) (Parent)(Thread)

Re: Just my $0.02
[info]ga_woo
2006-05-16 04:39 pm UTC (link)
Input it where?

(Reply to this) (Parent)(Thread)

Re: Just my $0.02
[info]poovanna
2006-05-16 04:44 pm UTC (link)
I dunno the Javascript syntax, but I was looking at something like this:

var c = Ox2D - Ox19;
print(c);

(Hope you guys have a print function).


(Reply to this) (Parent)(Thread)

Re: Just my $0.02
[info]ga_woo
2006-05-16 04:49 pm UTC (link)
Whatever representation a print() function prints the number in would just depend on how it chooses to do it.

But yeah, all output functions() default to decimal.

(Reply to this) (Parent)(Thread)

Re: Just my $0.02
[info]poovanna
2006-05-16 05:00 pm UTC (link)
Great! The guy's statement all mathematical operations return decimal results. makes sense now doesn't it?

(Reply to this) (Parent)(Thread)

Re: Just my $0.02
[info]ga_woo
2006-05-16 05:00 pm UTC (link)
No.

(Reply to this) (Parent)(Thread)

Re: Just my $0.02
[info]poovanna
2006-05-16 05:10 pm UTC (link)
Why?
Wait, forget it. Anyway, I wish you luck figuring it out.

(Reply to this) (Parent)(Thread)

Re: Just my $0.02
[info]ga_woo
2006-05-16 05:13 pm UTC (link)
"Why?"

http://ga-woo.livejournal.com/48652.html?thread=852748#t852748

(Reply to this) (Parent)

Re: Just my $0.02
[info]caprinus
2006-09-22 07:02 pm UTC (link)
I don't know Javascript, but what you are saying totally makes sense. The books makes a general statement about mathematical operations which has nothing to do with operations. Bad editor!

By the way, this is how the comment thread looks in your style at this point:
http://pics.livejournal.com/caprinus/pic/0000q49a

(Reply to this) (Parent)(Thread)

Re: Just my $0.02
[info]ga_woo
2006-09-22 07:06 pm UTC (link)
I know about the comment thread problem. I want to fix it instead of having to pick a different style. I like this one and I spent quite some time customising it before I noticed the problem. It happens when there is a long thread on a page with no other long threads (because then all the threads fold anyway) Thanks for taking a screenshot and uploading it :)

(Reply to this) (Parent)(Thread)

Re: Just my $0.02
[info]caprinus
2006-09-22 07:23 pm UTC (link)
Aha! So that's why I have never seen this before! (Many journals have similar step-like styles, but I guess only rarely is there only ONE thread which gets very very long. I'd never run across it before)

BTW I come across you rather randomly based on a little comment in [info]littleminx's LJ, and I'll be stalking you from afar for a bit now. Ooh Hyderabad! I've watched Hyderabad Blues. :) I'm always interested in following LJs from across the globe, especially when they are a) clever and b) in a language I can read (not Javascript!), but don't be offended if I wander off again in a while. I'm not sure why I am saying all this. Bored at work. Friday. Procrastinating. Ciao!

(Reply to this) (Parent)(Thread)

Re: Just my $0.02
[info]ga_woo
2006-09-22 07:37 pm UTC (link)
Aha! So that's why I have never seen this before! (Many journals have similar step-like styles, but I guess only rarely is there only ONE thread which gets very very long. I'd never run across it before)

Or maybe it's a problem only with this style, I don't know. Since this style is knew, I think it hasn't been tested all that well.

Ooh Hyderabad! I've watched Hyderabad Blues. :)

I've been here two years now. Don't know the local language. I'm from this place Pondicherry which is further south.

a) clever and b) in a language I can read

Thank you :D

Bored at work. Friday. Procrastinating

I'm still at work and it's saturday morning :)

(Reply to this) (Parent)(Thread)

Re: Just my $0.02
[info]caprinus
2006-09-22 07:45 pm UTC (link)
Pondicherry! I know it! Use to be a French colonial outpost, right?

*is happy to recognize something else, usually people from India tell me "I'm from this little village in Uttar Pradesh" or something and I just stare at them blankly*

(I live in Toronto's "Little India" neighbourhood and am slightly South Asia-obsessed, don't mind my little outbursts of glee about your country.)

(Reply to this) (Parent)(Thread)

Re: Just my $0.02
[info]ga_woo
2006-09-22 08:08 pm UTC (link)
How did you hear about Pondicherry? Some people on the Internet point to the Life of Pi book.

I'm so ignorant about Canada and it's a country that I actually have in my lj-interests (The other one is France) I hear about Toronto quite often and the other city I hear about is Calgary, but that's because I follow a bit of pro-wrestling (Can't believe I'm admitting this :D )

(Reply to this) (Parent)

Re: Just my $0.02
[info]ga_woo
2006-05-15 08:31 pm UTC (link)
"Actually they are in a particular base"

That base is binary.

(Reply to this) (Parent)

Re: Just my $0.02
[info]ga_woo
2006-05-15 08:23 pm UTC (link)
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

(Reply to this) (Parent)


[info]jace
2006-05-14 04:18 pm UTC (link)
Javascript is weak typed -- your variable is either string or number depending on what operator you're applying.

Ergo, (octal string) (numerical plus operator) (octal string) = (decimal string)

I think.

(Reply to this) (Thread)


[info]ga_woo
2006-05-15 07:29 pm UTC (link)
The + operator is also for string concatenation, so the example doesn't work, but considering the - operator

"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.

(Reply to this) (Parent)


[info]vijucat
2006-05-15 06:34 am UTC (link)
So, like, you work with JS?

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".

(Reply to this) (Thread)


[info]ga_woo
2006-05-15 07:32 pm UTC (link)
Yeah, I've never been in a project which had separate Javscript programmers. We do all the coding from the SQL to the CSS.

(It is, for me, but no book, just reading one online article at a time...)

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.

(Reply to this) (Parent)

ALL YOUR BASE ARE BELONG TO 10
[info]andr00
2006-08-22 02:29 am UTC (link)
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!

(Reply to this) (Thread)

Re: ALL YOUR BASE ARE BELONG TO 10
[info]ga_woo
2006-09-06 12:38 pm UTC (link)
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.

(Reply to this) (Parent)


Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…