Tuesday, September 11, 2012

Superstition

According to Merriam-Webster:
"a belief or practice resulting from ignorance, fear of the unknown, trust in magic or chance, or a false conception of causation"
  • "What apparently grounds the widespread respect in which religions of all kinds are held is the sense that those who are religious are well intentioned, trying to lead morally good lives, earnest in their desire not to do evil, and to make amends for their transgressions."
William James in "The Varieties of Religious Experiences":
"There is religious fear, religious love, religious awe, religious joy, and so forth. But religious love is only man's natural emotion of love directed to a religious object; religious fear is only the ordinary fear of commerce, so to speak, the common quaking of the human breast, in so far as the notion of divine retribution may arouse it; religious awe is the same organic thrill which we feel in a forest at twilight, or in a mountain gorge; only this time it comes over us at the though of our supernatural relations."

Friday, September 7, 2012

Web Basics

  • Client (Web Browser) and server (Web Server) usually talks using HTTP over TCP. UDP-based RTP is better for streaming.
  • Web Browser can read HTML, XHTML and can show pages written in these languages.
  • Cascaded Style Sheets (CSS) help separate design (color, font, etc.) from contents in HTML, XHTML, or XML. CSS has three variants: inline, internal, and external. Inline overrides Internal and Internal overrides External, hence cascaded. The cascading allows controlling individual elements (inline) or page (internal) while having a global control (external).
  • XML is mainly used for data structuring and all tags are user-defined.
  • Extensible Stylesheet Language Transformations (XSLT) helps transform XML into Browser-friendly formats.
  • Client-side scripts or "embedded scripts" help make otherwise static pages, dynamic or interactive. They are kind-of like applets.
  • JavaScript and VBScript are the most popular client-side scripts. 
  • Cookie is kind of cache-info put into my machine by a site (through client-side scripts) to which I visited previously. It has a name, value, and expiry date.  
  • Emails are sent to SMTP server and received from POP3 server. 
  • Active Server Pages (ASP), PHP (originally Personal Home Page, nowadays PHP: Hypertext Preprocessor), and Practical Extraction and Reporting Language (PERL) are popular server-side scripting languages. 
  • Common Gateway Interface (CGI): A script or executable program is a CGI script if it is inside of and executable by the server, triggered by the browser, and the result can be displayed on the browser. So, CGI scripts or programs can be written in C/C++, PERL, PHP, and ASP.

Thursday, September 6, 2012

Web Design

Wireshark issue on Ubuntu

After installation of wireshark on ubuntu, it was not showing any network interface to start capturing packets. Did the following:
sudo dpkg-reconfigure wireshark-common
sudo usermod -a -G wireshark $USER
sudo reboot

Tuesday, September 4, 2012

Octave on Ubuntu

  • To run Octave script without getting into Octave environment, type: octave --silent --eval 'myfactorial(5)' where --silent gets rid of some annoying prints regarding warranties, etc.
  • To turn on syntax highlighting for Octave in vi editor: download octave.vim from here, and copy it into /usr/share/vim/vim73/syntax. Inside /usr/share/vim/vim73, you will find filetype.vim, open it and replace all occurrences of the word matlab with octave and save it. 
  • A less geeky editor would be QtOctave.
  • A beautiful site. 

Saturday, September 1, 2012

Fractions, from decimal to binary

Say we have 0.625 and we want to convert it into binary. We can write 0.625 = 1*0.5+0*0.25+1*0.125, so in binary 0.625 becomes .101. But we can do this conversion from decimal fraction to binary fraction in a more systematic way:
0.625 * 2 = 1.25 (1.25 >= 1) so first digit after radix point is 1, remainder 1.25-1.0 = 0.25. Now 0.25 * 2 = 0.5 < 1, so 2nd digit is 0; now 0.5*2 = 1.0 >= 1, so 3rd digit after radix point is again 1 and as now remainder = 1-1 = 0, we are finished. Though the second method is more systematic, it is not very clear how it is equivalent to the first one. In the first one we comare 0.625 with 0.5 and as 0.625 > 0.5, there will be a 1 for 0.5's place after radix point and so on. Now, checking whether 0.625 is greater than or equal 0.5 is equivalent to checking if 2*0.625 is greater than or equal to 2*0.5 = 1.0; next, the remainder is 0.625-0.5 = 0.125, now comparing 0.125 and 0.25 is equivalent to comparing 2*(2*0.625-2*0.5) and 2*(2*0.25) or 2*0.25 and 1.0; and so on...

Halving issue in Binary Search

Here is the way to go. In Java: 
mid = (high+low)/2 may give incorrect result. The maximum positive value (in Java) of int is, 2^(31)-1 or 2147483647 (in binary     0111 1111 1111 1111 1111 1111 1111 1111). The problem is that the intermediate value (high+low) could exceed this maximum int value. For example, if high and low, both are equal to 2^(31)-1, then the intermediate value becomes 

 high        : 0111 1111 1111 1111 1111 1111 1111 1111

+low         : 0111 1111 1111 1111 1111 1111 1111 1111
------------------------------------------------------
Intermediate : 1111 1111 1111 1111 1111 1111 1111 1110

And in 2's complement, this intermediate is


2's complement = 1's complement(Intermediate)+1, hence


(1's complement) 0000 0000 0000 0000 0000 0000 0000 0001

(            +1)                                       1
--------------------------------------------------------
                 0000 0000 0000 0000 0000 0000 0000 0010

So, intermediate = -2. This -2 is like

[1111 1111 1111 1111 1111 1111 1111 11]10, where [...] is the sign extension.
Thus mid = -2/2 = -1, which is incorrect.

If we use mid = low+(high-low)/2, that overflow (intermediate exceeding maximum allowed value of
int) never happens, thus gives correct result.
Another incorrect way is mid = (high+low) >> 1; because the overflow has happened already before the shift and ">>" is the signed shift in Java, thus the intermediate 
[1111 1111 1111 1111 1111 1111 1111 11]10 becomes 
[1111 1111 1111 1111 1111 1111 1111 111]1, which is -1. 
The best way is, mid = (high+low) >>> 1. Because ">>>" is the unsigned shift in Java, the intermediate 
[1111 1111 1111 1111 1111 1111 1111 11]10 becomes
[0111 1111 1111 1111 1111 1111 1111 11]11 which is actually the correct mid (= 2^(31)-1).