Forums | Admin

Discussion Forums: help

Start New Thread Start New Thread

 

By: Kubo Takehiro
RE: Type NUMBER(3,1) and gem OCI8 [ reply ]  
2011-08-03 13:34
It is a conflict between Oracle's number-to-float logic and ruby's
float-to-string policy.

The Oracle library converts the number 15.7 from decimal number to
double precision floating-point number, whose precision is 15.95 in
decimal digits.
See: http://en.wikipedia.org/wiki/IEEE_754-2008#Basic_formats
15.700000000000001 has 17 digits. The last digit '1' is out of the precision.
Oracle will say, I guess, that they cannot guarantee its decimal string
representation over the precision.

Ruby has a policy for convertion from float to string (decimal number).
See: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/30145
But the round-trips work only when string (decimal number)-to-float
conversion logic is known by ruby.

To fix it by using ruby's decimal-to-float conversion, add the following
code at the head of 'lib/oci8/bindtype.rb' and replace all OCI8::BindType::Float
to OCI8::BindType::Float2 in the file.

class OCI8::BindType::Float2 < OCI8::BindType::OraNumber
def get
onum = super()
onum && onum.to_s.to_f
end
end

By: Devis Clark
Type NUMBER(3,1) and gem OCI8 [ reply ]  
2011-08-01 11:32
I have a field of type: RATE, NOT NULL, NUMBER(3,1).

The table "portfolios" contains one entry. The value of this field is 15.7.

Next I run irb(rails console) and execute commands:

OCI8.new('foobar', 'foobar', 'foobar.lan:1521/FOOBAR').exec('select rate from portfolios where id = 10001') do |r| p r.first; end

Get:
15.700000000000001 (Float class)

If I update a record (sqlplus> update portfolios set rate=18.2 where id = 10001; commit;), then:

>> OCI8.new('foobar', 'foobar', 'foobar.lan:1521/FOOBAR').exec('select rate from portfolios where id = 10001') do |r| p r.first; end
18.2

In what may be the problem?





System:

uname -a
Linux dev 2.6.32-5-amd64 #1 SMP Wed Jan 12 03:40:32 UTC 2011 x86_64 GNU/Linux

cat /etc/issue
Debian GNU/Linux squeeze/sid \n \l

ruby --version
ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-linux]

Ruby installed from a package.

rails=3.0.9
ruby-oci8=2.0.6