Sha256: 25630e94576c4a1d7115be726441e2dda7e4ed021b19264504ae733e048b14ef

Contents?: true

Size: 1.38 KB

Versions: 3

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

require_relative 'basic_math'
require_relative 'basic_object'

module Basic101

  class BasicNumeric < BasicObject

    include Comparable

    include BasicComparisons
    include BasicMath

    attr_reader :value

    def initialize(value)
      @value = value
    end

    def <=>(other)
      return nil unless other.is_a?(BasicNumeric)
      value <=> other.value
    end

    def to_numeric
      self
    end

    def to_i
      @value.to_i
    end

    def to_f
      @value.to_f
    end

    def to_b
      to_i != 0
    end

    def simplest
      if @value.modulo(1) == 0
        to_integer
      else
        self
      end
    end

    def negate
      self.class.new(-value)
    end

    def print_string(output)
      s = format + ' '
      s = ' ' + s unless s =~ /^-/
      output.print s
    end

    def print_new_line(output)
      output.print "\n"
    end

    def abs
      self.class.new(@value.abs)
    end

    def sqr
      BasicFloat.new(@value ** 0.5).simplest
    end

    def exp
      BasicFloat.new(Math::E ** @value).simplest
    end

    def sgn 
      BasicInteger.new(@value <=> 0)
    end

    def log
      BasicFloat.new(Math.log(@value))
    end

    def sin
      BasicFloat.new(Math.sin(@value))
    end

    def cos
      BasicFloat.new(Math.cos(@value))
    end

    def tan
      BasicFloat.new(Math.tan(@value))
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
basic101-1.0.2 lib/basic101/basic_numeric.rb
basic101-1.0.1 lib/basic101/basic_numeric.rb
basic101-1.0.0 lib/basic101/basic_numeric.rb