ruby extconf.rb
make
make install
ruby extconf.rb
nmake
nmake install
For the user using Microsoft Visual C/C++ 6.0,the project files are available. Download:
winide143.lzh and decompress it in the directory "../ruby-1.4.3/win32".or
winide143.lzh (or winide16.tar.gz) and decompress it in the directory "../ruby-1.6.x/win32".
require 'BigFloat'
a=BigFloat::new("0.123456789123456789")
b=BigFloat::new("123456.78912345678",40)
c=a+b
a = BigFloat.E(20)
c = a * "0.123456789123456789123456789" # A String is changed to BigFloat object.
is performed normally.
a = BigFloat.E(20)
c = "0.123456789123456789123456789" * a # ERROR
If you actually have any inconvenience about the error above.
You can define a new class derived from String class,
and define coerce method within the new class.
require "BigFloat"
aa = %w(1 -1 +0.0 -0.0 +Infinity -Infinity NaN)
ba = %w(1 -1 +0.0 -0.0 +Infinity -Infinity NaN)
opa = %w(+ - * / <=> > >= < == != <=)
for a in aa
for b in ba
for op in opa
x = BigFloat::new(a)
y = BigFloat::new(b)
eval("ans= x #{op} y;print a,' ',op,' ',b,' ==> ',ans.to_s,\"\n\"")
end
end
end
typedef struct {
unsigned long MaxPrec; // The size of the array frac[]
unsigned long Prec; // Current size of frac[] actually used.
short sign; // Attribute of the value.
// ==0 : NaN
// 1 : +0
// -1 : -0
// 2 : Positive number
// -2 : Negative number
// 3 : +Infinity
// -3 : -Infinity
unsigned short flag; // Control flag
int exponent; // Exponent value(0.xxxx*BASE**exponent)
unsigned long frac[1]; // An araay holding mantissa(Variable)
} Real;
The decimal value 1234.56784321 is represented as(BASE=10000):0.1234 5678 4321*(10000)**1wher frac[0]=1234,frac[1]=5678,frac[2]=4321, Prec=3,sign=2,exponent=1. MaxPrec can be any value greater than or equal to Prec.
file = File::open(....,"r")
s = BigFloat::new("0")
while line = file.gets
s = s + line
end
If the internal representation is binary,translation from decimal to
binary is required and the translation error is inevitable.
For example, 0.1 can not exactly be represented in binary.
e = BigFloat.new("1")
while e + 1.0 != 1.0
e = e / 10
end
Above example continues till all available memories is exhausted.
(Because no round operation is performed on e+1.0)
#
# PI (Calculates 3.1415.... using J. Machin's formula.
#
sig = 2000 # <== Number of significant figures
exp = -sig
sig = sig + sig/100 # no theoretical reason
pi = BigFloat::new("0",sig)
two = BigFloat::new("2")
m25 = BigFloat::new("-0.04")
m57121 = BigFloat::new("-57121")
k = BigFloat::new("1")
w = BigFloat::new("1")
t = BigFloat::new("-80",sig)
v = BigFloat::new("0",sig)
u = BigFloat::new("0",sig)
r = BigFloat::new("0",sig+sig+1)
n1 = 0
n2 = 0
ts = Time::now
while (u.exponent >= exp)
n1 += 1
BigFloat::mult!(v,t,m25)
BigFloat::assign!(t,v,1)
BigFloat::div!(u,r,t,k)
BigFloat::add!(v,pi,u)
BigFloat::assign!(pi,v,1)
BigFloat::add!(w,k,two)
BigFloat::assign!(k,w,1)
end
k = BigFloat::new("1")
w = BigFloat::new("1")
BigFloat::assign!(t,"956",1)
BigFloat::assign!(u,0,1)
while (u.exponent >= exp )
n2 += 1
BigFloat::div!(v,r,t,m57121)
BigFloat::assign!(t,v,1)
BigFloat::div!(u,r,t,k)
BigFloat::add!(v,pi,u)
BigFloat::assign!(pi,v,1)
BigFloat::add!(w,k,two)
BigFloat::assign!(k,w,1)
end
p pi
print "# of iterations = ",n1,"+",n2,"\n"
exit
2.2 Using instance method
#
# PI (Calculates 3.1415.... using J. Machin's formula.
#
sig = 2000 # <== Number of significant figures
exp = -sig
sig = sig + sig/100 # no theoretical reason
pi = BigFloat::new("0")
two = BigFloat::new("2")
m25 = BigFloat::new("-0.04")
m57121 = BigFloat::new("-57121")
n1 = 0
n2 = 0
u = BigFloat::new("1")
k = BigFloat::new("1")
w = BigFloat::new("1")
t = BigFloat::new("-80")
while (u.exponent >= exp)
n1 += 1
t = t*m25
u,r = t.div(k,sig)
pi = pi + u
k = k+two
end
u = BigFloat::new("1")
k = BigFloat::new("1")
w = BigFloat::new("1")
t = BigFloat::new("956")
while (u.exponent >= exp )
n2 += 1
t,r = t.div(m57121,sig)
u,r = t.div(k,sig)
pi = pi + u
k = k+two
end
p pi
print "# of iterations = ",n1,"+",n2,"\n"
exit