Module: RubyRunJs::JsMathMethods

Extended by:
Helper
Defined in:
lib/ruby_run_js/object_methods/js_math.rb

Class Method Summary collapse

Methods included from Helper

check_object, get_member, get_member_dot, is_accessor_descriptor, is_callable, is_data_descriptor, is_generic_descriptor, is_primitive, make_error, strict_equality

Methods included from ConversionHelper

#convert_to_js_type, #to_boolean, #to_int32, #to_integer, #to_number, #to_object, #to_primitive, #to_string, #to_uint16, #to_uint32

Class Method Details

.constructor_abs(builtin, this, x) ⇒ Object



22
23
24
25
# File 'lib/ruby_run_js/object_methods/js_math.rb', line 22

def constructor_abs(builtin, this, x)
  x = to_number(x)
  x.abs
end

.constructor_acos(builtin, this, x) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/ruby_run_js/object_methods/js_math.rb', line 27

def constructor_acos(builtin, this, x)
  x = to_number(x)
  begin
    return Math.acos(x)
  rescue
    return Float::NAN
  end
end

.constructor_asin(builtin, this, x) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/ruby_run_js/object_methods/js_math.rb', line 36

def constructor_asin(builtin, this, x)
  x = to_number(x)
  begin
    return Math.asin(x)
  rescue
    return Float::NAN
  end
end

.constructor_atan(builtin, this, x) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/ruby_run_js/object_methods/js_math.rb', line 45

def constructor_atan(builtin, this, x)
  x = to_number(x)
  begin
    return Math.atan(x)
  rescue
    return Float::NAN
  end
end

.constructor_atan2(builtin, this, y, x) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/ruby_run_js/object_methods/js_math.rb', line 54

def constructor_atan2(builtin, this, y, x)
  x = to_number(x)
  y = to_number(y)
  if x.nan? || y.nan?
    return Float::NAN
  end

  Math.atan2(y, x)
end

.constructor_ceil(builtin, this, x) ⇒ Object



64
65
66
67
68
# File 'lib/ruby_run_js/object_methods/js_math.rb', line 64

def constructor_ceil(builtin, this, x)
  x = to_number(x)
  return x unless x.finite?
  x.ceil.to_f
end

.constructor_cos(builtin, this, x) ⇒ Object



70
71
72
73
# File 'lib/ruby_run_js/object_methods/js_math.rb', line 70

def constructor_cos(builtin, this, x)
  x = to_number(x)
  Math.cos(x)
end

.constructor_exp(builtin, this, x) ⇒ Object



75
76
77
78
79
# File 'lib/ruby_run_js/object_methods/js_math.rb', line 75

def constructor_exp(builtin, this, x)
  x = to_number(x)
  return x if x.nan?
  Math.exp(x)
end

.constructor_floor(builtin, this, x) ⇒ Object



81
82
83
84
85
# File 'lib/ruby_run_js/object_methods/js_math.rb', line 81

def constructor_floor(builtin, this, x)
  x = to_number(x)
  return x unless x.finite?
  x.floor.to_f
end

.constructor_log(builtin, this, x) ⇒ Object



87
88
89
90
91
92
# File 'lib/ruby_run_js/object_methods/js_math.rb', line 87

def constructor_log(builtin, this, x)
  x = to_number(x)
  return x if x.nan?
  return Float::NAN if x < 0
  Math.log(x)
end

.constructor_max(builtin, this, *args) ⇒ Object



94
95
96
97
98
99
# File 'lib/ruby_run_js/object_methods/js_math.rb', line 94

def constructor_max(builtin, this, *args)
  return -Float::INFINITY if args.length == 0
  args = args.map { |i| to_number(i) }
  return Float::NAN if args.any?(&:nan?)
  args.max
end

.constructor_min(builtin, this, *args) ⇒ Object



101
102
103
104
105
106
# File 'lib/ruby_run_js/object_methods/js_math.rb', line 101

def constructor_min(builtin, this, *args)
  return Float::INFINITY if args.length == 0
  args = args.map { |i| to_number(i) }
  return Float::NAN if args.any?(&:nan?)
  args.min
end

.constructor_pow(builtin, this, x, y) ⇒ Object



108
109
110
111
112
# File 'lib/ruby_run_js/object_methods/js_math.rb', line 108

def constructor_pow(builtin, this, x, y)
  x = to_number(x)
  y = to_number(y)
  x ** y
end

.constructor_random(builtin, this) ⇒ Object



114
115
116
# File 'lib/ruby_run_js/object_methods/js_math.rb', line 114

def constructor_random(builtin, this)
  Random.rand
end

.constructor_round(builtin, this, x) ⇒ Object



118
119
120
121
122
# File 'lib/ruby_run_js/object_methods/js_math.rb', line 118

def constructor_round(builtin, this, x)
  x = to_number(x)
  return x unless x.finite?
  x.round.to_f
end

.constructor_sin(builtin, this, x) ⇒ Object



124
125
126
127
# File 'lib/ruby_run_js/object_methods/js_math.rb', line 124

def constructor_sin(builtin, this, x)
  x = to_number(x)
  Math.sin(x)
end

.constructor_sqrt(builtin, this, x) ⇒ Object



129
130
131
132
133
# File 'lib/ruby_run_js/object_methods/js_math.rb', line 129

def constructor_sqrt(builtin, this, x)
  x = to_number(x)
  return Float::NAN if x < 0
  Math.sqrt(x)
end

.constructor_tan(builtin, this, x) ⇒ Object



135
136
137
138
# File 'lib/ruby_run_js/object_methods/js_math.rb', line 135

def constructor_tan(builtin, this, x)
  x = to_number(x)
  Math.tan(x)
end

.property_valuesObject



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ruby_run_js/object_methods/js_math.rb', line 9

def property_values
  {
    'E' => 2.7182818284590452354,
    'LN10' => 2.302585092994046,
    'LN2' => 0.6931471805599453,
    'LOG2E' => 1.4426950408889634,
    'LOG10E' => 0.4342944819032518,
    'PI' => 3.1415926535897932,
    'SQRT1_2' => 0.7071067811865476,
    'SQRT2' => 1.4142135623730951
  }
end