Skip to content

Instantly share code, notes, and snippets.

@steakknife steakknife/README.md
Last active Dec 15, 2015

Embed
What would you like to do?
field math <3 ruby

The following patches except BigDecimal are non-working examples; they need to be ported to C because they are compiled-in to MRI.

require 'operations'
require 'identities'
require 'bigdecimal'
class BigDecimal
include Operations
extend Identities
ONE = BigDecimal.new('1.0')
ZERO = BigDecimal.new('0.0')
end
require 'operations'
require 'identities'
class Complex
include Operations
extend Identities
ONE = Complex(1)
ZERO = Complex(0)
end
require 'operations'
require 'identities'
class Float
include Operations
extend Identities
ONE = 1.0
ZERO = 0.0
end
module Identities
extend self
def myself
self.class == Class ? self : self.class
end
def additive_identity
myself::ZERO
end
def multiplicative_identity
myself::ONE
end
alias zero additive_identity
alias one multiplicative_identity
end
require 'operations'
require 'identities'
class Integer
include Operations
extend Identities
ONE = 1
ZERO = 0
end
require 'identities'
module Operations
include Identities
def add other
self + other
end
def additive_inverse
-self
end
def multiply other
self * other
end
def multiplicative_inverse
multiplicative_identity / self
end
end
require 'operations'
require 'identities'
class Rational
include Operations
extend Identities
ONE = 1.to_r
ZERO = 0.to_r
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.