Class: Rational

Inherits:
Object
show all
Defined in:
lib/mathn.rb,
lib/yaml/rubytypes.rb

Class Method Details

+ (Object) yaml_new(attrs)

 ``` 214 215 216 217 218 219 220``` ```# File 'lib/yaml/rubytypes.rb', line 214 def Rational.yaml_new(attrs) if attrs.is_a? String Rational(attrs) else Rational(attrs['numerator'], attrs['denominator']) end end```

Instance Method Details

- (Object) **(other)

 ``` 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117``` ```# File 'lib/mathn.rb', line 58 def ** (other) if other.kind_of?(Rational) other2 = other if self < 0 return Complex(self, 0.0) ** other elsif other == 0 return Rational(1,1) elsif self == 0 return Rational(0,1) elsif self == 1 return Rational(1,1) end npd = numerator.prime_division dpd = denominator.prime_division if other < 0 other = -other npd, dpd = dpd, npd end for elm in npd elm[1] = elm[1] * other if !elm[1].kind_of?(Integer) and elm[1].denominator != 1 return Float(self) ** other2 end elm[1] = elm[1].to_i end for elm in dpd elm[1] = elm[1] * other if !elm[1].kind_of?(Integer) and elm[1].denominator != 1 return Float(self) ** other2 end elm[1] = elm[1].to_i end num = Integer.from_prime_division(npd) den = Integer.from_prime_division(dpd) Rational(num,den) elsif other.kind_of?(Integer) if other > 0 num = numerator ** other den = denominator ** other elsif other < 0 num = denominator ** -other den = numerator ** -other elsif other == 0 num = 1 den = 1 end Rational(num, den) elsif other.kind_of?(Float) Float(self) ** other else x , y = other.coerce(self) x ** y end end```

- (Object) to_yaml(output = nil)

 ``` 222 223 224 225 226 227 228 229``` ```# File 'lib/yaml/rubytypes.rb', line 222 def to_yaml(output = nil) YAML::quick_emit(output) do |out| out.map(taguri, to_yaml_style) do |map| map.add('denominator', denominator) map.add('numerator', numerator) end end end```