1 # depends on: numeric.rb precision.rb class.rb
7 RADIX = Platform::Float.RADIX
8 ROUNDS = Platform::Float.ROUNDS
9 MIN = Platform::Float.MIN
10 MAX = Platform::Float.MAX
11 MIN_EXP = Platform::Float.MIN_EXP
12 MAX_EXP = Platform::Float.MAX_EXP
13 MIN_10_EXP = Platform::Float.MIN_10_EXP
14 MAX_10_EXP = Platform::Float.MAX_10_EXP
15 DIG = Platform::Float.DIG
16 MANT_DIG = Platform::Float.MANT_DIG
17 EPSILON = Platform::Float.EPSILON
20 def self.induced_from(obj)
22 when Float, Bignum, Fixnum
25 raise TypeError, "failed to convert #{obj.class} into Float"
30 return [other, self] if other.__kind_of__ Float
35 Ruby.primitive :float_uminus
39 Ruby.primitive :float_add
40 b, a = math_coerce other
45 Ruby.primitive :float_sub
46 b, a = math_coerce other
51 Ruby.primitive :float_mul
52 b, a = math_coerce other
57 # see README-DEVELOPERS regarding safe math compiler plugin
61 Ruby.primitive :float_div
62 b, a = math_coerce other
65 alias_method :/, :divide
69 Ruby.primitive :float_divmod
70 b, a = math_coerce other
75 Ruby.primitive :float_pow
76 b, a = math_coerce other
81 return 0 / 0.to_f if other == 0
82 self.divmod(Float(other))[1]
84 alias_method :modulo, :%
87 Ruby.primitive :float_lt
88 b, a = math_coerce other, :compare_error
93 Ruby.primitive :float_le
94 b, a = math_coerce other, :compare_error
99 Ruby.primitive :float_gt
100 b, a = math_coerce other, :compare_error
105 Ruby.primitive :float_ge
106 b, a = math_coerce other, :compare_error
111 Ruby.primitive :float_compare
112 b, a = math_coerce other, :compare_error
117 Ruby.primitive :float_equal
119 b, a = math_coerce(other)
127 Ruby.primitive :float_eql
131 Ruby.primitive :float_isnan
135 Ruby.primitive :float_isinf
139 not (nan? or infinite?)
147 Ruby.primitive :float_to_i
149 alias_method :to_int, :to_i
150 alias_method :truncate, :to_i
153 return (self < 0 ? "-Infinity" : "Infinity") if infinite?
156 str = Platform::Float.to_s_formatted STRLEN, "%#.15g", self
157 e = str.index('e') || str.size
158 unless str[e-1].isdigit
159 str = Platform::Float.to_s_formatted STRLEN, "%#.14e", self
160 e = str.index('e') || str.size
162 str.gsub(/(\.\d+?)(0+)($|e[+-]\d*)/, '\1\3')
164 alias_method :inspect, :to_s
166 def to_s_formatted(fmt)
167 Platform::Float.to_s_formatted STRLEN, fmt, self
169 private :to_s_formatted
172 Ruby.primitive :float_round