Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/ruby22/lib64/ruby/2.2.0
File: mathn.rb
#--
[0] Fix | Delete
# $Release Version: 0.5 $
[1] Fix | Delete
# $Revision: 1.1.1.1.4.1 $
[2] Fix | Delete
[3] Fix | Delete
##
[4] Fix | Delete
# = mathn
[5] Fix | Delete
#
[6] Fix | Delete
# mathn serves to make mathematical operations more precise in Ruby
[7] Fix | Delete
# and to integrate other mathematical standard libraries.
[8] Fix | Delete
#
[9] Fix | Delete
# Without mathn:
[10] Fix | Delete
#
[11] Fix | Delete
# 3 / 2 => 1 # Integer
[12] Fix | Delete
#
[13] Fix | Delete
# With mathn:
[14] Fix | Delete
#
[15] Fix | Delete
# 3 / 2 => 3/2 # Rational
[16] Fix | Delete
#
[17] Fix | Delete
# mathn keeps value in exact terms.
[18] Fix | Delete
#
[19] Fix | Delete
# Without mathn:
[20] Fix | Delete
#
[21] Fix | Delete
# 20 / 9 * 3 * 14 / 7 * 3 / 2 # => 18
[22] Fix | Delete
#
[23] Fix | Delete
# With mathn:
[24] Fix | Delete
#
[25] Fix | Delete
# 20 / 9 * 3 * 14 / 7 * 3 / 2 # => 20
[26] Fix | Delete
#
[27] Fix | Delete
#
[28] Fix | Delete
# When you require 'mathn', the libraries for Prime, CMath, Matrix and Vector
[29] Fix | Delete
# are also loaded.
[30] Fix | Delete
#
[31] Fix | Delete
# == Copyright
[32] Fix | Delete
#
[33] Fix | Delete
# Author: Keiju ISHITSUKA (SHL Japan Inc.)
[34] Fix | Delete
#--
[35] Fix | Delete
# class Numeric follows to make this documentation findable in a reasonable
[36] Fix | Delete
# location
[37] Fix | Delete
[38] Fix | Delete
warn('lib/mathn.rb is deprecated') if $VERBOSE
[39] Fix | Delete
[40] Fix | Delete
class Numeric; end
[41] Fix | Delete
[42] Fix | Delete
require "cmath.rb"
[43] Fix | Delete
require "matrix.rb"
[44] Fix | Delete
require "prime.rb"
[45] Fix | Delete
[46] Fix | Delete
require "mathn/rational"
[47] Fix | Delete
require "mathn/complex"
[48] Fix | Delete
[49] Fix | Delete
unless defined?(Math.exp!)
[50] Fix | Delete
Object.instance_eval{remove_const :Math}
[51] Fix | Delete
Math = CMath # :nodoc:
[52] Fix | Delete
end
[53] Fix | Delete
[54] Fix | Delete
##
[55] Fix | Delete
# When mathn is required, Fixnum's division is enhanced to
[56] Fix | Delete
# return more precise values from mathematical expressions.
[57] Fix | Delete
#
[58] Fix | Delete
# 2/3*3 # => 0
[59] Fix | Delete
# require 'mathn'
[60] Fix | Delete
# 2/3*3 # => 2
[61] Fix | Delete
[62] Fix | Delete
class Fixnum
[63] Fix | Delete
remove_method :/
[64] Fix | Delete
[65] Fix | Delete
##
[66] Fix | Delete
# +/+ defines the Rational division for Fixnum.
[67] Fix | Delete
#
[68] Fix | Delete
# 1/3 # => (1/3)
[69] Fix | Delete
[70] Fix | Delete
alias / quo
[71] Fix | Delete
end
[72] Fix | Delete
[73] Fix | Delete
##
[74] Fix | Delete
# When mathn is required Bignum's division is enhanced to
[75] Fix | Delete
# return more precise values from mathematical expressions.
[76] Fix | Delete
#
[77] Fix | Delete
# (2**72) / ((2**70) * 3) # => 4/3
[78] Fix | Delete
[79] Fix | Delete
class Bignum
[80] Fix | Delete
remove_method :/
[81] Fix | Delete
[82] Fix | Delete
##
[83] Fix | Delete
# +/+ defines the Rational division for Bignum.
[84] Fix | Delete
#
[85] Fix | Delete
# (2**72) / ((2**70) * 3) # => 4/3
[86] Fix | Delete
[87] Fix | Delete
alias / quo
[88] Fix | Delete
end
[89] Fix | Delete
[90] Fix | Delete
##
[91] Fix | Delete
# When mathn is required, the Math module changes as follows:
[92] Fix | Delete
#
[93] Fix | Delete
# Standard Math module behaviour:
[94] Fix | Delete
# Math.sqrt(4/9) # => 0.0
[95] Fix | Delete
# Math.sqrt(4.0/9.0) # => 0.666666666666667
[96] Fix | Delete
# Math.sqrt(- 4/9) # => Errno::EDOM: Numerical argument out of domain - sqrt
[97] Fix | Delete
#
[98] Fix | Delete
# After require 'mathn', this is changed to:
[99] Fix | Delete
#
[100] Fix | Delete
# require 'mathn'
[101] Fix | Delete
# Math.sqrt(4/9) # => 2/3
[102] Fix | Delete
# Math.sqrt(4.0/9.0) # => 0.666666666666667
[103] Fix | Delete
# Math.sqrt(- 4/9) # => Complex(0, 2/3)
[104] Fix | Delete
[105] Fix | Delete
module Math
[106] Fix | Delete
remove_method(:sqrt)
[107] Fix | Delete
[108] Fix | Delete
##
[109] Fix | Delete
# Computes the square root of +a+. It makes use of Complex and
[110] Fix | Delete
# Rational to have no rounding errors if possible.
[111] Fix | Delete
#
[112] Fix | Delete
# Math.sqrt(4/9) # => 2/3
[113] Fix | Delete
# Math.sqrt(- 4/9) # => Complex(0, 2/3)
[114] Fix | Delete
# Math.sqrt(4.0/9.0) # => 0.666666666666667
[115] Fix | Delete
[116] Fix | Delete
def sqrt(a)
[117] Fix | Delete
if a.kind_of?(Complex)
[118] Fix | Delete
abs = sqrt(a.real*a.real + a.imag*a.imag)
[119] Fix | Delete
x = sqrt((a.real + abs)/Rational(2))
[120] Fix | Delete
y = sqrt((-a.real + abs)/Rational(2))
[121] Fix | Delete
if a.imag >= 0
[122] Fix | Delete
Complex(x, y)
[123] Fix | Delete
else
[124] Fix | Delete
Complex(x, -y)
[125] Fix | Delete
end
[126] Fix | Delete
elsif a.respond_to?(:nan?) and a.nan?
[127] Fix | Delete
a
[128] Fix | Delete
elsif a >= 0
[129] Fix | Delete
rsqrt(a)
[130] Fix | Delete
else
[131] Fix | Delete
Complex(0,rsqrt(-a))
[132] Fix | Delete
end
[133] Fix | Delete
end
[134] Fix | Delete
[135] Fix | Delete
##
[136] Fix | Delete
# Compute square root of a non negative number. This method is
[137] Fix | Delete
# internally used by +Math.sqrt+.
[138] Fix | Delete
[139] Fix | Delete
def rsqrt(a)
[140] Fix | Delete
if a.kind_of?(Float)
[141] Fix | Delete
sqrt!(a)
[142] Fix | Delete
elsif a.kind_of?(Rational)
[143] Fix | Delete
rsqrt(a.numerator)/rsqrt(a.denominator)
[144] Fix | Delete
else
[145] Fix | Delete
src = a
[146] Fix | Delete
max = 2 ** 32
[147] Fix | Delete
byte_a = [src & 0xffffffff]
[148] Fix | Delete
# ruby's bug
[149] Fix | Delete
while (src >= max) and (src >>= 32)
[150] Fix | Delete
byte_a.unshift src & 0xffffffff
[151] Fix | Delete
end
[152] Fix | Delete
[153] Fix | Delete
answer = 0
[154] Fix | Delete
main = 0
[155] Fix | Delete
side = 0
[156] Fix | Delete
for elm in byte_a
[157] Fix | Delete
main = (main << 32) + elm
[158] Fix | Delete
side <<= 16
[159] Fix | Delete
if answer != 0
[160] Fix | Delete
if main * 4 < side * side
[161] Fix | Delete
applo = main.div(side)
[162] Fix | Delete
else
[163] Fix | Delete
applo = ((sqrt!(side * side + 4 * main) - side)/2.0).to_i + 1
[164] Fix | Delete
end
[165] Fix | Delete
else
[166] Fix | Delete
applo = sqrt!(main).to_i + 1
[167] Fix | Delete
end
[168] Fix | Delete
[169] Fix | Delete
while (x = (side + applo) * applo) > main
[170] Fix | Delete
applo -= 1
[171] Fix | Delete
end
[172] Fix | Delete
main -= x
[173] Fix | Delete
answer = (answer << 16) + applo
[174] Fix | Delete
side += applo * 2
[175] Fix | Delete
end
[176] Fix | Delete
if main == 0
[177] Fix | Delete
answer
[178] Fix | Delete
else
[179] Fix | Delete
sqrt!(a)
[180] Fix | Delete
end
[181] Fix | Delete
end
[182] Fix | Delete
end
[183] Fix | Delete
[184] Fix | Delete
class << self
[185] Fix | Delete
remove_method(:sqrt)
[186] Fix | Delete
end
[187] Fix | Delete
module_function :sqrt
[188] Fix | Delete
module_function :rsqrt
[189] Fix | Delete
end
[190] Fix | Delete
[191] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function