Wednesday, April 19, 2006

Latex: Sorting and referencing using bibtex

Standard way of referencing in Latex is to put references number is square brackets, ,e.g., [3]. However, sometimes it is necessary to put references in superscript. To do this in TexShop it is enough to include package Natbib and use it like this:\usepackage[super,sort&compress,numbers]{natbib} This results in reference numbers as shown below
instead of standard way:

Wednesday, April 05, 2006

Ruby: Real number precision

If you do some things that involve float/real/double numbers in Ruby, you must lookout for one thing. For example if one write code like this:

#!/opt/local/bin/ruby

(0..5).each {|x|
puts x/2
}

the result will be:

0
0
1
1
2
2

As everyone can see those results are quite incorrect, cause Ruby assumes that x/2 is division of two integer numbers; hence, the result also must be integer. Such small thing, can be very unseen and hard bug to detect, especially if long, mathematical function is written.
Correct version of the above code is:

#!/opt/local/bin/ruby

(0..5).each {|x|
puts x.to_f/2
}

The result finally is ok:

0.0
0.5
1.0
1.5
2.0
2.5


I had this bug lately and it took me quite a time to find out why my program gives incorrect results.