Recently in Programming Category

~/.vimrc를 다음과 같이 설정하면
파일을 utf-8 혹은 euc-kr의 타입에 맞게 open하고 그것에 맞게 저장한다. 아주 유용함.


set nu
set smarttab
set smartindent
set showmatch
set tabstop=4
set ts=4
set sw=4
syntax on

if v:lang =~ "^ko"
  set encoding=cp949
  set fileencodings=utf-8,cp949
  set guifontset=-*-*-medium-r-normal--16-*-*-*-*-*-*-*
elseif v:lang =~ "^ja_JP"
  set fileencodings=euc-jp
  set guifontset=-misc-fixed-medium-r-normal--14-*-*-*-*-*-*-*
elseif v:lang =~ "^zh_TW"
  set fileencodings=big5
  set guifontset=-sony-fixed-medium-r-normal--16-150-75-75-c-80-iso8859-1,-taipei-fixed-medium-r-normal--16-150-75-75-c-160-big5-0
elseif v:lang =~ "^zh_CN"
  set fileencodings=gb2312
  set guifontset=*-r-*
endif
if v:lang =~ "utf8$" || v:lang =~ "UTF-8$"
  set encoding=utf-8
  set fileencodings=utf-8,cp949
endif


set backspace=eol,start,indent

filetype on

set background=dark

colorscheme evening

set nobackup

java.math.BigDecimal

| 1 Comment | No TrackBacks

java.math.BigDecimal의 바른 사용에 대해서-2


또 쏘장입니다.

 

이전 글에서도 언급을 했지만 개발시 계산을 할 때 보통 primitive date type을 사용해서 계산을 하고, 아주 정확한 계산을 할 때 java.math.BigDecimal을 사용한다고 했습니다.

오늘도 정확한 계산을 위해 사용하는 BigDecimal을 잘 못 사용하고 있는 경우를 발견해서 이전글과 연결하여 포스팅 해봅니다.

 

만일 다음과 같은 값이 있다고 가정을 하고 나누기를 해봅니다.

double d1 = 1.0;

double d2 = .3;

 

d1 / d2 ==> 3.3333333333333335라는 결과가 나옵니다.(이건 제 노트북에서 나온 값이구요. 다른시스템 다른 OS에서는 다른 값이 나올 수도 있습니다.)

이상 없군요. ^^;

 

이제 동일한 값을 BigDecimal로 처리해봅시다.

BigDecimal b1 = BigDecimal.valueOf(1.0);

BigDecimal b2 = BigDecimal.valueOf(.3);

 

b1.divide(b2);

==> java.lang.ArithmeticException을 토해내는군요. (Non-terminating decimal expansion; no exact representable decimal result.)

이유는 계산에 끝이 보이지 않기 때문이라지요. 3.33333333333333333333333333333.....이 무한으로 나가는 경우입니다.

이런 경우가 자주 발생하지 않는 경우라도 한 번 발생하면 대책이 없지요. 소위 말하는 "시.한.폭.탄"이라고나 할까요.

 

오늘 발견한 잘 못 사용한 예도 바로 이 경우였습니다.

어 쩌다 한 번 나올까 말까한 로직에서 나와버린 거지요. ^^;

 

그러면 어떻게 하면 될까요.

BigDecimal을 사용해서 나누기(java.math.BigDecimal#divide())를 할 때는 반드시 소수 몇 째자리까지 계산을 할 것인지 지정을 해주어야 합니다.

 

아래와 같이 말이지요.

b1.divide(b2, MathContext.DECIMAL32); 처럼요.

MathContext에는 static으로 DECIMAL32,64,128이 선언이 되어있고 정밀도는 32-7, 64-16, 128-34로 지정이 되어있습니다. 만일 그 이상의 정확도를 요하는 경우라면 직접 new MathContext(256)처럼 정밀도를 지정해주시면 됩니다.

 

BigDecimal을 이용한 나누기 얘기가 나온김에 나누기에 관한 method도 한 번 살펴봅시다.

  • b1.divide(b2, BigDecimal.ROUND_HALF_UP);
    이경우는 수수자리는 버려진다고 보시면 됩니다. round mode는 BigDecimal의 상수를 참조하세요.
  • b1.divide(b2, RoundingMode.HALF_UP);
    RoundingMode enum에서 사용하는 상수는 BigDecimal에 정의 된 상수와 같습니다. 살펴보니. HALF_UP(BigDecimal.HALF_UP)으로 되어 있더군요.
  • b1.divide(b2, MathContext.DECIMAL32);
    정밀도를 계산결과 소수 6자리로 지정한 경우입니다. 이 경우는 3.333333이 나오겠지요.
    정밀도는 이전에 언급 된 내용을 참고하세요.
  • b1.divide(b2, new MathContext(256));
    이 경우는 정밀도를 직접 지정한 경우입니다.
  • b1.divide(b2, 16, BigDecimal.ROUND_HALF_UP);
    이 경우는 소수 16자리까지 scale을 하는 경우입니다. 소수 17자리에서 반올림됩니다.
  • b1.divide(b2, 16, RoundingMode.HALF_UP));
    위의 경우와 같습니다. 다만 rounding mode를 enum으로 사용하고 있을 뿐입니다.

이상으로 java.math.BigDecimal#divide()에 대해 살펴보았습니다.

 

뭐든지 잘 쓰면 약이 되고 잘 못 쓰면 독이됩니다.

항상 명심하시고 개발해주시길 바랍니다.

오늘도 즐거운 하루 보내시길...

이상 쏘장이었습니다.



[출처] java.math.BigDecimal 의 바른 사용에 대해서-2|작성자 아론

1. when you install cygwin, you must be selected ruby, python package.

2. if you want to install rubygem,
    first download 'rubygem' here ( http://rubyforge.org/projects/rubygems/ )

3.then unpack rubygem and move the folder. just type below,
  
  ruby setup.rb install


14:07:46 Wed Aug 05 [094061@~/rubygems-1.3.5]
$ ruby setup.rb install
RubyGems 1.3.5 installed

=== 1.3.5 / 2009-07-21

bug fixes:

* Fix use of prerelease gems.
* Gem.bin_path no longer escapes path with spaces. Bug #25935 and #26458.

Deprecation Notices:

* Bulk index update is no longer supported (the code currently remains, but not
  the tests)
* Gem::manage_gems was removed in 1.3.3.
* Time::today was removed in 1.3.3.


------------------------------------------------------------------------------

RubyGems installed the following executables:
        /usr/bin/gem


14:08:33 Wed Aug 05 [094061@~/rubygems-1.3.5]
$


4. it's done.

 Enjoy RubuGem

vi ~/.inputrc

아래의 내용을 추가한다.

set meta-flag on
set input-meta on
set convert-meta off
set output-meta on
set completion-ignore-case on



만약 디렉토리에 한글명이 안보인다면

vi ~/.bashrc

아래의 내용을 추가한다.

alias ls='ls -F --color=auto --show-control-chars'
alias ll='ls -al -F --color=auto --show-control-chars'

1. 일단 diffutil이 설치되어있는지 cygwin install파일을 보고 확인해본다. 만약 설치 안되어있다면 설치한다.

2. ~/.vimrc파일이 있다. 여기에 다음과 같은 부분을 추가한다.

set diffexpr=MyDiff()
function MyDiff()
  let opt = ''
  if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
  if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
  silent execute '!diff -a ' . opt . v:fname_in . ' ' . v:fname_new . ' > ' . v:fname_out
endfunction


그럼 완료.


1. Install cygwin
http://www.cygwin.com/

그러면 윈도우를 유닉스콘솔처럼 사용할 수 있다.

(설치할때 vi가 default가 아니니 꼭 체크해서 설치하도록 하자)



하지만!! 너무너무 불편한 콘솔!

copy & paste 도 안되고 폰트설정 및 스크롤도 안좋다.

그래서 cygwin에서 사용하기 편안한 콘솔프로그램을 설치하자. 물론 이건 무료다.

2.Install tera term console
http://ttssh2.sourceforge.jp/


MySql Connect Problem

| 1 Comment | No TrackBacks
Tomcat6.0, Mysql Server version: 5.0.77-1 (Debian)

1. Error
    // Cannot connect to MySQL server on localhost:3307. Is there a MySQL server running on the machine/port you are trying to connect to? (java.security.AccessControlException)


2. Solution
edit this file and add bold line like below.
debian:/var/lib/tomcat6/conf/policy.d/04webapps.policy


    permission java.io.FilePermission "${java.io.tmpdir}/-", "read,write,delete";

    // Cannot connect to MySQL server on localhost:3307. Is there a MySQL server running on the machine/port you are trying to connect to? (java.security.Acc
    permission java.net.SocketPermission "127.0.0.1:3307", "connect,resolve";
};



1. Error
Communication failure during handshake. Is there a server running on 127.0.0.1:3307?


2.Solution
replace your JDBC driver .
in my case below.

WEB-INF/lib/mm.mysql-2.0.14-bin.jar -> mysql-connector-java-5.1.7-bin.jar


"mysql-connector-java-5.1.7-bin.jar" download link...
http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.7.tar.gz/from/http://mysql.byungsoo.net/




:)

Newyork Times Article

Windows Is So Slow, but Why?



요약하면 윈도우가 95,98,2000,XP 점점 발전하면서 그 이전 코드들과의 상용성을 가지기 위해 하다보니 코드수가 점점 더 증가했고 점점 더 느려지며 복잡한 구조로 됐다고 한다.

VISTA가 그것을 깨트리기 위해 새로운 시도를 해서 윈도우는 좋아졌으나 그 이전 윈도우와의 호환성이 떨어져 실패를 했다.

이번에 나오는 Windows 7은 더더욱 새로운 개념의 OS라 한다.

모든 것이 Virtual Machine형태로 되어있어 OS는 그 VM을 돌리는 형태라나.

그래서 OS자체는 무척이나 가볍고 프로그램을 뜨울때는 VM을 같이 띄우게 되니

동시에 사용하는 프로그램이 많을 수록 느려질 것이라 생각된다.


위 기사는 관심있는 사람이라면 한번쯤 꼭 읽어볼 만한 기사다. Mac OSX에 대한 이야기도 나온다.

1. Install apache
> aptitude install apache2


apache home dir : /etc/apache2/
default www home dir : /var/www


2. Debian apache install python module
> aptitude install libapache2-mod-python


3.
> vi /etc/apache2/sites-available/default
[...]
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
AddHandler mod_python .py
PythonHandler mod_python.publisher
PythonDebug On

</Directory>
[...]

4.
> /etc/init.d/apache2 restart
.vimrc modify like below
au BufRead,BufNewFile *.py set ts=8 sw=4 softtabstop=4 expandtab
au BufRead,BufNewFile *.py set smarttab smartindent sta
au BufRead,BufNewFile Makefile set ts=8 sts=8 sw=8 noet


relate article - http://bbs.python.or.kr/viewtopic.php?t=21457