2007/04/11

Why (-9.5).round isn't -9?

Ruby
Why (-9.5).round isn't -9?
From irb console, if you type (-9.5).round, you'll get -10 as a result.Why this occur? You can see same answer from Microsoft Excel.So I searched RDoc. And here's the answer.

1. num.round => integerRounds num to the nearest integer. Numeric implements
this by converting itself to a Float and invoking Float#round.


2. flt.round => integerRounds flt to the nearest integer. Equivalent to:

def round

return (self+0.5).floor if self > 0.0

return (self-0.5).ceil if self <0.0

return 0

end

1.5.round #=> 2
(-1.5).round #=> -2


3. num.ceil => integerReturns the smallest Integer greater than or equal to num. Class Numeric achieves this by converting itself to a Float then invoking Float#ceil.

1.ceil #=> 1
1.2.ceil #=> 2
(-1.2).ceil #=> -1
(-1.0).ceil #=> -1


4. flt.ceil => integerReturns the smallest Integer greater than or
equal to flt.
1.2.ceil #=>2
2.0.ceil #=> 2
-1.2).ceil #=> -1
(-2.0).ceil #=> -2


If you want real answer for that, use (-9.5).ceil

(-9.49999999999999999999999999).round #=> -10
(-9.49999999999999999999999999).ceil #=> -9
(9.49).round #=> 9
(9.49999999999999999999).round #=> 10





2007/04/08

Buffon's Needle Simulation through Ruby

I'm reading "Beautiful Evidence" written by Edward Tufte. His view point about information is very amusing. Because we are currently facing tons of informations that forms our decisions. Too much informations lead us to simple conclusions that we didn't intend("Paradox of choice" written by Barry Schwartz). So it's important to consume informations properly. In this context, "Beautiful Evidence" is crucial for us as far as I think. I've done some coding through Ruby to get PI. And visualized it by Gruff. This fundamental simulation is just beginning of my simualtion work. This ruby code isn't polished yet. But you can test Gruff. Thanks Geoffey!

Installing Gruff.
http://nubyonrails.com/pages/gruff


1. Install imagemagick.
Download file at
ftp://ftp.kddlabs.co.jp/graphics/ImageMagick/linux/SRPMS/

2. Install RMagick
From
http://rubyforge.org/frs/?group_id=12
download rmagick-1.15.5.gem and then type
> gem install rmagick-1.15.5.gem --local

3. Install Gruff 설치
Just type
> gem install gruff
While installing it will ask you to install dependency files. Just say yes

> gem install sparklines

4.You need ariel font to use Gruff.
If you don't have it on your system, follow the specific steps from here.
http://tomcopeland.blogs.com/juniordeveloper/2006/02/charts_and_grap.html