Class: Counting

Inherits:
Natural show all
Includes:
SGML
Defined in:
lib/m500.rb

Direct Known Subclasses

Zahlen

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SGML

#sgml_id, #tog_sgml_id

Methods inherited from Natural

#==, #div, #divmod, #gcd, #gcd2, #gcdlcm, #is_0?, #lcm, #next_prime

Methods inherited from Numeric

#irrational?, #rational?, #sgml_id, #to_N0

Constructor Details

#initialize(a) ⇒ Counting

Returns a new instance of Counting.



1157
1158
1159
1160
1161
1162
1163
1164
# File 'lib/m500.rb', line 1157

def initialize(a)
  if (a.kind_of?(Counting))
    @a = a.to_i
  elsif a.kind_of?(Fixnum) or a.kind_of?(Bignum)
    @a = a
  else
  end
end

Class Method Details

.new!(num) ⇒ Object



1153
1154
1155
# File 'lib/m500.rb', line 1153

def Counting.new!(num)
  new(num)
end

Instance Method Details

#%(a) ⇒ Object



1230
1231
1232
# File 'lib/m500.rb', line 1230

def % (a)
  self.to_i%(a.to_i)
end

#*(a) ⇒ Object



1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
# File 'lib/m500.rb', line 1191

def * (a)
  t = nil
  if a.kind_of?(Zahlen) or a.kind_of?(Counting) or a.kind_of?(Natural) or a.kind_of?(Bignum) or a.kind_of?(Fixnum)
    a < 0 ? emptySet : Counting(@a*a)
  elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass)
    naught
  elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
    infinity
  else
    x, y = a.coerce(self)
    y < 0 ? emptySet : Counting(x*y)
  end
end

#**(a) ⇒ Object



1212
1213
1214
1215
1216
1217
1218
1219
# File 'lib/m500.rb', line 1212

def ** (a)
  if a.kind_of?(Zahlen) or a.kind_of?(Counting) or a.kind_of?(Natural) or a.kind_of?(Bignum) or a.kind_of?(Fixnum)
    Counting(self.to_i ** a.to_i)
  else
    x, y = a.coerce(self)
    Counting(x ** y)
  end
end

#+(a) ⇒ Object



1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
# File 'lib/m500.rb', line 1165

def + (a)
  if a.kind_of?(Zahlen) or a.kind_of?(Counting) or a.kind_of?(Natural) or a.kind_of?(Bignum) or a.kind_of?(Fixnum)
    Counting(@a+a)
  elsif a.kind_of?(Decimal) or a.kind_of?(Quotient) or a.kind_of?(Fraction)
    Counting(@a+a.to_N0)
  elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass)
    self
  elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
    infinity
  else
    x, y = a.coerce(self)
    Counting(x+y)
  end
end

#-(a) ⇒ Object



1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
# File 'lib/m500.rb', line 1179

def - (a)
  if a.kind_of?(Zahlen) or a.kind_of?(Counting) or a.kind_of?(Natural) or a.kind_of?(Bignum) or a.kind_of?(Fixnum)
    @a-a < 0 ? emptySet : Counting(@a-a)
  elsif a.kind_of?(NaughtClass) or a.kind_of?(EmptySetClass)
    self
  elsif a.kind_of?(NANClass) or a.kind_of?(InfinityClass)
    infinity
  else
    x, y = a.coerce(self)
    x-y < 0 ? emptySet :  Counting(x - y)
  end
end

#/(a) ⇒ Object



1204
1205
1206
1207
1208
1209
1210
1211
# File 'lib/m500.rb', line 1204

def / (a)
  if (a.kind_of?(Counting) or a.kind_of?(Natural)) and (self.to_i%(a.to_i) == 0)
   a < 0 ? emptySet : Counting(@a/ a)
  else
    x, y = a.coerce(self)
    y < 0 ? emptySet : Counting(x/y)
  end
end

#<=>(other) ⇒ Object



1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
# File 'lib/m500.rb', line 1236

def <=> (other)
  unless other.kind_of?(Counting) then other = Counting.new!(other) end
  if @a > other
    return 1
  elsif @a < other
    return  -1
  elsif @a == other
    return 0
  end
end

#absObject



1233
1234
1235
# File 'lib/m500.rb', line 1233

def abs
  @a.to_i.abs
end

#coerce(other) ⇒ Object



1246
1247
1248
1249
1250
1251
1252
# File 'lib/m500.rb', line 1246

def coerce(other)
  if Integer === other
    [other, @a]
  else Float === other
    return other, self.to_f
  end
end

#each {|Counting(@a+1)| ... } ⇒ Object

Yields:



1262
1263
1264
# File 'lib/m500.rb', line 1262

def each
  yield Counting(@a+1)
end

#factorialObject



1223
1224
1225
1226
1227
1228
1229
# File 'lib/m500.rb', line 1223

def factorial
  if self == Counting(0)
    Counting(1)
  else
    self * Counting(self.to_i-1).factorial
  end
end

#factorial_1Object



1220
1221
1222
# File 'lib/m500.rb', line 1220

def factorial_1
  (Counting(1)..self).inject {|product, n| product * n }
end

#hashObject



1303
1304
1305
# File 'lib/m500.rb', line 1303

def hash
  @a.hash
end

#inspectObject



1300
1301
1302
# File 'lib/m500.rb', line 1300

def inspect
  sprintf("Counting(%s)", @a)
end

#nextObject



1253
1254
1255
# File 'lib/m500.rb', line 1253

def next
  self + Counting(1)
end

#succObject



1256
1257
1258
# File 'lib/m500.rb', line 1256

def succ
  self.next
end

#to_DecObject



1289
1290
1291
# File 'lib/m500.rb', line 1289

def to_Dec
  Decimal(@a)
end

#to_fObject



1268
1269
1270
# File 'lib/m500.rb', line 1268

def to_f
  @a.to_f
end

#to_FracObject



1283
1284
1285
# File 'lib/m500.rb', line 1283

def to_Frac
  Fraction(@a,1)
end

#to_iObject



1265
1266
1267
# File 'lib/m500.rb', line 1265

def to_i
  @a.to_i
end

#to_KObject



1295
1296
1297
# File 'lib/m500.rb', line 1295

def to_K
  Kettenbruch(self.to_Frac)
end

#to_NObject



1277
1278
1279
# File 'lib/m500.rb', line 1277

def to_N
  Natural(@a)
end

#to_QObject



1286
1287
1288
# File 'lib/m500.rb', line 1286

def to_Q
  Quotient(@a,1)
end

#to_RObject



1298
1299
# File 'lib/m500.rb', line 1298

def to_R
end

#to_sObject



1274
1275
1276
# File 'lib/m500.rb', line 1274

def to_s
  @a.to_s
end

#to_s!Object



1271
1272
1273
# File 'lib/m500.rb', line 1271

def to_s!
  "Counting(#{@a})"
end

#to_sgmlObject



1150
1151
1152
# File 'lib/m500.rb', line 1150

def to_sgml
  "<mn #{sgml_id}class='counting'>#{@a}</mn>"
end

#to_SigObject



1292
1293
1294
# File 'lib/m500.rb', line 1292

def to_Sig
  Sigma(self.to_Q)
end

#to_ZObject



1280
1281
1282
# File 'lib/m500.rb', line 1280

def to_Z
  Zahlen(@a)
end